Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / chromeos / offline / offline_load_page.cc
blob36ca97037c143cf7d2086d8d82fc5a36743f1034
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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/offline/offline_load_page.h"
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/string_piece.h"
13 #include "base/stringprintf.h"
14 #include "base/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/chromeos/cros/cros_library.h"
17 #include "chrome/browser/chromeos/cros/network_library.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/chrome_notification_types.h"
23 #include "chrome/common/extensions/extension.h"
24 #include "chrome/common/extensions/extension_constants.h"
25 #include "chrome/common/extensions/extension_icon_set.h"
26 #include "chrome/common/jstemplate_builder.h"
27 #include "chrome/common/url_constants.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/interstitial_page.h"
30 #include "content/public/browser/notification_types.h"
31 #include "content/public/browser/web_contents.h"
32 #include "grit/browser_resources.h"
33 #include "grit/generated_resources.h"
34 #include "net/base/escape.h"
35 #include "ui/base/l10n/l10n_util.h"
36 #include "ui/base/resource/resource_bundle.h"
38 using content::BrowserThread;
39 using content::InterstitialPage;
40 using content::WebContents;
42 namespace {
44 // Maximum time to show a blank page.
45 const int kMaxBlankPeriod = 3000;
47 // A utility function to set the dictionary's value given by |resource_id|.
48 void SetString(DictionaryValue* strings, const char* name, int resource_id) {
49 strings->SetString(name, l10n_util::GetStringUTF16(resource_id));
52 } // namespace
54 namespace chromeos {
56 OfflineLoadPage::OfflineLoadPage(WebContents* web_contents,
57 const GURL& url,
58 const CompletionCallback& callback)
59 : callback_(callback),
60 proceeded_(false),
61 web_contents_(web_contents),
62 url_(url) {
63 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
64 interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this);
67 OfflineLoadPage::~OfflineLoadPage() {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
69 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
72 void OfflineLoadPage::Show() {
73 interstitial_page_->Show();
76 std::string OfflineLoadPage::GetHTMLContents() {
77 DictionaryValue strings;
78 int64 time_to_wait = kMaxBlankPeriod;
79 // Set the timeout to show the page.
80 strings.SetInteger("time_to_wait", static_cast<int>(time_to_wait));
81 // Button labels
82 SetString(&strings, "heading", IDS_OFFLINE_LOAD_HEADLINE);
83 SetString(&strings, "try_loading", IDS_OFFLINE_TRY_LOADING);
84 SetString(&strings, "network_settings", IDS_OFFLINE_NETWORK_SETTINGS);
86 // Activation
87 strings.SetBoolean("show_activation", ShowActivationMessage());
89 bool rtl = base::i18n::IsRTL();
90 strings.SetString("textdirection", rtl ? "rtl" : "ltr");
92 string16 failed_url(ASCIIToUTF16(url_.spec()));
93 if (rtl)
94 base::i18n::WrapStringWithLTRFormatting(&failed_url);
95 strings.SetString("url", failed_url);
97 // The offline page for app has icons and slightly different message.
98 Profile* profile = Profile::FromBrowserContext(
99 web_contents_->GetBrowserContext());
100 DCHECK(profile);
101 const extensions::Extension* extension = NULL;
102 ExtensionService* extensions_service = profile->GetExtensionService();
103 // Extension service does not exist in test.
104 if (extensions_service)
105 extension = extensions_service->extensions()->GetHostedAppByURL(
106 ExtensionURLInfo(url_));
108 if (extension)
109 GetAppOfflineStrings(extension, failed_url, &strings);
110 else
111 GetNormalOfflineStrings(failed_url, &strings);
113 base::StringPiece html(
114 ResourceBundle::GetSharedInstance().GetRawDataResource(
115 IDR_OFFLINE_LOAD_HTML));
116 return jstemplate_builder::GetI18nTemplateHtml(html, &strings);
119 void OfflineLoadPage::OverrideRendererPrefs(
120 content::RendererPreferences* prefs) {
121 Profile* profile = Profile::FromBrowserContext(
122 web_contents_->GetBrowserContext());
123 renderer_preferences_util::UpdateFromSystemSettings(prefs, profile);
126 void OfflineLoadPage::OnProceed() {
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
128 proceeded_ = true;
129 NotifyBlockingPageComplete(true);
132 void OfflineLoadPage::OnDontProceed() {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134 // Ignore if it's already proceeded.
135 if (proceeded_)
136 return;
137 NotifyBlockingPageComplete(false);
140 void OfflineLoadPage::GetAppOfflineStrings(
141 const extensions::Extension* app,
142 const string16& failed_url,
143 DictionaryValue* strings) const {
144 strings->SetString("title", app->name());
146 GURL icon_url = app->GetIconURL(extension_misc::EXTENSION_ICON_LARGE,
147 ExtensionIconSet::MATCH_BIGGER);
148 if (icon_url.is_empty()) {
149 strings->SetString("display_icon", "none");
150 strings->SetString("icon", string16());
151 } else {
152 // Default icon is not accessible from interstitial page.
153 // TODO(oshima): Figure out how to use default icon.
154 strings->SetString("display_icon", "block");
155 strings->SetString("icon", icon_url.spec());
158 strings->SetString(
159 "msg",
160 l10n_util::GetStringFUTF16(IDS_APP_OFFLINE_LOAD_DESCRIPTION,
161 net::EscapeForHTML(failed_url)));
164 void OfflineLoadPage::GetNormalOfflineStrings(
165 const string16& failed_url, DictionaryValue* strings) const {
166 strings->SetString("title", web_contents_->GetTitle());
168 // No icon for normal web site.
169 strings->SetString("display_icon", "none");
170 strings->SetString("icon", string16());
172 strings->SetString(
173 "msg",
174 l10n_util::GetStringFUTF16(IDS_SITE_OFFLINE_LOAD_DESCRIPTION,
175 net::EscapeForHTML(failed_url)));
178 void OfflineLoadPage::CommandReceived(const std::string& cmd) {
179 std::string command(cmd);
180 // The Jasonified response has quotes, remove them.
181 if (command.length() > 1 && command[0] == '"') {
182 command = command.substr(1, command.length() - 2);
184 // TODO(oshima): record action for metrics.
185 if (command == "proceed") {
186 interstitial_page_->Proceed();
187 } else if (command == "dontproceed") {
188 interstitial_page_->DontProceed();
189 } else if (command == "open_network_settings") {
190 ash::Shell::GetInstance()->tray_delegate()->ShowNetworkSettings();
191 } else {
192 LOG(WARNING) << "Unknown command:" << cmd;
196 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) {
197 BrowserThread::PostTask(
198 BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed));
201 void OfflineLoadPage::OnConnectionTypeChanged(
202 net::NetworkChangeNotifier::ConnectionType type) {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
204 const bool online = type != net::NetworkChangeNotifier::CONNECTION_NONE;
205 DVLOG(1) << "ConnectionTypeObserver notification received: state="
206 << (online ? "online" : "offline");
207 if (online) {
208 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
209 interstitial_page_->Proceed();
213 bool OfflineLoadPage::ShowActivationMessage() {
214 CrosLibrary* cros = CrosLibrary::Get();
215 if (!cros || !cros->GetNetworkLibrary()->cellular_available())
216 return false;
218 const CellularNetworkVector& cell_networks =
219 cros->GetNetworkLibrary()->cellular_networks();
220 for (size_t i = 0; i < cell_networks.size(); ++i) {
221 chromeos::ActivationState activation_state =
222 cell_networks[i]->activation_state();
223 if (activation_state == ACTIVATION_STATE_ACTIVATED)
224 return false;
226 return true;
229 } // namespace chromeos