Home | History | Annotate | Download | only in chromeos
      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::CellularNetwork;
     27 using chromeos::MobileActivator;
     28 using content::BrowserThread;
     29 using content::WebContents;
     30 using content::WebUIMessageHandler;
     31 using ui::WebDialogDelegate;
     32 
     33 class MobileSetupDialogDelegate : public WebDialogDelegate,
     34                                   public MobileActivator::Observer {
     35  public:
     36   static MobileSetupDialogDelegate* GetInstance();
     37   void ShowDialog(const std::string& service_path);
     38 
     39  protected:
     40   friend struct DefaultSingletonTraits<MobileSetupDialogDelegate>;
     41 
     42   MobileSetupDialogDelegate();
     43   virtual ~MobileSetupDialogDelegate();
     44 
     45   void OnCloseDialog();
     46 
     47   // WebDialogDelegate overrides.
     48   virtual ui::ModalType GetDialogModalType() const OVERRIDE;
     49   virtual string16 GetDialogTitle() const OVERRIDE;
     50   virtual GURL GetDialogContentURL() const OVERRIDE;
     51   virtual void GetWebUIMessageHandlers(
     52       std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE;
     53   virtual void GetDialogSize(gfx::Size* size) const OVERRIDE;
     54   virtual std::string GetDialogArgs() const OVERRIDE;
     55   virtual void OnDialogShown(
     56       content::WebUI* webui,
     57       content::RenderViewHost* render_view_host) OVERRIDE;
     58   virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE;
     59   virtual void OnCloseContents(WebContents* source,
     60                                bool* out_close_dialog) OVERRIDE;
     61   virtual bool ShouldShowDialogTitle() const OVERRIDE;
     62   virtual bool HandleContextMenu(
     63       const content::ContextMenuParams& params) OVERRIDE;
     64 
     65   // MobileActivator::Observer overrides.
     66   virtual void OnActivationStateChanged(
     67       CellularNetwork* network,
     68       MobileActivator::PlanActivationState state,
     69       const std::string& error_description) OVERRIDE;
     70 
     71  private:
     72   gfx::NativeWindow dialog_window_;
     73   // Cellular network service path.
     74   std::string service_path_;
     75   DISALLOW_COPY_AND_ASSIGN(MobileSetupDialogDelegate);
     76 };
     77 
     78 // static
     79 void MobileSetupDialog::Show(const std::string& service_path) {
     80   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     81   MobileSetupDialogDelegate::GetInstance()->ShowDialog(service_path);
     82 }
     83 
     84 // static
     85 MobileSetupDialogDelegate* MobileSetupDialogDelegate::GetInstance() {
     86   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     87   return Singleton<MobileSetupDialogDelegate>::get();
     88 }
     89 
     90 MobileSetupDialogDelegate::MobileSetupDialogDelegate() : dialog_window_(NULL) {
     91 }
     92 
     93 MobileSetupDialogDelegate::~MobileSetupDialogDelegate() {
     94   MobileActivator::GetInstance()->RemoveObserver(this);
     95 }
     96 
     97 void MobileSetupDialogDelegate::ShowDialog(const std::string& service_path) {
     98   service_path_ = service_path;
     99 
    100   gfx::NativeWindow parent = NULL;
    101   // If we're on the login screen.
    102   if (chromeos::LoginDisplayHostImpl::default_host()) {
    103     chromeos::LoginDisplayHostImpl* webui_host =
    104         static_cast<chromeos::LoginDisplayHostImpl*>(
    105             chromeos::LoginDisplayHostImpl::default_host());
    106     chromeos::WebUILoginView* login_view = webui_host->GetWebUILoginView();
    107     if (login_view)
    108       parent = login_view->GetNativeWindow();
    109   }
    110 
    111   dialog_window_ = chrome::ShowWebDialog(
    112       parent,
    113       ProfileManager::GetDefaultProfileOrOffTheRecord(),
    114       this);
    115 }
    116 
    117 ui::ModalType MobileSetupDialogDelegate::GetDialogModalType() const {
    118   return ui::MODAL_TYPE_SYSTEM;
    119 }
    120 
    121 string16 MobileSetupDialogDelegate::GetDialogTitle() const {
    122   return l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE);
    123 }
    124 
    125 GURL MobileSetupDialogDelegate::GetDialogContentURL() const {
    126   std::string url(chrome::kChromeUIMobileSetupURL);
    127   url.append(service_path_);
    128   return GURL(url);
    129 }
    130 
    131 void MobileSetupDialogDelegate::GetWebUIMessageHandlers(
    132     std::vector<WebUIMessageHandler*>* handlers) const {
    133 }
    134 
    135 void MobileSetupDialogDelegate::GetDialogSize(gfx::Size* size) const {
    136   size->SetSize(850, 650);
    137 }
    138 
    139 std::string MobileSetupDialogDelegate::GetDialogArgs() const {
    140   return std::string();
    141 }
    142 
    143 void MobileSetupDialogDelegate::OnDialogShown(
    144     content::WebUI* webui, content::RenderViewHost* render_view_host) {
    145   MobileActivator::GetInstance()->AddObserver(this);
    146 }
    147 
    148 
    149 void MobileSetupDialogDelegate::OnDialogClosed(const std::string& json_retval) {
    150   MobileActivator::GetInstance()->RemoveObserver(this);
    151   dialog_window_ = NULL;
    152 }
    153 
    154 void MobileSetupDialogDelegate::OnCloseContents(WebContents* source,
    155                                                 bool* out_close_dialog) {
    156   // If we're exiting, popping up the confirmation dialog can cause a
    157   // crash. Note: IsTryingToQuit can be cancelled on other platforms by the
    158   // onbeforeunload handler, except on ChromeOS. So IsTryingToQuit is the
    159   // appropriate check to use here.
    160   if (!dialog_window_ ||
    161       !MobileActivator::GetInstance()->RunningActivation() ||
    162       browser_shutdown::IsTryingToQuit()) {
    163     *out_close_dialog = true;
    164     return;
    165   }
    166 
    167   *out_close_dialog = chrome::ShowMessageBox(dialog_window_,
    168       l10n_util::GetStringUTF16(IDS_MOBILE_SETUP_TITLE),
    169       l10n_util::GetStringUTF16(IDS_MOBILE_CANCEL_ACTIVATION),
    170       chrome::MESSAGE_BOX_TYPE_QUESTION);
    171 }
    172 
    173 bool MobileSetupDialogDelegate::ShouldShowDialogTitle() const {
    174   return true;
    175 }
    176 
    177 bool MobileSetupDialogDelegate::HandleContextMenu(
    178     const content::ContextMenuParams& params) {
    179   return true;
    180 }
    181 
    182 void MobileSetupDialogDelegate::OnActivationStateChanged(
    183     CellularNetwork* network,
    184     MobileActivator::PlanActivationState state,
    185     const std::string& error_description) {
    186 }
    187