Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2013 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/merge_session_load_page.h"
      6 
      7 #include "ash/shell.h"
      8 #include "ash/shell_delegate.h"
      9 #include "ash/system/tray/system_tray_delegate.h"
     10 #include "base/i18n/rtl.h"
     11 #include "base/metrics/histogram.h"
     12 #include "base/strings/string_piece.h"
     13 #include "base/strings/stringprintf.h"
     14 #include "base/strings/utf_string_conversions.h"
     15 #include "base/values.h"
     16 #include "chrome/browser/chrome_notification_types.h"
     17 #include "chrome/browser/extensions/extension_service.h"
     18 #include "chrome/browser/extensions/extension_system.h"
     19 #include "chrome/browser/profiles/profile.h"
     20 #include "chrome/browser/renderer_preferences_util.h"
     21 #include "chrome/browser/tab_contents/tab_util.h"
     22 #include "chrome/common/extensions/extension.h"
     23 #include "chrome/common/extensions/extension_constants.h"
     24 #include "chrome/common/extensions/extension_icon_set.h"
     25 #include "chrome/common/url_constants.h"
     26 #include "content/public/browser/browser_thread.h"
     27 #include "content/public/browser/interstitial_page.h"
     28 #include "content/public/browser/notification_types.h"
     29 #include "content/public/browser/web_contents.h"
     30 #include "grit/browser_resources.h"
     31 #include "grit/generated_resources.h"
     32 #include "net/base/escape.h"
     33 #include "ui/base/l10n/l10n_util.h"
     34 #include "ui/base/resource/resource_bundle.h"
     35 #include "ui/webui/jstemplate_builder.h"
     36 
     37 using content::BrowserThread;
     38 using content::InterstitialPage;
     39 using content::WebContents;
     40 
     41 namespace {
     42 
     43 // Delay time for showing interstitial page.
     44 const int kShowDelayTimeMS = 1000;
     45 
     46 // Maximum time for showing interstitial page.
     47 const int kTotalWaitTimeMS = 10000;
     48 
     49 }  // namespace
     50 
     51 namespace chromeos {
     52 
     53 MergeSessionLoadPage::MergeSessionLoadPage(WebContents* web_contents,
     54                                            const GURL& url,
     55                                            const CompletionCallback& callback)
     56     : callback_(callback),
     57       proceeded_(false),
     58       web_contents_(web_contents),
     59       url_(url) {
     60   UserManager::Get()->AddObserver(this);
     61   interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this);
     62 }
     63 
     64 MergeSessionLoadPage::~MergeSessionLoadPage() {
     65   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     66   UserManager::Get()->RemoveObserver(this);
     67 }
     68 
     69 void MergeSessionLoadPage::Show() {
     70   interstitial_page_->Show();
     71 }
     72 
     73 std::string MergeSessionLoadPage::GetHTMLContents() {
     74   DictionaryValue strings;
     75   strings.SetString("title", web_contents_->GetTitle());
     76   // Set the timeout to show the page.
     77   strings.SetInteger("show_delay_time", kShowDelayTimeMS);
     78   strings.SetInteger("total_wait_time", kTotalWaitTimeMS);
     79   // TODO(zelidrag): Flip the message to IDS_MERGE_SESSION_LOAD_HEADLINE
     80   // after merge.
     81   strings.SetString("heading",
     82                     l10n_util::GetStringUTF16(IDS_TAB_LOADING_TITLE));
     83 
     84   bool rtl = base::i18n::IsRTL();
     85   strings.SetString("textdirection", rtl ? "rtl" : "ltr");
     86 
     87   base::StringPiece html(
     88       ResourceBundle::GetSharedInstance().GetRawDataResource(
     89           IDR_MERGE_SESSION_LOAD_HTML));
     90   return webui::GetI18nTemplateHtml(html, &strings);
     91 }
     92 
     93 void MergeSessionLoadPage::OverrideRendererPrefs(
     94       content::RendererPreferences* prefs) {
     95   Profile* profile = Profile::FromBrowserContext(
     96       web_contents_->GetBrowserContext());
     97   renderer_preferences_util::UpdateFromSystemSettings(prefs, profile);
     98 }
     99 
    100 void MergeSessionLoadPage::OnProceed() {
    101   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    102   proceeded_ = true;
    103   NotifyBlockingPageComplete();
    104 }
    105 
    106 void MergeSessionLoadPage::OnDontProceed() {
    107   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    108   // Ignore if it's already proceeded.
    109   if (proceeded_)
    110     return;
    111   NotifyBlockingPageComplete();
    112 }
    113 
    114 void MergeSessionLoadPage::CommandReceived(const std::string& cmd) {
    115   std::string command(cmd);
    116   // The Jasonified response has quotes, remove them.
    117   if (command.length() > 1 && command[0] == '"')
    118     command = command.substr(1, command.length() - 2);
    119 
    120   if (command == "proceed") {
    121     interstitial_page_->Proceed();
    122   } else {
    123     DVLOG(1) << "Unknown command:" << cmd;
    124   }
    125 }
    126 
    127 void MergeSessionLoadPage::NotifyBlockingPageComplete() {
    128   if (!callback_.is_null()) {
    129     BrowserThread::PostTask(
    130         BrowserThread::IO, FROM_HERE, callback_);
    131   }
    132 }
    133 
    134 void MergeSessionLoadPage::MergeSessionStateChanged(
    135     UserManager::MergeSessionState state) {
    136   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    137   DVLOG(1) << "Merge session is "
    138            << (state !=  UserManager:: MERGE_STATUS_IN_PROCESS ?
    139                   " NOT " : "")
    140            << " in progress, "
    141            << state;
    142   if (state !=  UserManager:: MERGE_STATUS_IN_PROCESS) {
    143     UserManager::Get()->RemoveObserver(this);
    144     interstitial_page_->Proceed();
    145   }
    146 }
    147 
    148 }  // namespace chromeos
    149