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/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 OnDialogClosed(const std::string& json_retval) override;
54 virtual void OnCloseContents(WebContents* source,
55 bool* out_close_dialog) override;
56 virtual bool ShouldShowDialogTitle() const override;
57 virtual bool HandleContextMenu(
58 const content::ContextMenuParams& params) override;
61 gfx::NativeWindow dialog_window_;
62 // Cellular network service path.
63 std::string service_path_;
64 DISALLOW_COPY_AND_ASSIGN(MobileSetupDialogDelegate);
68 void MobileSetupDialog::Show(const std::string& service_path) {
69 DCHECK_CURRENTLY_ON(BrowserThread::UI);
70 MobileSetupDialogDelegate::GetInstance()->ShowDialog(service_path);
74 MobileSetupDialogDelegate* MobileSetupDialogDelegate::GetInstance() {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76 return Singleton<MobileSetupDialogDelegate>::get();
79 MobileSetupDialogDelegate::MobileSetupDialogDelegate() : dialog_window_(NULL) {
82 MobileSetupDialogDelegate::~MobileSetupDialogDelegate() {
85 void MobileSetupDialogDelegate::ShowDialog(const std::string& service_path) {
86 service_path_ = service_path;
88 gfx::NativeWindow parent = NULL;
89 // If we're on the login screen.
90 if (chromeos::LoginDisplayHostImpl::default_host()) {
91 chromeos::LoginDisplayHostImpl* webui_host =
92 static_cast<chromeos::LoginDisplayHostImpl*>(
93 chromeos::LoginDisplayHostImpl::default_host());
94 chromeos::WebUILoginView* login_view = webui_host->GetWebUILoginView();
96 parent = login_view->GetNativeWindow();
98 // Only the primary user can change this.
99 dialog_window_ = chrome::ShowWebDialog(
101 ProfileManager::GetPrimaryUserProfile(),
105 ui::ModalType MobileSetupDialogDelegate::GetDialogModalType() const {
106 return ui::MODAL_TYPE_SYSTEM;
109 base::string16 MobileSetupDialogDelegate::GetDialogTitle() const {
110 return l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE);
113 GURL MobileSetupDialogDelegate::GetDialogContentURL() const {
114 std::string url(chrome::kChromeUIMobileSetupURL);
115 url.append(service_path_);
119 void MobileSetupDialogDelegate::GetWebUIMessageHandlers(
120 std::vector<WebUIMessageHandler*>* handlers) const {
123 void MobileSetupDialogDelegate::GetDialogSize(gfx::Size* size) const {
124 size->SetSize(850, 650);
127 std::string MobileSetupDialogDelegate::GetDialogArgs() const {
128 return std::string();
131 void MobileSetupDialogDelegate::OnDialogClosed(const std::string& json_retval) {
132 dialog_window_ = NULL;
135 void MobileSetupDialogDelegate::OnCloseContents(WebContents* source,
136 bool* out_close_dialog) {
137 // If we're exiting, popping up the confirmation dialog can cause a
138 // crash. Note: IsTryingToQuit can be cancelled on other platforms by the
139 // onbeforeunload handler, except on ChromeOS. So IsTryingToQuit is the
140 // appropriate check to use here.
141 if (!dialog_window_ ||
142 !MobileActivator::GetInstance()->RunningActivation() ||
143 browser_shutdown::IsTryingToQuit()) {
144 *out_close_dialog = true;
148 *out_close_dialog = chrome::ShowMessageBox(dialog_window_,
149 l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE),
150 l10n_util::GetStringUTF16(IDS_MOBILE_CANCEL_ACTIVATION),
151 chrome::MESSAGE_BOX_TYPE_QUESTION);
154 bool MobileSetupDialogDelegate::ShouldShowDialogTitle() const {
158 bool MobileSetupDialogDelegate::HandleContextMenu(
159 const content::ContextMenuParams& params) {