Home | History | Annotate | Download | only in charger_replace
      1 // Copyright (c) 2013 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/chromeos/charger_replace/charger_replacement_dialog.h"
      6 
      7 #include "base/prefs/pref_service.h"
      8 #include "chrome/browser/browser_process.h"
      9 #include "chrome/browser/profiles/profile_manager.h"
     10 #include "chrome/browser/ui/browser_dialogs.h"
     11 #include "chrome/browser/ui/webui/chromeos/charger_replacement_handler.h"
     12 #include "chrome/common/url_constants.h"
     13 #include "chrome/grit/generated_resources.h"
     14 #include "content/public/browser/user_metrics.h"
     15 #include "ui/aura/window.h"
     16 #include "ui/base/l10n/l10n_util.h"
     17 #include "ui/gfx/size.h"
     18 
     19 using content::WebContents;
     20 using content::WebUIMessageHandler;
     21 
     22 namespace chromeos {
     23 
     24 namespace {
     25 
     26 const int kDefaultDialogWidth = 500;
     27 const int kDefaultDialogHeight = 590;
     28 const int kMinDialogWidth = 100;
     29 const int kMinDialogHeight = 100;
     30 
     31 const char kNewChargerOrdered[] = "1";
     32 const char kNewChargerNotOrdered[] = "0";
     33 
     34 }  // namespace
     35 
     36 // static member variable.
     37 bool ChargerReplacementDialog::is_window_visible_ = false;
     38 gfx::NativeWindow ChargerReplacementDialog::current_window_ = NULL;
     39 
     40 ChargerReplacementDialog::ChargerReplacementDialog(
     41     gfx::NativeWindow parent_window)
     42     : parent_window_(parent_window),
     43       can_close_(false),
     44       charger_replacement_handler_(new ChargerReplacementHandler(this)) {
     45 }
     46 
     47 ChargerReplacementDialog::~ChargerReplacementDialog() {
     48   is_window_visible_ = false;
     49   current_window_ = NULL;
     50 }
     51 
     52 // static
     53 bool ChargerReplacementDialog::ShouldShowDialog() {
     54   if (is_window_visible_)
     55     return false;
     56 
     57   ChargerReplacementHandler::SpringChargerStatus charger_status =
     58       ChargerReplacementHandler::GetChargerStatusPref();
     59 
     60   return (charger_status == ChargerReplacementHandler::CHARGER_UNKNOWN ||
     61       charger_status ==
     62           ChargerReplacementHandler::CONFIRM_NEW_CHARGER_ORDERED_ONLINE ||
     63       charger_status ==
     64           ChargerReplacementHandler::CONFIRM_ORDER_NEW_CHARGER_BY_PHONE);
     65 }
     66 
     67 // static
     68 bool ChargerReplacementDialog::IsDialogVisible() {
     69   return is_window_visible_;
     70 }
     71 
     72 void ChargerReplacementDialog::SetFocusOnChargerDialogIfVisible() {
     73   if (is_window_visible_ && current_window_)
     74     current_window_->Focus();
     75 }
     76 
     77 void ChargerReplacementDialog::Show() {
     78   content::RecordAction(
     79         base::UserMetricsAction("ShowChargerReplacementDialog"));
     80 
     81   is_window_visible_ = true;
     82   // We show the dialog for the active user, so that the dialog will get
     83   // displayed immediately. The parent window is a system modal/lock container
     84   // and does not belong to any user.
     85   current_window_ = chrome::ShowWebDialog(
     86       parent_window_,
     87       ProfileManager::GetActiveUserProfile(),
     88       this);
     89   charger_replacement_handler_->set_charger_window(current_window_);
     90 }
     91 
     92 ui::ModalType ChargerReplacementDialog::GetDialogModalType() const {
     93   return ui::MODAL_TYPE_SYSTEM;
     94 }
     95 
     96 base::string16 ChargerReplacementDialog::GetDialogTitle() const {
     97   return l10n_util::GetStringUTF16(IDS_CHARGER_REPLACEMENT_DIALOG_TITLE);
     98 }
     99 
    100 GURL ChargerReplacementDialog::GetDialogContentURL() const {
    101   return GURL(chrome::kChromeUIChargerReplacementURL);
    102 }
    103 
    104 void ChargerReplacementDialog::GetWebUIMessageHandlers(
    105     std::vector<WebUIMessageHandler*>* handlers) const {
    106   handlers->push_back(charger_replacement_handler_);
    107 }
    108 
    109 void ChargerReplacementDialog::GetMinimumDialogSize(gfx::Size* size) const {
    110   size->SetSize(kMinDialogWidth, kMinDialogHeight);
    111 }
    112 
    113 void ChargerReplacementDialog::GetDialogSize(gfx::Size* size) const {
    114   size->SetSize(kDefaultDialogWidth, kDefaultDialogHeight);
    115 }
    116 
    117 std::string ChargerReplacementDialog::GetDialogArgs() const {
    118   ChargerReplacementHandler::SpringChargerStatus charger_status =
    119       ChargerReplacementHandler::GetChargerStatusPref();
    120   if (charger_status ==
    121           ChargerReplacementHandler::CONFIRM_NEW_CHARGER_ORDERED_ONLINE ||
    122       charger_status ==
    123           ChargerReplacementHandler::CONFIRM_ORDER_NEW_CHARGER_BY_PHONE) {
    124     return std::string(kNewChargerOrdered);
    125   } else {
    126     return std::string(kNewChargerNotOrdered);
    127   }
    128 }
    129 
    130 bool ChargerReplacementDialog::CanCloseDialog() const {
    131   return can_close_;
    132 }
    133 
    134 void ChargerReplacementDialog::OnDialogClosed(const std::string& json_retval) {
    135   delete this;
    136 }
    137 
    138 void ChargerReplacementDialog::OnCloseContents(WebContents* source,
    139                                                bool* out_close_dialog) {
    140   if (out_close_dialog)
    141     *out_close_dialog = true;
    142 }
    143 
    144 bool ChargerReplacementDialog::ShouldShowDialogTitle() const {
    145   return true;
    146 }
    147 
    148 bool ChargerReplacementDialog::HandleContextMenu(
    149     const content::ContextMenuParams& params) {
    150   // Disable context menu.
    151   return true;
    152 }
    153 
    154 }  // namespace chromeos
    155