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/login_display_host_impl.h"
12 #include "chrome/browser/chromeos/login/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 "content/public/browser/browser_thread.h"
20 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/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 virtual ~MobileSetupDialogDelegate();
45 // WebDialogDelegate overrides.
46 virtual ui::ModalType
GetDialogModalType() const OVERRIDE
;
47 virtual base::string16
GetDialogTitle() const OVERRIDE
;
48 virtual GURL
GetDialogContentURL() const OVERRIDE
;
49 virtual void GetWebUIMessageHandlers(
50 std::vector
<WebUIMessageHandler
*>* handlers
) const OVERRIDE
;
51 virtual void GetDialogSize(gfx::Size
* size
) const OVERRIDE
;
52 virtual std::string
GetDialogArgs() const OVERRIDE
;
53 virtual void OnDialogShown(
54 content::WebUI
* webui
,
55 content::RenderViewHost
* render_view_host
) OVERRIDE
;
56 virtual void OnDialogClosed(const std::string
& json_retval
) OVERRIDE
;
57 virtual void OnCloseContents(WebContents
* source
,
58 bool* out_close_dialog
) OVERRIDE
;
59 virtual bool ShouldShowDialogTitle() const OVERRIDE
;
60 virtual bool HandleContextMenu(
61 const content::ContextMenuParams
& params
) OVERRIDE
;
64 gfx::NativeWindow dialog_window_
;
65 // Cellular network service path.
66 std::string service_path_
;
67 DISALLOW_COPY_AND_ASSIGN(MobileSetupDialogDelegate
);
71 void MobileSetupDialog::Show(const std::string
& service_path
) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
73 MobileSetupDialogDelegate::GetInstance()->ShowDialog(service_path
);
77 MobileSetupDialogDelegate
* MobileSetupDialogDelegate::GetInstance() {
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
79 return Singleton
<MobileSetupDialogDelegate
>::get();
82 MobileSetupDialogDelegate::MobileSetupDialogDelegate() : dialog_window_(NULL
) {
85 MobileSetupDialogDelegate::~MobileSetupDialogDelegate() {
88 void MobileSetupDialogDelegate::ShowDialog(const std::string
& service_path
) {
89 service_path_
= service_path
;
91 gfx::NativeWindow parent
= NULL
;
92 // If we're on the login screen.
93 if (chromeos::LoginDisplayHostImpl::default_host()) {
94 chromeos::LoginDisplayHostImpl
* webui_host
=
95 static_cast<chromeos::LoginDisplayHostImpl
*>(
96 chromeos::LoginDisplayHostImpl::default_host());
97 chromeos::WebUILoginView
* login_view
= webui_host
->GetWebUILoginView();
99 parent
= login_view
->GetNativeWindow();
101 // Only the primary user can change this.
102 dialog_window_
= chrome::ShowWebDialog(
104 ProfileManager::GetPrimaryUserProfile(),
108 ui::ModalType
MobileSetupDialogDelegate::GetDialogModalType() const {
109 return ui::MODAL_TYPE_SYSTEM
;
112 base::string16
MobileSetupDialogDelegate::GetDialogTitle() const {
113 return l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE
);
116 GURL
MobileSetupDialogDelegate::GetDialogContentURL() const {
117 std::string
url(chrome::kChromeUIMobileSetupURL
);
118 url
.append(service_path_
);
122 void MobileSetupDialogDelegate::GetWebUIMessageHandlers(
123 std::vector
<WebUIMessageHandler
*>* handlers
) const {
126 void MobileSetupDialogDelegate::GetDialogSize(gfx::Size
* size
) const {
127 size
->SetSize(850, 650);
130 std::string
MobileSetupDialogDelegate::GetDialogArgs() const {
131 return std::string();
134 void MobileSetupDialogDelegate::OnDialogShown(
135 content::WebUI
* webui
, content::RenderViewHost
* render_view_host
) {
139 void MobileSetupDialogDelegate::OnDialogClosed(const std::string
& json_retval
) {
140 dialog_window_
= NULL
;
143 void MobileSetupDialogDelegate::OnCloseContents(WebContents
* source
,
144 bool* out_close_dialog
) {
145 // If we're exiting, popping up the confirmation dialog can cause a
146 // crash. Note: IsTryingToQuit can be cancelled on other platforms by the
147 // onbeforeunload handler, except on ChromeOS. So IsTryingToQuit is the
148 // appropriate check to use here.
149 if (!dialog_window_
||
150 !MobileActivator::GetInstance()->RunningActivation() ||
151 browser_shutdown::IsTryingToQuit()) {
152 *out_close_dialog
= true;
156 *out_close_dialog
= chrome::ShowMessageBox(dialog_window_
,
157 l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE
),
158 l10n_util::GetStringUTF16(IDS_MOBILE_CANCEL_ACTIVATION
),
159 chrome::MESSAGE_BOX_TYPE_QUESTION
);
162 bool MobileSetupDialogDelegate::ShouldShowDialogTitle() const {
166 bool MobileSetupDialogDelegate::HandleContextMenu(
167 const content::ContextMenuParams
& params
) {