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.
5 #include "chrome/browser/chromeos/login/ui/simple_web_view_dialog.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"
39 using content::WebContents
;
40 using views::GridLayout
;
44 const int kLocationBarHeight
= 35;
46 // Margin between screen edge and SimpleWebViewDialog border.
47 const int kExternalMargin
= 60;
49 // Margin between WebView and SimpleWebViewDialog border.
50 const int kInnerMargin
= 2;
52 const SkColor kDialogColor
= SK_ColorWHITE
;
54 class ToolbarRowView
: public views::View
{
57 set_background(views::Background::CreateSolidBackground(kDialogColor
));
60 ~ToolbarRowView() override
{}
62 void Init(views::View
* back
,
65 views::View
* location_bar
) {
66 GridLayout
* layout
= new GridLayout(this);
67 SetLayoutManager(layout
);
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
);
75 column_set
->AddColumn(GridLayout::CENTER
, GridLayout::CENTER
, 0,
76 GridLayout::USE_PREF
, 0, 0);
77 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
79 column_set
->AddColumn(GridLayout::CENTER
, GridLayout::CENTER
, 0,
80 GridLayout::USE_PREF
, 0, 0);
81 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
83 column_set
->AddColumn(GridLayout::FILL
, GridLayout::CENTER
, 1,
84 GridLayout::FIXED
, kLocationBarHeight
, 0);
85 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
87 layout
->StartRow(0, 0);
88 layout
->AddView(back
);
89 layout
->AddView(forward
);
90 layout
->AddView(reload
);
91 layout
->AddView(location_bar
);
95 DISALLOW_COPY_AND_ASSIGN(ToolbarRowView
);
102 // Stub implementation of ContentSettingBubbleModelDelegate.
103 class StubBubbleModelDelegate
: public ContentSettingBubbleModelDelegate
{
105 StubBubbleModelDelegate() {}
106 ~StubBubbleModelDelegate() override
{}
108 // ContentSettingBubbleModelDelegate implementation:
109 void ShowCollectedCookiesDialog(content::WebContents
* web_contents
) override
{
112 void ShowContentSettingsPage(ContentSettingsType type
) override
{}
114 void ShowLearnMorePage(ContentSettingsType type
) override
{}
117 DISALLOW_COPY_AND_ASSIGN(StubBubbleModelDelegate
);
120 // SimpleWebViewDialog class ---------------------------------------------------
122 SimpleWebViewDialog::SimpleWebViewDialog(Profile
* profile
)
129 bubble_model_delegate_(new StubBubbleModelDelegate
) {
130 command_updater_
.reset(new CommandUpdater(this));
131 command_updater_
->UpdateCommandEnabled(IDC_BACK
, true);
132 command_updater_
->UpdateCommandEnabled(IDC_FORWARD
, true);
133 command_updater_
->UpdateCommandEnabled(IDC_STOP
, true);
134 command_updater_
->UpdateCommandEnabled(IDC_RELOAD
, true);
135 command_updater_
->UpdateCommandEnabled(IDC_RELOAD_IGNORING_CACHE
, true);
136 command_updater_
->UpdateCommandEnabled(IDC_RELOAD_CLEARING_CACHE
, true);
139 SimpleWebViewDialog::~SimpleWebViewDialog() {
140 if (web_view_
&& web_view_
->web_contents())
141 web_view_
->web_contents()->SetDelegate(NULL
);
144 void SimpleWebViewDialog::StartLoad(const GURL
& url
) {
145 if (!web_view_container_
.get())
146 web_view_container_
.reset(new views::WebView(profile_
));
147 web_view_
= web_view_container_
.get();
148 web_view_
->set_owned_by_client();
149 web_view_
->GetWebContents()->SetDelegate(this);
150 web_view_
->LoadInitialURL(url
);
152 WebContents
* web_contents
= web_view_
->GetWebContents();
153 DCHECK(web_contents
);
155 // Create the password manager that is needed for the proxy.
156 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
158 autofill::ChromeAutofillClient::FromWebContents(web_contents
));
161 void SimpleWebViewDialog::Init() {
162 toolbar_model_
.reset(new ToolbarModelImpl(this));
164 set_background(views::Background::CreateSolidBackground(kDialogColor
));
166 // Back/Forward buttons.
167 back_
= new views::ImageButton(this);
168 back_
->set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON
|
169 ui::EF_MIDDLE_MOUSE_BUTTON
);
170 back_
->set_tag(IDC_BACK
);
171 back_
->SetImageAlignment(views::ImageButton::ALIGN_RIGHT
,
172 views::ImageButton::ALIGN_TOP
);
173 back_
->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_BACK
));
174 back_
->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_BACK
));
175 back_
->set_id(VIEW_ID_BACK_BUTTON
);
177 forward_
= new views::ImageButton(this);
178 forward_
->set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON
|
179 ui::EF_MIDDLE_MOUSE_BUTTON
);
180 forward_
->set_tag(IDC_FORWARD
);
181 forward_
->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_FORWARD
));
182 forward_
->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FORWARD
));
183 forward_
->set_id(VIEW_ID_FORWARD_BUTTON
);
186 location_bar_
= new LocationBarView(NULL
, profile_
, command_updater_
.get(),
190 reload_
= new ReloadButton(command_updater_
.get());
191 reload_
->set_triggerable_event_flags(ui::EF_LEFT_MOUSE_BUTTON
|
192 ui::EF_MIDDLE_MOUSE_BUTTON
);
193 reload_
->set_tag(IDC_RELOAD
);
194 reload_
->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_RELOAD
));
195 reload_
->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_RELOAD
));
196 reload_
->set_id(VIEW_ID_RELOAD_BUTTON
);
198 // Use separate view to setup custom background.
199 ToolbarRowView
* toolbar_row
= new ToolbarRowView
;
200 toolbar_row
->Init(back_
, forward_
, reload_
, location_bar_
);
203 GridLayout
* layout
= new GridLayout(this);
204 SetLayoutManager(layout
);
206 views::ColumnSet
* column_set
= layout
->AddColumnSet(0);
207 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
208 GridLayout::FIXED
, 0, 0);
210 column_set
= layout
->AddColumnSet(1);
211 column_set
->AddPaddingColumn(0, kInnerMargin
);
212 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
213 GridLayout::FIXED
, 0, 0);
214 column_set
->AddPaddingColumn(0, kInnerMargin
);
216 // Setup layout rows.
217 layout
->StartRow(0, 0);
218 layout
->AddView(toolbar_row
);
220 layout
->AddPaddingRow(0, kInnerMargin
);
222 layout
->StartRow(1, 1);
223 layout
->AddView(web_view_container_
.get());
224 layout
->AddPaddingRow(0, kInnerMargin
);
228 location_bar_
->Init();
229 UpdateReload(web_view_
->web_contents()->IsLoading(), true);
231 gfx::Rect
bounds(CalculateScreenBounds(gfx::Size()));
232 bounds
.Inset(kExternalMargin
, kExternalMargin
);
233 layout
->set_minimum_size(bounds
.size());
238 void SimpleWebViewDialog::Layout() {
239 views::WidgetDelegateView::Layout();
242 views::View
* SimpleWebViewDialog::GetContentsView() {
246 views::View
* SimpleWebViewDialog::GetInitiallyFocusedView() {
250 void SimpleWebViewDialog::ButtonPressed(views::Button
* sender
,
251 const ui::Event
& event
) {
252 command_updater_
->ExecuteCommand(sender
->tag());
255 content::WebContents
* SimpleWebViewDialog::OpenURL(
256 const content::OpenURLParams
& params
) {
257 // As there are no Browsers right now, this could not actually ever work.
262 void SimpleWebViewDialog::NavigationStateChanged(
264 content::InvalidateTypes changed_flags
) {
266 location_bar_
->Update(NULL
);
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
);
278 WebContents
* SimpleWebViewDialog::GetWebContents() {
282 ToolbarModel
* SimpleWebViewDialog::GetToolbarModel() {
283 return toolbar_model_
.get();
286 const ToolbarModel
* SimpleWebViewDialog::GetToolbarModel() const {
287 return toolbar_model_
.get();
290 views::Widget
* SimpleWebViewDialog::CreateViewsBubble(
291 views::BubbleDelegateView
* bubble_delegate
) {
292 return views::BubbleDelegateView::CreateBubble(bubble_delegate
);
295 ContentSettingBubbleModelDelegate
*
296 SimpleWebViewDialog::GetContentSettingBubbleModelDelegate() {
297 return bubble_model_delegate_
.get();
300 void SimpleWebViewDialog::ShowWebsiteSettings(
301 content::WebContents
* web_contents
,
303 const content::SSLStatus
& ssl
) {
305 // TODO (markusheintz@): implement this
308 PageActionImageView
* SimpleWebViewDialog::CreatePageActionImageView(
309 LocationBarView
* owner
,
310 ExtensionAction
* action
) {
311 // Notreached because SimpleWebViewDialog uses a popup-mode LocationBarView,
312 // and it doesn't create PageActionImageViews.
317 content::WebContents
* SimpleWebViewDialog::GetActiveWebContents() const {
318 return web_view_
->web_contents();
321 void SimpleWebViewDialog::ExecuteCommandWithDisposition(
323 WindowOpenDisposition
) {
324 WebContents
* web_contents
= web_view_
->web_contents();
327 if (web_contents
->GetController().CanGoBack()) {
328 location_bar_
->Revert();
329 web_contents
->GetController().GoBack();
333 if (web_contents
->GetController().CanGoForward()) {
334 location_bar_
->Revert();
335 web_contents
->GetController().GoForward();
339 web_contents
->Stop();
342 // Always reload ignoring cache.
343 case IDC_RELOAD_IGNORING_CACHE
:
344 case IDC_RELOAD_CLEARING_CACHE
:
345 location_bar_
->Revert();
346 web_contents
->GetController().ReloadIgnoringCache(true);
353 void SimpleWebViewDialog::LoadImages() {
354 ui::ThemeProvider
* tp
= GetThemeProvider();
356 back_
->SetImage(views::CustomButton::STATE_NORMAL
,
357 tp
->GetImageSkiaNamed(IDR_BACK
));
358 back_
->SetImage(views::CustomButton::STATE_HOVERED
,
359 tp
->GetImageSkiaNamed(IDR_BACK_H
));
360 back_
->SetImage(views::CustomButton::STATE_PRESSED
,
361 tp
->GetImageSkiaNamed(IDR_BACK_P
));
362 back_
->SetImage(views::CustomButton::STATE_DISABLED
,
363 tp
->GetImageSkiaNamed(IDR_BACK_D
));
365 forward_
->SetImage(views::CustomButton::STATE_NORMAL
,
366 tp
->GetImageSkiaNamed(IDR_FORWARD
));
367 forward_
->SetImage(views::CustomButton::STATE_HOVERED
,
368 tp
->GetImageSkiaNamed(IDR_FORWARD_H
));
369 forward_
->SetImage(views::CustomButton::STATE_PRESSED
,
370 tp
->GetImageSkiaNamed(IDR_FORWARD_P
));
371 forward_
->SetImage(views::CustomButton::STATE_DISABLED
,
372 tp
->GetImageSkiaNamed(IDR_FORWARD_D
));
374 reload_
->LoadImages();
377 void SimpleWebViewDialog::UpdateButtons() {
378 const content::NavigationController
& navigation_controller
=
379 web_view_
->web_contents()->GetController();
380 back_
->SetEnabled(navigation_controller
.CanGoBack());
381 forward_
->SetEnabled(navigation_controller
.CanGoForward());
384 void SimpleWebViewDialog::UpdateReload(bool is_loading
, bool force
) {
387 is_loading
? ReloadButton::MODE_STOP
: ReloadButton::MODE_RELOAD
,
392 } // namespace chromeos