Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / simple_web_view_dialog.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/login/simple_web_view_dialog.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/chromeos/login/captive_portal_window_proxy.h"
13 #include "chrome/browser/chromeos/login/helper.h"
14 #include "chrome/browser/command_updater.h"
15 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/content_settings/content_setting_bubble_model_delegate.h"
20 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
21 #include "chrome/browser/ui/view_ids.h"
22 #include "chrome/browser/ui/views/location_bar/location_icon_view.h"
23 #include "chrome/browser/ui/views/toolbar/reload_button.h"
24 #include "components/password_manager/core/browser/password_manager.h"
25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/web_contents.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ipc/ipc_message.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/theme_provider.h"
33 #include "ui/views/background.h"
34 #include "ui/views/controls/webview/webview.h"
35 #include "ui/views/layout/grid_layout.h"
36 #include "ui/views/layout/layout_constants.h"
37 #include "ui/views/view.h"
38 #include "ui/views/widget/widget.h"
39
40 using content::WebContents;
41 using views::GridLayout;
42
43 namespace {
44
45 const int kLocationBarHeight = 35;
46
47 // Margin between screen edge and SimpleWebViewDialog border.
48 const int kExternalMargin = 60;
49
50 // Margin between WebView and SimpleWebViewDialog border.
51 const int kInnerMargin = 2;
52
53 const SkColor kDialogColor = SK_ColorWHITE;
54
55 class ToolbarRowView : public views::View {
56  public:
57   ToolbarRowView() {
58     set_background(views::Background::CreateSolidBackground(kDialogColor));
59   }
60
61   virtual ~ToolbarRowView() {}
62
63   void Init(views::View* back,
64             views::View* forward,
65             views::View* reload,
66             views::View* location_bar) {
67     GridLayout* layout = new GridLayout(this);
68     SetLayoutManager(layout);
69
70     // Back button.
71     views::ColumnSet* column_set = layout->AddColumnSet(0);
72     column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
73                           GridLayout::USE_PREF, 0, 0);
74     column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
75     // Forward button.
76     column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
77                           GridLayout::USE_PREF, 0, 0);
78     column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
79     // Reload button.
80     column_set->AddColumn(GridLayout::CENTER, GridLayout::CENTER, 0,
81                           GridLayout::USE_PREF, 0, 0);
82     column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
83     // Location bar.
84     column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
85                           GridLayout::FIXED, kLocationBarHeight, 0);
86     column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
87
88     layout->StartRow(0, 0);
89     layout->AddView(back);
90     layout->AddView(forward);
91     layout->AddView(reload);
92     layout->AddView(location_bar);
93   }
94
95  private:
96   DISALLOW_COPY_AND_ASSIGN(ToolbarRowView);
97 };
98
99 }  // namespace
100
101 namespace chromeos {
102
103 // Stub implementation of ContentSettingBubbleModelDelegate.
104 class StubBubbleModelDelegate : public ContentSettingBubbleModelDelegate {
105  public:
106   StubBubbleModelDelegate() {}
107   virtual ~StubBubbleModelDelegate() {}
108
109   // ContentSettingBubbleModelDelegate implementation:
110   virtual void ShowCollectedCookiesDialog(
111       content::WebContents* web_contents) OVERRIDE {
112   }
113
114   virtual void ShowContentSettingsPage(ContentSettingsType type) OVERRIDE {
115   }
116
117  private:
118   DISALLOW_COPY_AND_ASSIGN(StubBubbleModelDelegate);
119 };
120
121 // SimpleWebViewDialog class ---------------------------------------------------
122
123 SimpleWebViewDialog::SimpleWebViewDialog(Profile* profile)
124     : profile_(profile),
125       back_(NULL),
126       forward_(NULL),
127       reload_(NULL),
128       location_bar_(NULL),
129       web_view_(NULL),
130       bubble_model_delegate_(new StubBubbleModelDelegate) {
131   command_updater_.reset(new CommandUpdater(this));
132   command_updater_->UpdateCommandEnabled(IDC_BACK, true);
133   command_updater_->UpdateCommandEnabled(IDC_FORWARD, true);
134   command_updater_->UpdateCommandEnabled(IDC_STOP, true);
135   command_updater_->UpdateCommandEnabled(IDC_RELOAD, true);
136   command_updater_->UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE, true);
137   command_updater_->UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE, true);
138 }
139
140 SimpleWebViewDialog::~SimpleWebViewDialog() {
141   if (web_view_ && web_view_->web_contents())
142     web_view_->web_contents()->SetDelegate(NULL);
143 }
144
145 void SimpleWebViewDialog::StartLoad(const GURL& url) {
146   if (!web_view_container_.get())
147     web_view_container_.reset(new views::WebView(profile_));
148   web_view_ = web_view_container_.get();
149   web_view_->set_owned_by_client();
150   web_view_->GetWebContents()->SetDelegate(this);
151   web_view_->LoadInitialURL(url);
152
153   WebContents* web_contents = web_view_->GetWebContents();
154   DCHECK(web_contents);
155
156   // Create the password manager that is needed for the proxy.
157   ChromePasswordManagerClient::CreateForWebContentsWithAutofillManagerDelegate(
158       web_contents,
159       autofill::TabAutofillManagerDelegate::FromWebContents(web_contents));
160 }
161
162 void SimpleWebViewDialog::Init() {
163   toolbar_model_.reset(new ToolbarModelImpl(this));
164
165   set_background(views::Background::CreateSolidBackground(kDialogColor));
166
167   // Back/Forward buttons.
168   back_ = new views::ImageButton(this);
169   back_->set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON |
170                                      ui::EF_MIDDLE_MOUSE_BUTTON);
171   back_->set_tag(IDC_BACK);
172   back_->SetImageAlignment(views::ImageButton::ALIGN_RIGHT,
173                            views::ImageButton::ALIGN_TOP);
174   back_->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_BACK));
175   back_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_BACK));
176   back_->set_id(VIEW_ID_BACK_BUTTON);
177
178   forward_ = new views::ImageButton(this);
179   forward_->set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON |
180                                         ui::EF_MIDDLE_MOUSE_BUTTON);
181   forward_->set_tag(IDC_FORWARD);
182   forward_->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_FORWARD));
183   forward_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FORWARD));
184   forward_->set_id(VIEW_ID_FORWARD_BUTTON);
185
186   // Location bar.
187   location_bar_ = new LocationBarView(NULL, profile_, command_updater_.get(),
188                                       this, true);
189
190   // Reload button.
191   reload_ = new ReloadButton(command_updater_.get());
192   reload_->set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON |
193                                        ui::EF_MIDDLE_MOUSE_BUTTON);
194   reload_->set_tag(IDC_RELOAD);
195   reload_->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_RELOAD));
196   reload_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_RELOAD));
197   reload_->set_id(VIEW_ID_RELOAD_BUTTON);
198
199   // Use separate view to setup custom background.
200   ToolbarRowView* toolbar_row = new ToolbarRowView;
201   toolbar_row->Init(back_, forward_, reload_, location_bar_);
202
203   // Layout.
204   GridLayout* layout = new GridLayout(this);
205   SetLayoutManager(layout);
206
207   views::ColumnSet* column_set = layout->AddColumnSet(0);
208   column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
209                         GridLayout::FIXED, 0, 0);
210
211   column_set = layout->AddColumnSet(1);
212   column_set->AddPaddingColumn(0, kInnerMargin);
213   column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
214                         GridLayout::FIXED, 0, 0);
215   column_set->AddPaddingColumn(0, kInnerMargin);
216
217   // Setup layout rows.
218   layout->StartRow(0, 0);
219   layout->AddView(toolbar_row);
220
221   layout->AddPaddingRow(0, kInnerMargin);
222
223   layout->StartRow(1, 1);
224   layout->AddView(web_view_container_.get());
225   layout->AddPaddingRow(0, kInnerMargin);
226
227   LoadImages();
228
229   location_bar_->Init();
230   UpdateReload(web_view_->web_contents()->IsLoading(), true);
231
232   gfx::Rect bounds(CalculateScreenBounds(gfx::Size()));
233   bounds.Inset(kExternalMargin, kExternalMargin);
234   layout->set_minimum_size(bounds.size());
235
236   Layout();
237 }
238
239 void SimpleWebViewDialog::Layout() {
240   views::WidgetDelegateView::Layout();
241 }
242
243 views::View* SimpleWebViewDialog::GetContentsView() {
244   return this;
245 }
246
247 views::View* SimpleWebViewDialog::GetInitiallyFocusedView() {
248   return web_view_;
249 }
250
251 void SimpleWebViewDialog::ButtonPressed(views::Button* sender,
252                                         const ui::Event& event) {
253   command_updater_->ExecuteCommand(sender->tag());
254 }
255
256 content::WebContents* SimpleWebViewDialog::OpenURL(
257     const content::OpenURLParams& params) {
258   // As there are no Browsers right now, this could not actually ever work.
259   NOTIMPLEMENTED();
260   return NULL;
261 }
262
263 void SimpleWebViewDialog::NavigationStateChanged(
264     const WebContents* source, unsigned changed_flags) {
265   if (location_bar_) {
266     location_bar_->Update(NULL);
267     UpdateButtons();
268   }
269 }
270
271 void SimpleWebViewDialog::LoadingStateChanged(WebContents* source,
272     bool to_different_document) {
273   bool is_loading = source->IsLoading();
274   UpdateReload(is_loading && to_different_document, false);
275   command_updater_->UpdateCommandEnabled(IDC_STOP, is_loading);
276 }
277
278 WebContents* SimpleWebViewDialog::GetWebContents() {
279   return NULL;
280 }
281
282 ToolbarModel* SimpleWebViewDialog::GetToolbarModel() {
283   return toolbar_model_.get();
284 }
285
286 const ToolbarModel* SimpleWebViewDialog::GetToolbarModel() const {
287   return toolbar_model_.get();
288 }
289
290 InstantController* SimpleWebViewDialog::GetInstant() {
291   return NULL;
292 }
293
294 views::Widget* SimpleWebViewDialog::CreateViewsBubble(
295     views::BubbleDelegateView* bubble_delegate) {
296   return views::BubbleDelegateView::CreateBubble(bubble_delegate);
297 }
298
299 ContentSettingBubbleModelDelegate*
300 SimpleWebViewDialog::GetContentSettingBubbleModelDelegate() {
301   return bubble_model_delegate_.get();
302 }
303
304 void SimpleWebViewDialog::ShowWebsiteSettings(
305     content::WebContents* web_contents,
306     const GURL& url,
307     const content::SSLStatus& ssl) {
308   NOTIMPLEMENTED();
309   // TODO (ygorshenin@,markusheintz@): implement this
310 }
311
312 PageActionImageView* SimpleWebViewDialog::CreatePageActionImageView(
313     LocationBarView* owner,
314     ExtensionAction* action) {
315   // Notreached because SimpleWebViewDialog uses a popup-mode LocationBarView,
316   // and it doesn't create PageActionImageViews.
317   NOTREACHED();
318   return NULL;
319 }
320
321 content::WebContents* SimpleWebViewDialog::GetActiveWebContents() const {
322   return web_view_->web_contents();
323 }
324
325 bool SimpleWebViewDialog::InTabbedBrowser() const {
326   return false;
327 }
328
329 void SimpleWebViewDialog::ExecuteCommandWithDisposition(
330     int id,
331     WindowOpenDisposition) {
332   WebContents* web_contents = web_view_->web_contents();
333   switch (id) {
334     case IDC_BACK:
335       if (web_contents->GetController().CanGoBack()) {
336         location_bar_->Revert();
337         web_contents->GetController().GoBack();
338       }
339       break;
340     case IDC_FORWARD:
341       if (web_contents->GetController().CanGoForward()) {
342         location_bar_->Revert();
343         web_contents->GetController().GoForward();
344       }
345       break;
346     case IDC_STOP:
347       web_contents->Stop();
348       break;
349     case IDC_RELOAD:
350       // Always reload ignoring cache.
351     case IDC_RELOAD_IGNORING_CACHE:
352     case IDC_RELOAD_CLEARING_CACHE:
353       location_bar_->Revert();
354       web_contents->GetController().ReloadIgnoringCache(true);
355       break;
356     default:
357       NOTREACHED();
358   }
359 }
360
361 void SimpleWebViewDialog::LoadImages() {
362   ui::ThemeProvider* tp = GetThemeProvider();
363
364   back_->SetImage(views::CustomButton::STATE_NORMAL,
365                   tp->GetImageSkiaNamed(IDR_BACK));
366   back_->SetImage(views::CustomButton::STATE_HOVERED,
367                   tp->GetImageSkiaNamed(IDR_BACK_H));
368   back_->SetImage(views::CustomButton::STATE_PRESSED,
369                   tp->GetImageSkiaNamed(IDR_BACK_P));
370   back_->SetImage(views::CustomButton::STATE_DISABLED,
371                   tp->GetImageSkiaNamed(IDR_BACK_D));
372
373   forward_->SetImage(views::CustomButton::STATE_NORMAL,
374                      tp->GetImageSkiaNamed(IDR_FORWARD));
375   forward_->SetImage(views::CustomButton::STATE_HOVERED,
376                      tp->GetImageSkiaNamed(IDR_FORWARD_H));
377   forward_->SetImage(views::CustomButton::STATE_PRESSED,
378                      tp->GetImageSkiaNamed(IDR_FORWARD_P));
379   forward_->SetImage(views::CustomButton::STATE_DISABLED,
380                      tp->GetImageSkiaNamed(IDR_FORWARD_D));
381
382   reload_->LoadImages();
383 }
384
385 void SimpleWebViewDialog::UpdateButtons() {
386   const content::NavigationController& navigation_controller =
387       web_view_->web_contents()->GetController();
388   back_->SetEnabled(navigation_controller.CanGoBack());
389   forward_->SetEnabled(navigation_controller.CanGoForward());
390 }
391
392 void SimpleWebViewDialog::UpdateReload(bool is_loading, bool force) {
393   if (reload_) {
394     reload_->ChangeMode(
395         is_loading ? ReloadButton::MODE_STOP : ReloadButton::MODE_RELOAD,
396         force);
397   }
398 }
399
400 }  // namespace chromeos