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