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/web_page_view.h"
      6 
      7 #include "base/callback.h"
      8 #include "base/logging.h"
      9 #include "base/string_util.h"
     10 #include "base/time.h"
     11 #include "base/utf_string_conversions.h"
     12 #include "chrome/browser/chromeos/login/helper.h"
     13 #include "chrome/browser/chromeos/login/rounded_rect_painter.h"
     14 #include "content/browser/child_process_security_policy.h"
     15 #include "content/browser/tab_contents/tab_contents.h"
     16 #include "content/browser/webui/web_ui.h"
     17 #include "content/common/bindings_policy.h"
     18 #include "grit/generated_resources.h"
     19 #include "grit/theme_resources.h"
     20 #include "ipc/ipc_message.h"
     21 #include "third_party/skia/include/core/SkColor.h"
     22 #include "ui/base/l10n/l10n_util.h"
     23 #include "ui/base/resource/resource_bundle.h"
     24 #include "ui/gfx/canvas.h"
     25 #include "views/background.h"
     26 #include "views/border.h"
     27 #include "views/controls/label.h"
     28 #include "views/controls/throbber.h"
     29 
     30 using base::TimeDelta;
     31 using views::Label;
     32 using views::View;
     33 using webkit_glue::FormData;
     34 
     35 namespace chromeos {
     36 
     37 namespace {
     38 
     39 // Spacing (vertical/horizontal) between controls.
     40 const int kSpacing = 10;
     41 
     42 // Time in ms after that waiting controls are shown on Start.
     43 const int kStartDelayMs = 500;
     44 
     45 // Time in ms after that waiting controls are hidden on Stop.
     46 const int kStopDelayMs = 500;
     47 
     48 }  // namespace
     49 
     50 ///////////////////////////////////////////////////////////////////////////////
     51 // WizardWebPageViewTabContents, public:
     52 
     53 WizardWebPageViewTabContents::WizardWebPageViewTabContents(
     54     Profile* profile,
     55     SiteInstance* site_instance,
     56     WebPageDelegate* page_delegate)
     57       : TabContents(profile, site_instance, MSG_ROUTING_NONE, NULL, NULL),
     58         page_delegate_(page_delegate) {
     59   }
     60 
     61 void WizardWebPageViewTabContents::DidFailProvisionalLoadWithError(
     62       RenderViewHost* render_view_host,
     63       bool is_main_frame,
     64       int error_code,
     65       const GURL& url,
     66       bool showing_repost_interstitial) {
     67   LOG(ERROR) << "Page load failed. URL = " << url << ", error: " << error_code;
     68   page_delegate_->OnPageLoadFailed(url.spec());
     69 }
     70 
     71 void WizardWebPageViewTabContents::DidDisplayInsecureContent() {
     72   LOG(ERROR) << "Page load failed: did display insecure content";
     73   page_delegate_->OnPageLoadFailed("Displayed insecure content");
     74 }
     75 
     76 void WizardWebPageViewTabContents::DidRunInsecureContent(
     77     const std::string& security_origin) {
     78   LOG(ERROR) << "Page load failed: did run insecure content";
     79   page_delegate_->OnPageLoadFailed(security_origin);
     80 }
     81 
     82 void WizardWebPageViewTabContents::DocumentLoadedInFrame(
     83     long long /*frame_id*/) {
     84   page_delegate_->OnPageLoaded();
     85 }
     86 
     87 void WizardWebPageViewTabContents::DidFinishLoad(
     88     long long /*frame_id*/) {
     89 }
     90 
     91 void WizardWebPageViewTabContents::OnContentBlocked(ContentSettingsType type) {
     92   LOG(ERROR) << "Page load failed: content blocked. Type: " << type;
     93   page_delegate_->OnPageLoadFailed("");
     94 }
     95 
     96 ///////////////////////////////////////////////////////////////////////////////
     97 // WebPageDomView, public:
     98 
     99 void WebPageDomView::SetTabContentsDelegate(
    100     TabContentsDelegate* delegate) {
    101   tab_contents_->set_delegate(delegate);
    102 }
    103 
    104 ///////////////////////////////////////////////////////////////////////////////
    105 // WebPageView, public:
    106 
    107 void WebPageView::Init() {
    108   views::Painter* painter = CreateWizardPainter(
    109       &BorderDefinition::kScreenBorder);
    110   set_background(
    111       views::Background::CreateBackgroundPainter(true, painter));
    112   set_border(CreateWizardBorder(&BorderDefinition::kScreenBorder));
    113   dom_view()->SetVisible(false);
    114   AddChildView(dom_view());
    115 
    116   throbber_ = CreateDefaultThrobber();
    117   AddChildView(throbber_);
    118 
    119   connecting_label_ = new views::Label();
    120   connecting_label_->SetText(
    121       UTF16ToWide(l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING)));
    122   ResourceBundle& rb = ResourceBundle::GetSharedInstance();
    123   connecting_label_->SetFont(rb.GetFont(ResourceBundle::MediumFont));
    124   connecting_label_->SetVisible(false);
    125   AddChildView(connecting_label_ );
    126 
    127   start_timer_.Start(TimeDelta::FromMilliseconds(kStartDelayMs),
    128                      this,
    129                      &WebPageView::ShowWaitingControls);
    130 }
    131 
    132 void WebPageView::InitDOM(Profile* profile,
    133                           SiteInstance* site_instance) {
    134   dom_view()->Init(profile, site_instance);
    135 }
    136 
    137 void WebPageView::LoadURL(const GURL& url) {
    138   dom_view()->LoadURL(url);
    139 }
    140 
    141 void WebPageView::SetTabContentsDelegate(
    142     TabContentsDelegate* delegate) {
    143   dom_view()->SetTabContentsDelegate(delegate);
    144 }
    145 
    146 void WebPageView::SetWebPageDelegate(WebPageDelegate* delegate) {
    147   dom_view()->set_web_page_delegate(delegate);
    148 }
    149 
    150 void WebPageView::ShowPageContent() {
    151   // TODO(nkostylev): Show throbber as an overlay until page has been rendered.
    152   start_timer_.Stop();
    153   if (!stop_timer_.IsRunning()) {
    154     stop_timer_.Start(TimeDelta::FromMilliseconds(kStopDelayMs),
    155                       this,
    156                       &WebPageView::ShowRenderedPage);
    157   }
    158 }
    159 
    160 ///////////////////////////////////////////////////////////////////////////////
    161 // WebPageView, private:
    162 
    163 void WebPageView::ShowRenderedPage() {
    164   throbber_->Stop();
    165   connecting_label_->SetVisible(false);
    166   dom_view()->SetVisible(true);
    167 }
    168 
    169 void WebPageView::ShowWaitingControls() {
    170   throbber_->Start();
    171   connecting_label_->SetVisible(true);
    172 }
    173 
    174 ///////////////////////////////////////////////////////////////////////////////
    175 // WebPageView, views::View implementation:
    176 
    177 void WebPageView::Layout() {
    178   dom_view()->SetBoundsRect(GetContentsBounds());
    179   int y = height() / 2  - throbber_->GetPreferredSize().height() / 2;
    180   throbber_->SetBounds(
    181       width() / 2 - throbber_->GetPreferredSize().width() / 2,
    182       y,
    183       throbber_->GetPreferredSize().width(),
    184       throbber_->GetPreferredSize().height());
    185   connecting_label_->SetBounds(
    186       width() / 2 - connecting_label_->GetPreferredSize().width() / 2,
    187       y + throbber_->GetPreferredSize().height() + kSpacing,
    188       connecting_label_->GetPreferredSize().width(),
    189       connecting_label_->GetPreferredSize().height());
    190 }
    191 
    192 }  // namespace chromeos
    193