Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2011 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/login/enterprise_enrollment_view.h"
      6 
      7 #include "base/json/json_writer.h"
      8 #include "base/values.h"
      9 #include "chrome/browser/chromeos/login/enterprise_enrollment_screen.h"
     10 #include "chrome/browser/chromeos/login/helper.h"
     11 #include "chrome/browser/chromeos/login/rounded_rect_painter.h"
     12 #include "chrome/browser/profiles/profile_manager.h"
     13 #include "chrome/browser/ui/webui/chromeos/enterprise_enrollment_ui.h"
     14 #include "chrome/common/url_constants.h"
     15 #include "content/browser/renderer_host/render_view_host.h"
     16 #include "content/browser/site_instance.h"
     17 #include "content/browser/tab_contents/tab_contents_delegate.h"
     18 #include "grit/generated_resources.h"
     19 #include "ui/base/l10n/l10n_util.h"
     20 #include "views/border.h"
     21 #include "views/layout/layout_constants.h"
     22 
     23 namespace chromeos {
     24 
     25 namespace {
     26 
     27 // Layout constants.
     28 const int kBorderSize = 30;
     29 
     30 // Renders the registration page.
     31 class EnrollmentDomView : public WebPageDomView,
     32                           public TabContentsDelegate {
     33  public:
     34   EnrollmentDomView() {}
     35   virtual ~EnrollmentDomView() {}
     36 
     37  protected:
     38   // DomView imlementation:
     39   virtual TabContents* CreateTabContents(Profile* profile,
     40                                          SiteInstance* instance) {
     41     TabContents* contents = new WizardWebPageViewTabContents(profile,
     42                                                              instance,
     43                                                              page_delegate_);
     44     contents->set_delegate(this);
     45     return contents;
     46   }
     47 
     48   // TabContentsDelegate implementation:
     49   virtual void OpenURLFromTab(TabContents* source,
     50                               const GURL& url, const GURL& referrer,
     51                               WindowOpenDisposition disposition,
     52                               PageTransition::Type transition) {}
     53   virtual void NavigationStateChanged(const TabContents* source,
     54                                       unsigned changed_flags) {}
     55   virtual void AddNewContents(TabContents* source,
     56                               TabContents* new_contents,
     57                               WindowOpenDisposition disposition,
     58                               const gfx::Rect& initial_pos,
     59                               bool user_gesture) {}
     60   virtual void ActivateContents(TabContents* contents) {}
     61   virtual void DeactivateContents(TabContents* contents) {}
     62   virtual void LoadingStateChanged(TabContents* source) {}
     63   virtual void CloseContents(TabContents* source) {}
     64   virtual bool IsPopup(TabContents* source) { return false; }
     65   virtual void UpdateTargetURL(TabContents* source, const GURL& url) {}
     66   virtual bool ShouldAddNavigationToHistory(
     67       const history::HistoryAddPageArgs& add_page_args,
     68       NavigationType::Type navigation_type) {
     69     return false;
     70   }
     71   virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {}
     72   virtual bool HandleContextMenu(const ContextMenuParams& params) {
     73     return true;
     74   }
     75 
     76  private:
     77   DISALLOW_COPY_AND_ASSIGN(EnrollmentDomView);
     78 };
     79 
     80 }  // namespace
     81 
     82 EnterpriseEnrollmentView::EnterpriseEnrollmentView(
     83     EnterpriseEnrollmentController* controller)
     84     : controller_(controller),
     85       editable_user_(true) {}
     86 
     87 EnterpriseEnrollmentView::~EnterpriseEnrollmentView() {}
     88 
     89 void EnterpriseEnrollmentView::Init() {
     90   // Use rounded rect background.
     91   views::Painter* painter =
     92       CreateWizardPainter(&BorderDefinition::kScreenBorder);
     93   set_background(views::Background::CreateBackgroundPainter(true, painter));
     94 
     95   // Create the view that hosts the enrollment page.
     96   enrollment_page_view_ = new EnrollmentDomView();
     97   enrollment_page_view_->set_border(
     98       views::Border::CreateEmptyBorder(kBorderSize, kBorderSize,
     99                                        kBorderSize, kBorderSize));
    100 
    101   AddChildView(enrollment_page_view_);
    102 
    103   // Load the enrollment page.
    104   Profile* profile = ProfileManager::GetDefaultProfile();
    105   GURL url(chrome::kChromeUIEnterpriseEnrollmentURL);
    106   enrollment_page_view_->Init(
    107       profile, SiteInstance::CreateSiteInstanceForURL(profile, url));
    108   EnterpriseEnrollmentUI::SetController(enrollment_page_view_->tab_contents(),
    109                                         this);
    110   enrollment_page_view_->LoadURL(url);
    111 }
    112 
    113 void EnterpriseEnrollmentView::ShowConfirmationScreen() {
    114   RenderViewHost* render_view_host =
    115       enrollment_page_view_->tab_contents()->render_view_host();
    116   render_view_host->ExecuteJavascriptInWebFrame(
    117       string16(),
    118       UTF8ToUTF16("enterpriseEnrollment.showScreen('confirmation-screen');"));
    119 }
    120 
    121 void EnterpriseEnrollmentView::ShowAuthError(
    122     const GoogleServiceAuthError& error) {
    123   DictionaryValue args;
    124   args.SetInteger("error", error.state());
    125   args.SetBoolean("editable_user", editable_user_);
    126   args.SetString("captchaUrl", error.captcha().image_url.spec());
    127   UpdateGaiaLogin(args);
    128 }
    129 
    130 void EnterpriseEnrollmentView::ShowAccountError() {
    131   ShowError(IDS_ENTERPRISE_ENROLLMENT_ACCOUNT_ERROR);
    132 }
    133 
    134 void EnterpriseEnrollmentView::ShowFatalAuthError() {
    135   ShowError(IDS_ENTERPRISE_ENROLLMENT_FATAL_AUTH_ERROR);
    136 }
    137 
    138 void EnterpriseEnrollmentView::ShowFatalEnrollmentError() {
    139   ShowError(IDS_ENTERPRISE_ENROLLMENT_FATAL_ENROLLMENT_ERROR);
    140 }
    141 
    142 void EnterpriseEnrollmentView::ShowNetworkEnrollmentError() {
    143   ShowError(IDS_ENTERPRISE_ENROLLMENT_NETWORK_ENROLLMENT_ERROR);
    144 }
    145 
    146 void EnterpriseEnrollmentView::OnAuthSubmitted(const std::string& user,
    147                                                const std::string& password,
    148                                                const std::string& captcha,
    149                                                const std::string& access_code) {
    150   controller_->Authenticate(user, password, captcha, access_code);
    151 }
    152 
    153 void EnterpriseEnrollmentView::OnAuthCancelled() {
    154   controller_->CancelEnrollment();
    155 }
    156 
    157 void EnterpriseEnrollmentView::OnConfirmationClosed() {
    158   controller_->CloseConfirmation();
    159 }
    160 
    161 bool EnterpriseEnrollmentView::GetInitialUser(std::string* user) {
    162   return controller_->GetInitialUser(user);
    163 }
    164 
    165 void EnterpriseEnrollmentView::UpdateGaiaLogin(const DictionaryValue& args) {
    166   std::string json;
    167   base::JSONWriter::Write(&args, false, &json);
    168 
    169   RenderViewHost* render_view_host =
    170       enrollment_page_view_->tab_contents()->render_view_host();
    171   render_view_host->ExecuteJavascriptInWebFrame(
    172       ASCIIToUTF16("//iframe[@id='gaialogin']"),
    173       UTF8ToUTF16("showGaiaLogin(" + json + ");"));
    174 }
    175 
    176 void EnterpriseEnrollmentView::ShowError(int message_id) {
    177   DictionaryValue args;
    178   args.SetInteger("error", GoogleServiceAuthError::NONE);
    179   args.SetBoolean("editable_user", editable_user_);
    180   args.SetString("error_message", l10n_util::GetStringUTF16(message_id));
    181   UpdateGaiaLogin(args);
    182 }
    183 
    184 void EnterpriseEnrollmentView::Layout() {
    185   enrollment_page_view_->SetBoundsRect(GetContentsBounds());
    186 }
    187 
    188 void EnterpriseEnrollmentView::set_editable_user(bool editable) {
    189   editable_user_ = editable;
    190 }
    191 
    192 }  // namespace chromeos
    193