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:
EnrollmentDomView()34 EnrollmentDomView() {}
~EnrollmentDomView()35 virtual ~EnrollmentDomView() {}
36
37 protected:
38 // DomView imlementation:
CreateTabContents(Profile * profile,SiteInstance * instance)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:
OpenURLFromTab(TabContents * source,const GURL & url,const GURL & referrer,WindowOpenDisposition disposition,PageTransition::Type transition)49 virtual void OpenURLFromTab(TabContents* source,
50 const GURL& url, const GURL& referrer,
51 WindowOpenDisposition disposition,
52 PageTransition::Type transition) {}
NavigationStateChanged(const TabContents * source,unsigned changed_flags)53 virtual void NavigationStateChanged(const TabContents* source,
54 unsigned changed_flags) {}
AddNewContents(TabContents * source,TabContents * new_contents,WindowOpenDisposition disposition,const gfx::Rect & initial_pos,bool user_gesture)55 virtual void AddNewContents(TabContents* source,
56 TabContents* new_contents,
57 WindowOpenDisposition disposition,
58 const gfx::Rect& initial_pos,
59 bool user_gesture) {}
ActivateContents(TabContents * contents)60 virtual void ActivateContents(TabContents* contents) {}
DeactivateContents(TabContents * contents)61 virtual void DeactivateContents(TabContents* contents) {}
LoadingStateChanged(TabContents * source)62 virtual void LoadingStateChanged(TabContents* source) {}
CloseContents(TabContents * source)63 virtual void CloseContents(TabContents* source) {}
IsPopup(TabContents * source)64 virtual bool IsPopup(TabContents* source) { return false; }
UpdateTargetURL(TabContents * source,const GURL & url)65 virtual void UpdateTargetURL(TabContents* source, const GURL& url) {}
ShouldAddNavigationToHistory(const history::HistoryAddPageArgs & add_page_args,NavigationType::Type navigation_type)66 virtual bool ShouldAddNavigationToHistory(
67 const history::HistoryAddPageArgs& add_page_args,
68 NavigationType::Type navigation_type) {
69 return false;
70 }
MoveContents(TabContents * source,const gfx::Rect & pos)71 virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {}
HandleContextMenu(const ContextMenuParams & params)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
EnterpriseEnrollmentView(EnterpriseEnrollmentController * controller)82 EnterpriseEnrollmentView::EnterpriseEnrollmentView(
83 EnterpriseEnrollmentController* controller)
84 : controller_(controller),
85 editable_user_(true) {}
86
~EnterpriseEnrollmentView()87 EnterpriseEnrollmentView::~EnterpriseEnrollmentView() {}
88
Init()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
ShowConfirmationScreen()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
ShowAuthError(const GoogleServiceAuthError & error)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
ShowAccountError()130 void EnterpriseEnrollmentView::ShowAccountError() {
131 ShowError(IDS_ENTERPRISE_ENROLLMENT_ACCOUNT_ERROR);
132 }
133
ShowFatalAuthError()134 void EnterpriseEnrollmentView::ShowFatalAuthError() {
135 ShowError(IDS_ENTERPRISE_ENROLLMENT_FATAL_AUTH_ERROR);
136 }
137
ShowFatalEnrollmentError()138 void EnterpriseEnrollmentView::ShowFatalEnrollmentError() {
139 ShowError(IDS_ENTERPRISE_ENROLLMENT_FATAL_ENROLLMENT_ERROR);
140 }
141
ShowNetworkEnrollmentError()142 void EnterpriseEnrollmentView::ShowNetworkEnrollmentError() {
143 ShowError(IDS_ENTERPRISE_ENROLLMENT_NETWORK_ENROLLMENT_ERROR);
144 }
145
OnAuthSubmitted(const std::string & user,const std::string & password,const std::string & captcha,const std::string & access_code)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
OnAuthCancelled()153 void EnterpriseEnrollmentView::OnAuthCancelled() {
154 controller_->CancelEnrollment();
155 }
156
OnConfirmationClosed()157 void EnterpriseEnrollmentView::OnConfirmationClosed() {
158 controller_->CloseConfirmation();
159 }
160
GetInitialUser(std::string * user)161 bool EnterpriseEnrollmentView::GetInitialUser(std::string* user) {
162 return controller_->GetInitialUser(user);
163 }
164
UpdateGaiaLogin(const DictionaryValue & args)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
ShowError(int message_id)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
Layout()184 void EnterpriseEnrollmentView::Layout() {
185 enrollment_page_view_->SetBoundsRect(GetContentsBounds());
186 }
187
set_editable_user(bool editable)188 void EnterpriseEnrollmentView::set_editable_user(bool editable) {
189 editable_user_ = editable;
190 }
191
192 } // namespace chromeos
193