Home | History | Annotate | Download | only in login
      1 // Copyright 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/ui/webui/chromeos/login/kiosk_enable_screen_handler.h"
      6 
      7 #include <string>
      8 
      9 #include "chrome/browser/browser_process.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/common/chrome_switches.h"
     12 #include "content/public/browser/notification_details.h"
     13 #include "content/public/browser/notification_service.h"
     14 #include "grit/browser_resources.h"
     15 #include "grit/generated_resources.h"
     16 
     17 namespace {
     18 
     19 const char kJsScreenPath[] = "login.KioskEnableScreen";
     20 
     21 // Reset screen id.
     22 const char kKioskEnableScreen[] = "kiosk-enable";
     23 
     24 }  // namespace
     25 
     26 namespace chromeos {
     27 
     28 KioskEnableScreenHandler::KioskEnableScreenHandler()
     29     : BaseScreenHandler(kJsScreenPath),
     30       delegate_(NULL),
     31       show_on_init_(false),
     32       is_configurable_(false),
     33       weak_ptr_factory_(this) {
     34 }
     35 
     36 KioskEnableScreenHandler::~KioskEnableScreenHandler() {
     37   if (delegate_)
     38     delegate_->OnActorDestroyed(this);
     39 }
     40 
     41 void KioskEnableScreenHandler::Show() {
     42   if (!page_is_ready()) {
     43     show_on_init_ = true;
     44     return;
     45   }
     46 
     47   KioskAppManager::Get()->GetConsumerKioskModeStatus(
     48       base::Bind(&KioskEnableScreenHandler::OnGetConsumerKioskModeStatus,
     49                  weak_ptr_factory_.GetWeakPtr()));
     50 }
     51 
     52 void KioskEnableScreenHandler::OnGetConsumerKioskModeStatus(
     53     KioskAppManager::ConsumerKioskModeStatus status) {
     54   is_configurable_ =
     55       (status == KioskAppManager::CONSUMER_KIOSK_MODE_CONFIGURABLE);
     56   if (!is_configurable_) {
     57     LOG(WARNING) << "Consumer kiosk feature is not configurable anymore!";
     58     return;
     59   }
     60 
     61   ShowScreen(kKioskEnableScreen, NULL);
     62 
     63   content::NotificationService::current()->Notify(
     64       chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_VISIBLE,
     65       content::NotificationService::AllSources(),
     66       content::NotificationService::NoDetails());
     67 }
     68 
     69 void KioskEnableScreenHandler::SetDelegate(Delegate* delegate) {
     70   delegate_ = delegate;
     71   if (page_is_ready())
     72     Initialize();
     73 }
     74 
     75 void KioskEnableScreenHandler::DeclareLocalizedValues(
     76     LocalizedValuesBuilder* builder) {
     77   builder->Add("kioskEnableTitle", IDS_KIOSK_ENABLE_SCREEN_WARNING);
     78   builder->Add("kioskEnableWarningText",
     79                IDS_KIOSK_ENABLE_SCREEN_WARNING);
     80   builder->Add("kioskEnableWarningDetails",
     81                IDS_KIOSK_ENABLE_SCREEN_WARNING_DETAILS);
     82   builder->Add("kioskEnableButton", IDS_KIOSK_ENABLE_SCREEN_ENABLE_BUTTON);
     83   builder->Add("kioskCancelButton", IDS_CANCEL);
     84   builder->Add("kioskOKButton", IDS_OK);
     85   builder->Add("kioskEnableSuccessMsg", IDS_KIOSK_ENABLE_SCREEN_SUCCESS);
     86   builder->Add("kioskEnableErrorMsg", IDS_KIOSK_ENABLE_SCREEN_ERROR);
     87 }
     88 
     89 void KioskEnableScreenHandler::Initialize() {
     90   if (!page_is_ready() || !delegate_)
     91     return;
     92 
     93   if (show_on_init_) {
     94     Show();
     95     show_on_init_ = false;
     96   }
     97 }
     98 
     99 void KioskEnableScreenHandler::RegisterMessages() {
    100   AddCallback("kioskOnClose", &KioskEnableScreenHandler::HandleOnClose);
    101   AddCallback("kioskOnEnable", &KioskEnableScreenHandler::HandleOnEnable);
    102 }
    103 
    104 void KioskEnableScreenHandler::HandleOnClose() {
    105   if (delegate_)
    106     delegate_->OnExit();
    107 
    108   content::NotificationService::current()->Notify(
    109       chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_COMPLETED,
    110       content::NotificationService::AllSources(),
    111       content::NotificationService::NoDetails());
    112 }
    113 
    114 void KioskEnableScreenHandler::HandleOnEnable() {
    115   if (!is_configurable_) {
    116     NOTREACHED();
    117     if (delegate_)
    118       delegate_->OnExit();
    119 
    120     content::NotificationService::current()->Notify(
    121         chrome::NOTIFICATION_KIOSK_ENABLE_WARNING_COMPLETED,
    122         content::NotificationService::AllSources(),
    123         content::NotificationService::NoDetails());
    124     return;
    125   }
    126 
    127   KioskAppManager::Get()->EnableConsumerModeKiosk(
    128       base::Bind(&KioskEnableScreenHandler::OnEnableConsumerModeKiosk,
    129                  weak_ptr_factory_.GetWeakPtr()));
    130 }
    131 
    132 void KioskEnableScreenHandler::OnEnableConsumerModeKiosk(bool success) {
    133   if (!success)
    134     LOG(WARNING) << "Consumer kiosk mode can't be enabled!";
    135 
    136   CallJS("onCompleted", success);
    137   if (success) {
    138     content::NotificationService::current()->Notify(
    139         chrome::NOTIFICATION_KIOSK_ENABLED,
    140         content::NotificationService::AllSources(),
    141         content::NotificationService::NoDetails());
    142   }
    143 }
    144 
    145 }  // namespace chromeos
    146