| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/offline/offline_load_page.h" | 5 #include "chrome/browser/chromeos/offline/offline_load_page.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/chromeos/cros/cros_library.h" | 13 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 14 #include "chrome/browser/chromeos/cros/network_library.h" | 14 #include "chrome/browser/chromeos/cros/network_library.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/renderer_preferences_util.h" |
| 17 #include "chrome/browser/tab_contents/tab_util.h" | 18 #include "chrome/browser/tab_contents/tab_util.h" |
| 18 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_list.h" | 20 #include "chrome/browser/ui/browser_list.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 21 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/extensions/extension.h" | 22 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/jstemplate_builder.h" | 23 #include "chrome/common/jstemplate_builder.h" |
| 23 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 25 #include "content/browser/tab_contents/interstitial_page.h" |
| 24 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/notification_types.h" | 27 #include "content/public/browser/notification_types.h" |
| 26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 27 #include "grit/browser_resources.h" | 29 #include "grit/browser_resources.h" |
| 28 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
| 31 | 33 |
| 32 using content::BrowserThread; | 34 using content::BrowserThread; |
| 33 using content::WebContents; | 35 using content::WebContents; |
| 34 | 36 |
| 35 namespace { | 37 namespace { |
| 36 | 38 |
| 37 // Maximum time to show a blank page. | 39 // Maximum time to show a blank page. |
| 38 const int kMaxBlankPeriod = 3000; | 40 const int kMaxBlankPeriod = 3000; |
| 39 | 41 |
| 40 // A utility function to set the dictionary's value given by |resource_id|. | 42 // A utility function to set the dictionary's value given by |resource_id|. |
| 41 void SetString(DictionaryValue* strings, const char* name, int resource_id) { | 43 void SetString(DictionaryValue* strings, const char* name, int resource_id) { |
| 42 strings->SetString(name, l10n_util::GetStringUTF16(resource_id)); | 44 strings->SetString(name, l10n_util::GetStringUTF16(resource_id)); |
| 43 } | 45 } |
| 44 | 46 |
| 45 } // namespace | 47 } // namespace |
| 46 | 48 |
| 47 namespace chromeos { | 49 namespace chromeos { |
| 48 | 50 |
| 49 OfflineLoadPage::OfflineLoadPage(WebContents* web_contents, | 51 OfflineLoadPage::OfflineLoadPage(WebContents* web_contents, |
| 50 const GURL& url, | 52 const GURL& url, |
| 51 const CompletionCallback& callback) | 53 const CompletionCallback& callback) |
| 52 : ChromeInterstitialPage(web_contents, true, url), | 54 : callback_(callback), |
| 53 callback_(callback), | 55 proceeded_(false), |
| 54 proceeded_(false) { | 56 web_contents_(web_contents), |
| 57 url_(url) { |
| 55 net::NetworkChangeNotifier::AddOnlineStateObserver(this); | 58 net::NetworkChangeNotifier::AddOnlineStateObserver(this); |
| 59 interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this); |
| 60 interstitial_page_->Show(); |
| 56 } | 61 } |
| 57 | 62 |
| 58 OfflineLoadPage::~OfflineLoadPage() { | 63 OfflineLoadPage::~OfflineLoadPage() { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 65 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
| 61 } | 66 } |
| 62 | 67 |
| 63 std::string OfflineLoadPage::GetHTMLContents() { | 68 std::string OfflineLoadPage::GetHTMLContents() { |
| 64 DictionaryValue strings; | 69 DictionaryValue strings; |
| 65 int64 time_to_wait = 0; // kMaxBlankPeriod | 70 int64 time_to_wait = 0; // kMaxBlankPeriod |
| 66 // Set the timeout to show the page. | 71 // Set the timeout to show the page. |
| 67 strings.SetInteger("time_to_wait", static_cast<int>(time_to_wait)); | 72 strings.SetInteger("time_to_wait", static_cast<int>(time_to_wait)); |
| 68 // Button labels | 73 // Button labels |
| 69 SetString(&strings, "heading", IDS_OFFLINE_LOAD_HEADLINE); | 74 SetString(&strings, "heading", IDS_OFFLINE_LOAD_HEADLINE); |
| 70 SetString(&strings, "try_loading", IDS_OFFLINE_TRY_LOADING); | 75 SetString(&strings, "try_loading", IDS_OFFLINE_TRY_LOADING); |
| 71 SetString(&strings, "network_settings", IDS_OFFLINE_NETWORK_SETTINGS); | 76 SetString(&strings, "network_settings", IDS_OFFLINE_NETWORK_SETTINGS); |
| 72 | 77 |
| 73 // Activation | 78 // Activation |
| 74 strings.SetBoolean("show_activation", ShowActivationMessage()); | 79 strings.SetBoolean("show_activation", ShowActivationMessage()); |
| 75 | 80 |
| 76 bool rtl = base::i18n::IsRTL(); | 81 bool rtl = base::i18n::IsRTL(); |
| 77 strings.SetString("textdirection", rtl ? "rtl" : "ltr"); | 82 strings.SetString("textdirection", rtl ? "rtl" : "ltr"); |
| 78 | 83 |
| 79 string16 failed_url(ASCIIToUTF16(url().spec())); | 84 string16 failed_url(ASCIIToUTF16(url_.spec())); |
| 80 if (rtl) | 85 if (rtl) |
| 81 base::i18n::WrapStringWithLTRFormatting(&failed_url); | 86 base::i18n::WrapStringWithLTRFormatting(&failed_url); |
| 82 strings.SetString("url", failed_url); | 87 strings.SetString("url", failed_url); |
| 83 | 88 |
| 84 // The offline page for app has icons and slightly different message. | 89 // The offline page for app has icons and slightly different message. |
| 85 Profile* profile = Profile::FromBrowserContext(tab()->GetBrowserContext()); | 90 Profile* profile = Profile::FromBrowserContext( |
| 91 web_contents_->GetBrowserContext()); |
| 86 DCHECK(profile); | 92 DCHECK(profile); |
| 87 const Extension* extension = NULL; | 93 const Extension* extension = NULL; |
| 88 ExtensionService* extensions_service = profile->GetExtensionService(); | 94 ExtensionService* extensions_service = profile->GetExtensionService(); |
| 89 // Extension service does not exist in test. | 95 // Extension service does not exist in test. |
| 90 if (extensions_service) | 96 if (extensions_service) |
| 91 extension = extensions_service->extensions()->GetHostedAppByURL( | 97 extension = extensions_service->extensions()->GetHostedAppByURL( |
| 92 ExtensionURLInfo(url())); | 98 ExtensionURLInfo(url_)); |
| 93 | 99 |
| 94 if (extension) | 100 if (extension) |
| 95 GetAppOfflineStrings(extension, failed_url, &strings); | 101 GetAppOfflineStrings(extension, failed_url, &strings); |
| 96 else | 102 else |
| 97 GetNormalOfflineStrings(failed_url, &strings); | 103 GetNormalOfflineStrings(failed_url, &strings); |
| 98 | 104 |
| 99 base::StringPiece html( | 105 base::StringPiece html( |
| 100 ResourceBundle::GetSharedInstance().GetRawDataResource( | 106 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 101 IDR_OFFLINE_LOAD_HTML)); | 107 IDR_OFFLINE_LOAD_HTML)); |
| 102 return jstemplate_builder::GetI18nTemplateHtml(html, &strings); | 108 return jstemplate_builder::GetI18nTemplateHtml(html, &strings); |
| 103 } | 109 } |
| 104 | 110 |
| 111 void OfflineLoadPage::OverrideRendererPrefs( |
| 112 content::RendererPreferences* prefs) { |
| 113 Profile* profile = Profile::FromBrowserContext( |
| 114 web_contents_->GetBrowserContext()); |
| 115 renderer_preferences_util::UpdateFromSystemSettings(prefs, profile); |
| 116 } |
| 117 |
| 118 void OfflineLoadPage::OnProceed() { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 120 proceeded_ = true; |
| 121 NotifyBlockingPageComplete(true); |
| 122 } |
| 123 |
| 124 void OfflineLoadPage::OnDontProceed() { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 126 // Ignore if it's already proceeded. |
| 127 if (proceeded_) |
| 128 return; |
| 129 NotifyBlockingPageComplete(false); |
| 130 } |
| 131 |
| 105 void OfflineLoadPage::GetAppOfflineStrings( | 132 void OfflineLoadPage::GetAppOfflineStrings( |
| 106 const Extension* app, | 133 const Extension* app, |
| 107 const string16& failed_url, | 134 const string16& failed_url, |
| 108 DictionaryValue* strings) const { | 135 DictionaryValue* strings) const { |
| 109 strings->SetString("title", app->name()); | 136 strings->SetString("title", app->name()); |
| 110 | 137 |
| 111 GURL icon_url = app->GetIconURL(Extension::EXTENSION_ICON_LARGE, | 138 GURL icon_url = app->GetIconURL(Extension::EXTENSION_ICON_LARGE, |
| 112 ExtensionIconSet::MATCH_EXACTLY); | 139 ExtensionIconSet::MATCH_EXACTLY); |
| 113 if (icon_url.is_empty()) { | 140 if (icon_url.is_empty()) { |
| 114 strings->SetString("display_icon", "none"); | 141 strings->SetString("display_icon", "none"); |
| 115 strings->SetString("icon", string16()); | 142 strings->SetString("icon", string16()); |
| 116 } else { | 143 } else { |
| 117 // Default icon is not accessible from interstitial page. | 144 // Default icon is not accessible from interstitial page. |
| 118 // TODO(oshima): Figure out how to use default icon. | 145 // TODO(oshima): Figure out how to use default icon. |
| 119 strings->SetString("display_icon", "block"); | 146 strings->SetString("display_icon", "block"); |
| 120 strings->SetString("icon", icon_url.spec()); | 147 strings->SetString("icon", icon_url.spec()); |
| 121 } | 148 } |
| 122 | 149 |
| 123 strings->SetString( | 150 strings->SetString( |
| 124 "msg", | 151 "msg", |
| 125 l10n_util::GetStringFUTF16(IDS_APP_OFFLINE_LOAD_DESCRIPTION, | 152 l10n_util::GetStringFUTF16(IDS_APP_OFFLINE_LOAD_DESCRIPTION, |
| 126 failed_url)); | 153 failed_url)); |
| 127 } | 154 } |
| 128 | 155 |
| 129 void OfflineLoadPage::GetNormalOfflineStrings( | 156 void OfflineLoadPage::GetNormalOfflineStrings( |
| 130 const string16& failed_url, DictionaryValue* strings) const { | 157 const string16& failed_url, DictionaryValue* strings) const { |
| 131 strings->SetString("title", tab()->GetTitle()); | 158 strings->SetString("title", web_contents_->GetTitle()); |
| 132 | 159 |
| 133 // No icon for normal web site. | 160 // No icon for normal web site. |
| 134 strings->SetString("display_icon", "none"); | 161 strings->SetString("display_icon", "none"); |
| 135 strings->SetString("icon", string16()); | 162 strings->SetString("icon", string16()); |
| 136 | 163 |
| 137 strings->SetString( | 164 strings->SetString( |
| 138 "msg", | 165 "msg", |
| 139 l10n_util::GetStringFUTF16(IDS_SITE_OFFLINE_LOAD_DESCRIPTION, | 166 l10n_util::GetStringFUTF16(IDS_SITE_OFFLINE_LOAD_DESCRIPTION, |
| 140 failed_url)); | 167 failed_url)); |
| 141 } | 168 } |
| 142 | 169 |
| 143 void OfflineLoadPage::CommandReceived(const std::string& cmd) { | 170 void OfflineLoadPage::CommandReceived(const std::string& cmd) { |
| 144 std::string command(cmd); | 171 std::string command(cmd); |
| 145 // The Jasonified response has quotes, remove them. | 172 // The Jasonified response has quotes, remove them. |
| 146 if (command.length() > 1 && command[0] == '"') { | 173 if (command.length() > 1 && command[0] == '"') { |
| 147 command = command.substr(1, command.length() - 2); | 174 command = command.substr(1, command.length() - 2); |
| 148 } | 175 } |
| 149 // TODO(oshima): record action for metrics. | 176 // TODO(oshima): record action for metrics. |
| 150 if (command == "proceed") { | 177 if (command == "proceed") { |
| 151 Proceed(); | 178 interstitial_page_->Proceed(); |
| 152 } else if (command == "dontproceed") { | 179 } else if (command == "dontproceed") { |
| 153 DontProceed(); | 180 interstitial_page_->DontProceed(); |
| 154 } else if (command == "open_network_settings") { | 181 } else if (command == "open_network_settings") { |
| 155 Browser* browser = BrowserList::GetLastActive(); | 182 Browser* browser = BrowserList::GetLastActive(); |
| 156 DCHECK(browser); | 183 DCHECK(browser); |
| 157 browser->ShowOptionsTab(chrome::kInternetOptionsSubPage); | 184 browser->ShowOptionsTab(chrome::kInternetOptionsSubPage); |
| 158 } else if (command == "open_activate_broadband") { | 185 } else if (command == "open_activate_broadband") { |
| 159 Browser* browser = BrowserList::GetLastActive(); | 186 Browser* browser = BrowserList::GetLastActive(); |
| 160 DCHECK(browser); | 187 DCHECK(browser); |
| 161 browser->OpenMobilePlanTabAndActivate(); | 188 browser->OpenMobilePlanTabAndActivate(); |
| 162 } else { | 189 } else { |
| 163 LOG(WARNING) << "Unknown command:" << cmd; | 190 LOG(WARNING) << "Unknown command:" << cmd; |
| 164 } | 191 } |
| 165 } | 192 } |
| 166 | 193 |
| 167 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) { | 194 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) { |
| 168 BrowserThread::PostTask( | 195 BrowserThread::PostTask( |
| 169 BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed)); | 196 BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed)); |
| 170 } | 197 } |
| 171 | 198 |
| 172 void OfflineLoadPage::Proceed() { | |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 174 proceeded_ = true; | |
| 175 NotifyBlockingPageComplete(true); | |
| 176 InterstitialPage::Proceed(); | |
| 177 } | |
| 178 | |
| 179 void OfflineLoadPage::DontProceed() { | |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 181 // Ignore if it's already proceeded. | |
| 182 if (proceeded_) | |
| 183 return; | |
| 184 NotifyBlockingPageComplete(false); | |
| 185 InterstitialPage::DontProceed(); | |
| 186 } | |
| 187 | |
| 188 void OfflineLoadPage::OnOnlineStateChanged(bool online) { | 199 void OfflineLoadPage::OnOnlineStateChanged(bool online) { |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 190 DVLOG(1) << "OnlineStateObserver notification received: state=" | 201 DVLOG(1) << "OnlineStateObserver notification received: state=" |
| 191 << (online ? "online" : "offline"); | 202 << (online ? "online" : "offline"); |
| 192 if (online) { | 203 if (online) { |
| 193 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 204 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
| 194 Proceed(); | 205 interstitial_page_->Proceed(); |
| 195 } | 206 } |
| 196 } | 207 } |
| 197 | 208 |
| 198 bool OfflineLoadPage::ShowActivationMessage() { | 209 bool OfflineLoadPage::ShowActivationMessage() { |
| 199 CrosLibrary* cros = CrosLibrary::Get(); | 210 CrosLibrary* cros = CrosLibrary::Get(); |
| 200 if (!cros || !cros->GetNetworkLibrary()->cellular_available()) | 211 if (!cros || !cros->GetNetworkLibrary()->cellular_available()) |
| 201 return false; | 212 return false; |
| 202 | 213 |
| 203 const CellularNetworkVector& cell_networks = | 214 const CellularNetworkVector& cell_networks = |
| 204 cros->GetNetworkLibrary()->cellular_networks(); | 215 cros->GetNetworkLibrary()->cellular_networks(); |
| 205 for (size_t i = 0; i < cell_networks.size(); ++i) { | 216 for (size_t i = 0; i < cell_networks.size(); ++i) { |
| 206 chromeos::ActivationState activation_state = | 217 chromeos::ActivationState activation_state = |
| 207 cell_networks[i]->activation_state(); | 218 cell_networks[i]->activation_state(); |
| 208 if (activation_state == ACTIVATION_STATE_ACTIVATED) | 219 if (activation_state == ACTIVATION_STATE_ACTIVATED) |
| 209 return false; | 220 return false; |
| 210 } | 221 } |
| 211 return true; | 222 return true; |
| 212 } | 223 } |
| 213 | 224 |
| 214 } // namespace chromeos | 225 } // namespace chromeos |
| OLD | NEW |