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/html_page_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/screen_observer.h"
12#include "chrome/browser/profiles/profile_manager.h"
13#include "content/browser/renderer_host/render_view_host.h"
14#include "content/browser/site_instance.h"
15#include "content/browser/tab_contents/tab_contents.h"
16#include "googleurl/src/gurl.h"
17#include "views/events/event.h"
18#include "views/widget/widget_gtk.h"
19
20namespace chromeos {
21
22static const char kHTMLPageDoneUrl[] = "about:blank";
23
24///////////////////////////////////////////////////////////////////////////////
25// HTMLPageDomView
26TabContents* HTMLPageDomView::CreateTabContents(Profile* profile,
27                                                SiteInstance* instance) {
28  return new WizardWebPageViewTabContents(profile,
29                                          instance,
30                                          page_delegate_);
31}
32
33///////////////////////////////////////////////////////////////////////////////
34// HTMLPageView
35HTMLPageView::HTMLPageView()
36    : dom_view_(new HTMLPageDomView()) {
37}
38
39///////////////////////////////////////////////////////////////////////////////
40// HTMLPageScreen, public:
41HTMLPageScreen::HTMLPageScreen(WizardScreenDelegate* delegate,
42                               const std::string& url)
43    : ViewScreen<HTMLPageView>(delegate), url_(url) {
44}
45
46///////////////////////////////////////////////////////////////////////////////
47// HTMLPageScreen, ViewScreen implementation:
48void HTMLPageScreen::CreateView() {
49  ViewScreen<HTMLPageView>::CreateView();
50  view()->SetWebPageDelegate(this);
51}
52
53void HTMLPageScreen::Refresh() {
54  VLOG(1) << "HTMLPageScreen::Refresh(): " << url_;
55  StartTimeoutTimer();
56  GURL url(url_);
57  Profile* profile = ProfileManager::GetDefaultProfile();
58  view()->InitDOM(profile,
59                  SiteInstance::CreateSiteInstanceForURL(profile, url));
60  view()->SetTabContentsDelegate(this);
61  view()->LoadURL(url);
62}
63
64HTMLPageView* HTMLPageScreen::AllocateView() {
65  return new HTMLPageView();
66}
67
68///////////////////////////////////////////////////////////////////////////////
69// HTMLPageScreen, TabContentsDelegate implementation:
70void HTMLPageScreen::LoadingStateChanged(TabContents* source) {
71  std::string url = source->GetURL().spec();
72  if (url == kHTMLPageDoneUrl) {
73    source->Stop();
74    // TODO(dpolukhin): use special code for this case but now
75    // ACCOUNT_CREATE_BACK works as we would like, i.e. get to login page.
76    VLOG(1) << "HTMLPageScreen::LoadingStateChanged: " << url;
77    CloseScreen(ScreenObserver::ACCOUNT_CREATE_BACK);
78  }
79}
80
81void HTMLPageScreen::NavigationStateChanged(const TabContents* source,
82                                            unsigned changed_flags) {
83}
84
85void HTMLPageScreen::HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {
86  views::Widget* widget = view()->GetWidget();
87  if (widget && event.os_event && !event.skip_in_browser) {
88    views::KeyEvent views_event(reinterpret_cast<GdkEvent*>(event.os_event));
89    static_cast<views::WidgetGtk*>(widget)->HandleKeyboardEvent(views_event);
90  }
91}
92
93///////////////////////////////////////////////////////////////////////////////
94// HTMLPageScreen, WebPageDelegate implementation:
95void HTMLPageScreen::OnPageLoaded() {
96  StopTimeoutTimer();
97  // Enable input methods (e.g. Chinese, Japanese) so that users could input
98  // their first and last names.
99  if (g_browser_process) {
100    const std::string locale = g_browser_process->GetApplicationLocale();
101    input_method::EnableInputMethods(
102        locale, input_method::kAllInputMethods, "");
103  }
104  view()->ShowPageContent();
105}
106
107void HTMLPageScreen::OnPageLoadFailed(const std::string& url) {
108  VLOG(1) << "HTMLPageScreen::OnPageLoadFailed: " << url;
109  CloseScreen(ScreenObserver::CONNECTION_FAILED);
110}
111
112///////////////////////////////////////////////////////////////////////////////
113// HTMLPageScreen, WebPageScreen implementation:
114void HTMLPageScreen::OnNetworkTimeout() {
115  VLOG(1) << "HTMLPageScreen::OnNetworkTimeout";
116  // Just show what we have now. We shouldn't exit from the screen on timeout.
117  OnPageLoaded();
118}
119
120///////////////////////////////////////////////////////////////////////////////
121// HTMLPageScreen, private:
122void HTMLPageScreen::CloseScreen(ScreenObserver::ExitCodes code) {
123  StopTimeoutTimer();
124  // Disable input methods since they are not necessary to input username and
125  // password.
126  if (g_browser_process) {
127    const std::string locale = g_browser_process->GetApplicationLocale();
128    input_method::EnableInputMethods(
129        locale, input_method::kKeyboardLayoutsOnly, "");
130  }
131  delegate()->GetObserver(this)->OnExit(code);
132}
133
134}  // namespace chromeos
135