1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/login/signin/merge_session_load_page.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/chromeos/login/signin/oauth2_login_manager_factory.h"
18 #include "chrome/browser/extensions/extension_service.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/browser/ui/zoom/zoom_controller.h"
23 #include "chrome/common/extensions/extension_constants.h"
24 #include "chrome/common/url_constants.h"
25 #include "chrome/grit/browser_resources.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/interstitial_page.h"
29 #include "content/public/browser/notification_types.h"
30 #include "content/public/browser/web_contents.h"
31 #include "extensions/browser/extension_system.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/extension_icon_set.h"
34 #include "net/base/escape.h"
35 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/base/webui/jstemplate_builder.h"
39 using content::BrowserThread;
40 using content::InterstitialPage;
41 using content::WebContents;
45 // Delay time for showing interstitial page.
46 const int kShowDelayTimeMS = 1000;
48 // Maximum time for showing interstitial page.
49 const int kTotalWaitTimeMS = 10000;
55 MergeSessionLoadPage::MergeSessionLoadPage(
56 WebContents* web_contents,
58 const MergeSessionThrottle::CompletionCallback& callback)
59 : callback_(callback),
61 web_contents_(web_contents),
63 interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this);
66 MergeSessionLoadPage::~MergeSessionLoadPage() {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
70 void MergeSessionLoadPage::Show() {
71 OAuth2LoginManager* manager = GetOAuth2LoginManager();
72 if (manager && manager->ShouldBlockTabLoading()) {
73 manager->AddObserver(this);
74 interstitial_page_->Show();
76 interstitial_page_->Proceed();
80 std::string MergeSessionLoadPage::GetHTMLContents() {
81 base::DictionaryValue strings;
82 strings.SetString("title", web_contents_->GetTitle());
83 // Set the timeout to show the page.
84 strings.SetInteger("show_delay_time", kShowDelayTimeMS);
85 strings.SetInteger("total_wait_time", kTotalWaitTimeMS);
86 // TODO(zelidrag): Flip the message to IDS_MERGE_SESSION_LOAD_HEADLINE
88 strings.SetString("heading",
89 l10n_util::GetStringUTF16(IDS_TAB_LOADING_TITLE));
91 bool rtl = base::i18n::IsRTL();
92 strings.SetString("textdirection", rtl ? "rtl" : "ltr");
94 base::StringPiece html(
95 ResourceBundle::GetSharedInstance().GetRawDataResource(
96 IDR_MERGE_SESSION_LOAD_HTML));
97 return webui::GetI18nTemplateHtml(html, &strings);
100 void MergeSessionLoadPage::OverrideRendererPrefs(
101 content::RendererPreferences* prefs) {
102 Profile* profile = Profile::FromBrowserContext(
103 web_contents_->GetBrowserContext());
104 renderer_preferences_util::UpdateFromSystemSettings(
105 prefs, profile, web_contents_);
108 void MergeSessionLoadPage::OnProceed() {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
111 NotifyBlockingPageComplete();
114 void MergeSessionLoadPage::OnDontProceed() {
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
116 // Ignore if it's already proceeded.
119 NotifyBlockingPageComplete();
122 void MergeSessionLoadPage::CommandReceived(const std::string& cmd) {
123 std::string command(cmd);
124 // The Jasonified response has quotes, remove them.
125 if (command.length() > 1 && command[0] == '"')
126 command = command.substr(1, command.length() - 2);
128 if (command == "proceed") {
129 interstitial_page_->Proceed();
131 DVLOG(1) << "Unknown command:" << cmd;
135 void MergeSessionLoadPage::NotifyBlockingPageComplete() {
136 OAuth2LoginManager* manager = GetOAuth2LoginManager();
138 manager->RemoveObserver(this);
140 if (!callback_.is_null()) {
141 BrowserThread::PostTask(
142 BrowserThread::IO, FROM_HERE, callback_);
146 OAuth2LoginManager* MergeSessionLoadPage::GetOAuth2LoginManager() {
147 content::BrowserContext* browser_context = web_contents_->GetBrowserContext();
148 if (!browser_context)
151 Profile* profile = Profile::FromBrowserContext(browser_context);
155 return OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile);
158 void MergeSessionLoadPage::OnSessionRestoreStateChanged(
159 Profile* user_profile, OAuth2LoginManager::SessionRestoreState state) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162 OAuth2LoginManager* manager = GetOAuth2LoginManager();
163 DVLOG(1) << "Merge session should "
164 << (!manager->ShouldBlockTabLoading() ?
166 << " be blocking now, "
168 if (!manager->ShouldBlockTabLoading()) {
169 manager->RemoveObserver(this);
170 interstitial_page_->Proceed();
174 } // namespace chromeos