account_screen.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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/account_screen.h"
6
7#include "base/string_util.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/chromeos/input_method/input_method_util.h"
10#include "chrome/browser/chromeos/login/account_creation_view.h"
11#include "chrome/browser/chromeos/login/screen_observer.h"
12#include "chrome/browser/profile_manager.h"
13#include "chrome/browser/renderer_host/render_view_host.h"
14#include "chrome/browser/renderer_host/site_instance.h"
15#include "chrome/browser/tab_contents/tab_contents.h"
16#include "googleurl/src/gurl.h"
17
18namespace chromeos {
19
20namespace {
21
22const char kCreateAccountPageUrl[] =
23    "https://www.google.com/accounts/NewAccount?service=mail&hl=en";
24
25const char kCreateAccountDoneUrl[] =
26    "http://mail.google.com/mail/help/intro.html";
27
28const char kCreateAccountBackUrl[] =
29    "about:blank";
30
31const char kCreateAccountCSS[] =
32    "body > table, div.body > h3, div.body > table, a, "
33    "#cookieWarning1, #cookieWarning2 {\n"
34    " display: none !important;\n"
35    "}\n"
36    "tbody tr:nth-child(7), tbody tr:nth-child(8), tbody tr:nth-child(9),"
37    "tbody tr:nth-child(13), tbody tr:nth-child(16), tbody tr:nth-child(17),"
38    "tbody tr:nth-child(18) {\n"
39    " display: none !important;\n"
40    "}\n"
41    "body {\n"
42    " padding: 0;\n"
43    "}\n";
44
45const char kCreateAccountJS[] =
46  "try {\n"
47  " var smhck = document.getElementById('smhck');\n"
48  " smhck.checked = false;\n"
49  " smhck.value = 0;\n"
50  " var tables = document.getElementsByTagName('table');\n"
51  " for (var i = 0; i < tables.length; i++) {\n"
52  "   if (tables[i].bgColor == '#cbdced') tables[i].cellPadding = 0;\n"
53  " }\n"
54  " var submitbtn = document.getElementById('submitbutton');\n"
55  " submitbtn.value = 'Create Account';\n"
56  " submitbtn.parentNode.parentNode.firstElementChild.innerHTML ="
57  "   \"<input type='button' style='width:8em' value='<< Back'"
58  "      onclick='window.location=\\\"about:blank\\\";'/>\";\n"
59  "} catch(err) {\n"
60  "}\n";
61
62}  // namespace
63
64///////////////////////////////////////////////////////////////////////////////
65// AccountScreen, public:
66AccountScreen::AccountScreen(WizardScreenDelegate* delegate)
67    : ViewScreen<AccountCreationView>(delegate) {
68  if (!new_account_page_url_.get())
69    new_account_page_url_.reset(new GURL(kCreateAccountPageUrl));
70}
71
72AccountScreen::~AccountScreen() {
73}
74
75// static
76void AccountScreen::set_new_account_page_url(const GURL& url) {
77  new_account_page_url_.reset(new GURL(url));
78}
79
80// static
81scoped_ptr<GURL> AccountScreen::new_account_page_url_;
82// static
83bool AccountScreen::check_for_https_ = true;
84
85///////////////////////////////////////////////////////////////////////////////
86// AccountScreen, ViewScreen implementation:
87void AccountScreen::CreateView() {
88  ViewScreen<AccountCreationView>::CreateView();
89  view()->SetWebPageDelegate(this);
90  view()->SetAccountCreationViewDelegate(this);
91}
92
93void AccountScreen::Refresh() {
94  StartTimeoutTimer();
95  GURL url(*new_account_page_url_);
96  Profile* profile = ProfileManager::GetDefaultProfile();
97  view()->InitDOM(profile,
98                  SiteInstance::CreateSiteInstanceForURL(profile, url));
99  view()->SetTabContentsDelegate(this);
100  view()->LoadURL(url);
101}
102
103AccountCreationView* AccountScreen::AllocateView() {
104  return new AccountCreationView();
105}
106
107///////////////////////////////////////////////////////////////////////////////
108// AccountScreen, TabContentsDelegate implementation:
109void AccountScreen::LoadingStateChanged(TabContents* source) {
110  std::string url = source->GetURL().spec();
111  if (url == kCreateAccountDoneUrl) {
112    source->Stop();
113    CloseScreen(ScreenObserver::ACCOUNT_CREATED);
114  } else if (url == kCreateAccountBackUrl) {
115    CloseScreen(ScreenObserver::ACCOUNT_CREATE_BACK);
116  } else if (check_for_https_ && !source->GetURL().SchemeIsSecure()) {
117    CloseScreen(ScreenObserver::CONNECTION_FAILED);
118  }
119}
120
121void AccountScreen::NavigationStateChanged(const TabContents* source,
122                                           unsigned changed_flags) {
123  if (source->render_view_host()) {
124    source->render_view_host()->InsertCSSInWebFrame(
125        L"", kCreateAccountCSS, "");
126    source->render_view_host()->ExecuteJavascriptInWebFrame(
127        L"", ASCIIToWide(kCreateAccountJS));
128  }
129}
130
131///////////////////////////////////////////////////////////////////////////////
132// AccountScreen, WebPageDelegate implementation:
133void AccountScreen::OnPageLoaded() {
134  StopTimeoutTimer();
135  // Enable input methods (e.g. Chinese, Japanese) so that users could input
136  // their first and last names.
137  if (g_browser_process) {
138    const std::string locale = g_browser_process->GetApplicationLocale();
139    input_method::EnableInputMethods(
140        locale, input_method::kKeyboardLayoutsOnly, "");
141    // TODO(yusukes,suzhe): Change the 2nd argument to kAllInputMethods when
142    // crosbug.com/2670 is fixed.
143  }
144  view()->ShowPageContent();
145}
146
147void AccountScreen::OnPageLoadFailed(const std::string& url) {
148  CloseScreen(ScreenObserver::CONNECTION_FAILED);
149}
150
151///////////////////////////////////////////////////////////////////////////////
152// AccountScreen, AccountCreationViewDelegate implementation:
153void AccountScreen::OnUserCreated(const std::string& username,
154                                  const std::string& password) {
155  delegate()->GetObserver(this)->OnSetUserNamePassword(username, password);
156}
157
158///////////////////////////////////////////////////////////////////////////////
159// AccountScreen, private:
160void AccountScreen::CloseScreen(ScreenObserver::ExitCodes code) {
161  StopTimeoutTimer();
162  // Disable input methods since they are not necessary to input username and
163  // password.
164  if (g_browser_process) {
165    const std::string locale = g_browser_process->GetApplicationLocale();
166    input_method::EnableInputMethods(
167        locale, input_method::kKeyboardLayoutsOnly, "");
168  }
169  delegate()->GetObserver(this)->OnExit(code);
170}
171
172}  // namespace chromeos
173