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.
5 #include "chrome/browser/ui/webui/chromeos/mobile_setup_dialog.h"
8 #include "base/memory/singleton.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_shutdown.h"
11 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
12 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
13 #include "chrome/browser/chromeos/mobile/mobile_activator.h"
14 #include "chrome/browser/platform_util.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/browser_dialogs.h"
17 #include "chrome/browser/ui/simple_message_box.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/geometry/size.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/web_dialogs/web_dialog_delegate.h"
26 using chromeos::MobileActivator
;
27 using content::BrowserThread
;
28 using content::WebContents
;
29 using content::WebUIMessageHandler
;
30 using ui::WebDialogDelegate
;
32 class MobileSetupDialogDelegate
: public WebDialogDelegate
{
34 static MobileSetupDialogDelegate
* GetInstance();
35 void ShowDialog(const std::string
& service_path
);
38 friend struct DefaultSingletonTraits
<MobileSetupDialogDelegate
>;
40 MobileSetupDialogDelegate();
41 ~MobileSetupDialogDelegate() override
;
45 // WebDialogDelegate overrides.
46 ui::ModalType
GetDialogModalType() const override
;
47 base::string16
GetDialogTitle() const override
;
48 GURL
GetDialogContentURL() const override
;
49 void GetWebUIMessageHandlers(
50 std::vector
<WebUIMessageHandler
*>* handlers
) const override
;
51 void GetDialogSize(gfx::Size
* size
) const override
;
52 std::string
GetDialogArgs() const override
;
53 void OnDialogClosed(const std::string
& json_retval
) override
;
54 void OnCloseContents(WebContents
* source
, bool* out_close_dialog
) override
;
55 bool ShouldShowDialogTitle() const override
;
56 bool HandleContextMenu(const content::ContextMenuParams
& params
) override
;
59 gfx::NativeWindow dialog_window_
;
60 // Cellular network service path.
61 std::string service_path_
;
62 DISALLOW_COPY_AND_ASSIGN(MobileSetupDialogDelegate
);
66 void MobileSetupDialog::Show(const std::string
& service_path
) {
67 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
68 MobileSetupDialogDelegate::GetInstance()->ShowDialog(service_path
);
72 MobileSetupDialogDelegate
* MobileSetupDialogDelegate::GetInstance() {
73 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
74 return Singleton
<MobileSetupDialogDelegate
>::get();
77 MobileSetupDialogDelegate::MobileSetupDialogDelegate() : dialog_window_(NULL
) {
80 MobileSetupDialogDelegate::~MobileSetupDialogDelegate() {
83 void MobileSetupDialogDelegate::ShowDialog(const std::string
& service_path
) {
84 service_path_
= service_path
;
86 gfx::NativeWindow parent
= NULL
;
87 // If we're on the login screen.
88 if (chromeos::LoginDisplayHostImpl::default_host()) {
89 chromeos::LoginDisplayHostImpl
* webui_host
=
90 static_cast<chromeos::LoginDisplayHostImpl
*>(
91 chromeos::LoginDisplayHostImpl::default_host());
92 chromeos::WebUILoginView
* login_view
= webui_host
->GetWebUILoginView();
94 parent
= login_view
->GetNativeWindow();
96 // Only the primary user can change this.
97 dialog_window_
= chrome::ShowWebDialog(
99 ProfileManager::GetPrimaryUserProfile(),
103 ui::ModalType
MobileSetupDialogDelegate::GetDialogModalType() const {
104 return ui::MODAL_TYPE_SYSTEM
;
107 base::string16
MobileSetupDialogDelegate::GetDialogTitle() const {
108 return l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE
);
111 GURL
MobileSetupDialogDelegate::GetDialogContentURL() const {
112 std::string
url(chrome::kChromeUIMobileSetupURL
);
113 url
.append(service_path_
);
117 void MobileSetupDialogDelegate::GetWebUIMessageHandlers(
118 std::vector
<WebUIMessageHandler
*>* handlers
) const {
121 void MobileSetupDialogDelegate::GetDialogSize(gfx::Size
* size
) const {
122 size
->SetSize(850, 650);
125 std::string
MobileSetupDialogDelegate::GetDialogArgs() const {
126 return std::string();
129 void MobileSetupDialogDelegate::OnDialogClosed(const std::string
& json_retval
) {
130 dialog_window_
= NULL
;
133 void MobileSetupDialogDelegate::OnCloseContents(WebContents
* source
,
134 bool* out_close_dialog
) {
135 // If we're exiting, popping up the confirmation dialog can cause a
136 // crash. Note: IsTryingToQuit can be cancelled on other platforms by the
137 // onbeforeunload handler, except on ChromeOS. So IsTryingToQuit is the
138 // appropriate check to use here.
139 if (!dialog_window_
||
140 !MobileActivator::GetInstance()->RunningActivation() ||
141 browser_shutdown::IsTryingToQuit()) {
142 *out_close_dialog
= true;
146 *out_close_dialog
= chrome::ShowMessageBox(dialog_window_
,
147 l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE
),
148 l10n_util::GetStringUTF16(IDS_MOBILE_CANCEL_ACTIVATION
),
149 chrome::MESSAGE_BOX_TYPE_QUESTION
);
152 bool MobileSetupDialogDelegate::ShouldShowDialogTitle() const {
156 bool MobileSetupDialogDelegate::HandleContextMenu(
157 const content::ContextMenuParams
& params
) {