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. 4 5 #include "chrome/browser/chromeos/login/signin/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/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/common/extensions/extension_constants.h" 23 #include "chrome/common/url_constants.h" 24 #include "chrome/grit/browser_resources.h" 25 #include "chrome/grit/generated_resources.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 "extensions/browser/extension_system.h" 31 #include "extensions/common/extension.h" 32 #include "extensions/common/extension_icon_set.h" 33 #include "net/base/escape.h" 34 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/resource/resource_bundle.h" 36 #include "ui/base/webui/jstemplate_builder.h" 37 38 using content::BrowserThread; 39 using content::InterstitialPage; 40 using content::WebContents; 41 42 namespace { 43 44 // Delay time for showing interstitial page. 45 const int kShowDelayTimeMS = 1000; 46 47 // Maximum time for showing interstitial page. 48 const int kTotalWaitTimeMS = 10000; 49 50 } // namespace 51 52 namespace chromeos { 53 54 MergeSessionLoadPage::MergeSessionLoadPage( 55 WebContents* web_contents, 56 const GURL& url, 57 const MergeSessionThrottle::CompletionCallback& callback) 58 : callback_(callback), 59 proceeded_(false), 60 web_contents_(web_contents), 61 url_(url) { 62 interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this); 63 } 64 65 MergeSessionLoadPage::~MergeSessionLoadPage() { 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 67 } 68 69 void MergeSessionLoadPage::Show() { 70 OAuth2LoginManager* manager = GetOAuth2LoginManager(); 71 if (manager && manager->ShouldBlockTabLoading()) { 72 manager->AddObserver(this); 73 interstitial_page_->Show(); 74 } else { 75 interstitial_page_->Proceed(); 76 } 77 } 78 79 std::string MergeSessionLoadPage::GetHTMLContents() { 80 base::DictionaryValue strings; 81 strings.SetString("title", web_contents_->GetTitle()); 82 // Set the timeout to show the page. 83 strings.SetInteger("show_delay_time", kShowDelayTimeMS); 84 strings.SetInteger("total_wait_time", kTotalWaitTimeMS); 85 // TODO(zelidrag): Flip the message to IDS_MERGE_SESSION_LOAD_HEADLINE 86 // after merge. 87 strings.SetString("heading", 88 l10n_util::GetStringUTF16(IDS_TAB_LOADING_TITLE)); 89 90 bool rtl = base::i18n::IsRTL(); 91 strings.SetString("textdirection", rtl ? "rtl" : "ltr"); 92 93 webui::UseVersion2 version2; 94 base::StringPiece html( 95 ResourceBundle::GetSharedInstance().GetRawDataResource( 96 IDR_MERGE_SESSION_LOAD_HTML)); 97 return webui::GetI18nTemplateHtml(html, &strings); 98 } 99 100 void MergeSessionLoadPage::OverrideRendererPrefs( 101 content::RendererPreferences* prefs) { 102 Profile* profile = Profile::FromBrowserContext( 103 web_contents_->GetBrowserContext()); 104 renderer_preferences_util::UpdateFromSystemSettings(prefs, profile); 105 } 106 107 void MergeSessionLoadPage::OnProceed() { 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 109 proceeded_ = true; 110 NotifyBlockingPageComplete(); 111 } 112 113 void MergeSessionLoadPage::OnDontProceed() { 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 115 // Ignore if it's already proceeded. 116 if (proceeded_) 117 return; 118 NotifyBlockingPageComplete(); 119 } 120 121 void MergeSessionLoadPage::CommandReceived(const std::string& cmd) { 122 std::string command(cmd); 123 // The Jasonified response has quotes, remove them. 124 if (command.length() > 1 && command[0] == '"') 125 command = command.substr(1, command.length() - 2); 126 127 if (command == "proceed") { 128 interstitial_page_->Proceed(); 129 } else { 130 DVLOG(1) << "Unknown command:" << cmd; 131 } 132 } 133 134 void MergeSessionLoadPage::NotifyBlockingPageComplete() { 135 OAuth2LoginManager* manager = GetOAuth2LoginManager(); 136 if (manager) 137 manager->RemoveObserver(this); 138 139 if (!callback_.is_null()) { 140 BrowserThread::PostTask( 141 BrowserThread::IO, FROM_HERE, callback_); 142 } 143 } 144 145 OAuth2LoginManager* MergeSessionLoadPage::GetOAuth2LoginManager() { 146 content::BrowserContext* browser_context = web_contents_->GetBrowserContext(); 147 if (!browser_context) 148 return NULL; 149 150 Profile* profile = Profile::FromBrowserContext(browser_context); 151 if (!profile) 152 return NULL; 153 154 return OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile); 155 } 156 157 void MergeSessionLoadPage::OnSessionRestoreStateChanged( 158 Profile* user_profile, OAuth2LoginManager::SessionRestoreState state) { 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 160 161 OAuth2LoginManager* manager = GetOAuth2LoginManager(); 162 DVLOG(1) << "Merge session should " 163 << (!manager->ShouldBlockTabLoading() ? 164 " NOT " : "") 165 << " be blocking now, " 166 << state; 167 if (!manager->ShouldBlockTabLoading()) { 168 manager->RemoveObserver(this); 169 interstitial_page_->Proceed(); 170 } 171 } 172 173 } // namespace chromeos 174