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