- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / chromeos / mobile_setup_dialog.cc
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.
4
5 #include "chrome/browser/ui/webui/chromeos/mobile_setup_dialog.h"
6
7 #include "base/bind.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"
25
26 using chromeos::MobileActivator;
27 using content::BrowserThread;
28 using content::WebContents;
29 using content::WebUIMessageHandler;
30 using ui::WebDialogDelegate;
31
32 class MobileSetupDialogDelegate : public WebDialogDelegate {
33  public:
34   static MobileSetupDialogDelegate* GetInstance();
35   void ShowDialog(const std::string& service_path);
36
37  protected:
38   friend struct DefaultSingletonTraits<MobileSetupDialogDelegate>;
39
40   MobileSetupDialogDelegate();
41   virtual ~MobileSetupDialogDelegate();
42
43   void OnCloseDialog();
44
45   // WebDialogDelegate overrides.
46   virtual ui::ModalType GetDialogModalType() const OVERRIDE;
47   virtual 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;
62
63  private:
64   gfx::NativeWindow dialog_window_;
65   // Cellular network service path.
66   std::string service_path_;
67   DISALLOW_COPY_AND_ASSIGN(MobileSetupDialogDelegate);
68 };
69
70 // static
71 void MobileSetupDialog::Show(const std::string& service_path) {
72   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
73   MobileSetupDialogDelegate::GetInstance()->ShowDialog(service_path);
74 }
75
76 // static
77 MobileSetupDialogDelegate* MobileSetupDialogDelegate::GetInstance() {
78   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
79   return Singleton<MobileSetupDialogDelegate>::get();
80 }
81
82 MobileSetupDialogDelegate::MobileSetupDialogDelegate() : dialog_window_(NULL) {
83 }
84
85 MobileSetupDialogDelegate::~MobileSetupDialogDelegate() {
86 }
87
88 void MobileSetupDialogDelegate::ShowDialog(const std::string& service_path) {
89   service_path_ = service_path;
90
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();
98     if (login_view)
99       parent = login_view->GetNativeWindow();
100   }
101
102   dialog_window_ = chrome::ShowWebDialog(
103       parent,
104       ProfileManager::GetDefaultProfileOrOffTheRecord(),
105       this);
106 }
107
108 ui::ModalType MobileSetupDialogDelegate::GetDialogModalType() const {
109   return ui::MODAL_TYPE_SYSTEM;
110 }
111
112 string16 MobileSetupDialogDelegate::GetDialogTitle() const {
113   return l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE);
114 }
115
116 GURL MobileSetupDialogDelegate::GetDialogContentURL() const {
117   std::string url(chrome::kChromeUIMobileSetupURL);
118   url.append(service_path_);
119   return GURL(url);
120 }
121
122 void MobileSetupDialogDelegate::GetWebUIMessageHandlers(
123     std::vector<WebUIMessageHandler*>* handlers) const {
124 }
125
126 void MobileSetupDialogDelegate::GetDialogSize(gfx::Size* size) const {
127   size->SetSize(850, 650);
128 }
129
130 std::string MobileSetupDialogDelegate::GetDialogArgs() const {
131   return std::string();
132 }
133
134 void MobileSetupDialogDelegate::OnDialogShown(
135     content::WebUI* webui, content::RenderViewHost* render_view_host) {
136 }
137
138
139 void MobileSetupDialogDelegate::OnDialogClosed(const std::string& json_retval) {
140   dialog_window_ = NULL;
141 }
142
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;
153     return;
154   }
155
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);
160 }
161
162 bool MobileSetupDialogDelegate::ShouldShowDialogTitle() const {
163   return true;
164 }
165
166 bool MobileSetupDialogDelegate::HandleContextMenu(
167     const content::ContextMenuParams& params) {
168   return true;
169 }