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/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"
25
26using chromeos::MobileActivator;
27using content::BrowserThread;
28using content::WebContents;
29using content::WebUIMessageHandler;
30using ui::WebDialogDelegate;
31
32class 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 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;
59
60 private:
61  gfx::NativeWindow dialog_window_;
62  // Cellular network service path.
63  std::string service_path_;
64  DISALLOW_COPY_AND_ASSIGN(MobileSetupDialogDelegate);
65};
66
67// static
68void MobileSetupDialog::Show(const std::string& service_path) {
69  DCHECK_CURRENTLY_ON(BrowserThread::UI);
70  MobileSetupDialogDelegate::GetInstance()->ShowDialog(service_path);
71}
72
73// static
74MobileSetupDialogDelegate* MobileSetupDialogDelegate::GetInstance() {
75  DCHECK_CURRENTLY_ON(BrowserThread::UI);
76  return Singleton<MobileSetupDialogDelegate>::get();
77}
78
79MobileSetupDialogDelegate::MobileSetupDialogDelegate() : dialog_window_(NULL) {
80}
81
82MobileSetupDialogDelegate::~MobileSetupDialogDelegate() {
83}
84
85void MobileSetupDialogDelegate::ShowDialog(const std::string& service_path) {
86  service_path_ = service_path;
87
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();
95    if (login_view)
96      parent = login_view->GetNativeWindow();
97  }
98  // Only the primary user can change this.
99  dialog_window_ = chrome::ShowWebDialog(
100      parent,
101      ProfileManager::GetPrimaryUserProfile(),
102      this);
103}
104
105ui::ModalType MobileSetupDialogDelegate::GetDialogModalType() const {
106  return ui::MODAL_TYPE_SYSTEM;
107}
108
109base::string16 MobileSetupDialogDelegate::GetDialogTitle() const {
110  return l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE);
111}
112
113GURL MobileSetupDialogDelegate::GetDialogContentURL() const {
114  std::string url(chrome::kChromeUIMobileSetupURL);
115  url.append(service_path_);
116  return GURL(url);
117}
118
119void MobileSetupDialogDelegate::GetWebUIMessageHandlers(
120    std::vector<WebUIMessageHandler*>* handlers) const {
121}
122
123void MobileSetupDialogDelegate::GetDialogSize(gfx::Size* size) const {
124  size->SetSize(850, 650);
125}
126
127std::string MobileSetupDialogDelegate::GetDialogArgs() const {
128  return std::string();
129}
130
131void MobileSetupDialogDelegate::OnDialogClosed(const std::string& json_retval) {
132  dialog_window_ = NULL;
133}
134
135void 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;
145    return;
146  }
147
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);
152}
153
154bool MobileSetupDialogDelegate::ShouldShowDialogTitle() const {
155  return true;
156}
157
158bool MobileSetupDialogDelegate::HandleContextMenu(
159    const content::ContextMenuParams& params) {
160  return true;
161}
162