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 |
105 void OfflineLoadPage::GetAppOfflineStrings( | 118 void OfflineLoadPage::GetAppOfflineStrings( |
106 const Extension* app, | 119 const Extension* app, |
107 const string16& failed_url, | 120 const string16& failed_url, |
108 DictionaryValue* strings) const { | 121 DictionaryValue* strings) const { |
109 strings->SetString("title", app->name()); | 122 strings->SetString("title", app->name()); |
110 | 123 |
111 GURL icon_url = app->GetIconURL(Extension::EXTENSION_ICON_LARGE, | 124 GURL icon_url = app->GetIconURL(Extension::EXTENSION_ICON_LARGE, |
112 ExtensionIconSet::MATCH_EXACTLY); | 125 ExtensionIconSet::MATCH_EXACTLY); |
113 if (icon_url.is_empty()) { | 126 if (icon_url.is_empty()) { |
114 strings->SetString("display_icon", "none"); | 127 strings->SetString("display_icon", "none"); |
115 strings->SetString("icon", string16()); | 128 strings->SetString("icon", string16()); |
116 } else { | 129 } else { |
117 // Default icon is not accessible from interstitial page. | 130 // Default icon is not accessible from interstitial page. |
118 // TODO(oshima): Figure out how to use default icon. | 131 // TODO(oshima): Figure out how to use default icon. |
119 strings->SetString("display_icon", "block"); | 132 strings->SetString("display_icon", "block"); |
120 strings->SetString("icon", icon_url.spec()); | 133 strings->SetString("icon", icon_url.spec()); |
121 } | 134 } |
122 | 135 |
123 strings->SetString( | 136 strings->SetString( |
124 "msg", | 137 "msg", |
125 l10n_util::GetStringFUTF16(IDS_APP_OFFLINE_LOAD_DESCRIPTION, | 138 l10n_util::GetStringFUTF16(IDS_APP_OFFLINE_LOAD_DESCRIPTION, |
126 failed_url)); | 139 failed_url)); |
127 } | 140 } |
128 | 141 |
129 void OfflineLoadPage::GetNormalOfflineStrings( | 142 void OfflineLoadPage::GetNormalOfflineStrings( |
130 const string16& failed_url, DictionaryValue* strings) const { | 143 const string16& failed_url, DictionaryValue* strings) const { |
131 strings->SetString("title", tab()->GetTitle()); | 144 strings->SetString("title", web_contents_->GetTitle()); |
132 | 145 |
133 // No icon for normal web site. | 146 // No icon for normal web site. |
134 strings->SetString("display_icon", "none"); | 147 strings->SetString("display_icon", "none"); |
135 strings->SetString("icon", string16()); | 148 strings->SetString("icon", string16()); |
136 | 149 |
137 strings->SetString( | 150 strings->SetString( |
138 "msg", | 151 "msg", |
139 l10n_util::GetStringFUTF16(IDS_SITE_OFFLINE_LOAD_DESCRIPTION, | 152 l10n_util::GetStringFUTF16(IDS_SITE_OFFLINE_LOAD_DESCRIPTION, |
140 failed_url)); | 153 failed_url)); |
141 } | 154 } |
(...skipping 24 matching lines...) Expand all Loading... |
166 | 179 |
167 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) { | 180 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) { |
168 BrowserThread::PostTask( | 181 BrowserThread::PostTask( |
169 BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed)); | 182 BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed)); |
170 } | 183 } |
171 | 184 |
172 void OfflineLoadPage::Proceed() { | 185 void OfflineLoadPage::Proceed() { |
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
174 proceeded_ = true; | 187 proceeded_ = true; |
175 NotifyBlockingPageComplete(true); | 188 NotifyBlockingPageComplete(true); |
176 InterstitialPage::Proceed(); | 189 interstitial_page_->Proceed(); |
177 } | 190 } |
178 | 191 |
179 void OfflineLoadPage::DontProceed() { | 192 void OfflineLoadPage::DontProceed() { |
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
181 // Ignore if it's already proceeded. | 194 // Ignore if it's already proceeded. |
182 if (proceeded_) | 195 if (proceeded_) |
183 return; | 196 return; |
184 NotifyBlockingPageComplete(false); | 197 NotifyBlockingPageComplete(false); |
185 InterstitialPage::DontProceed(); | 198 interstitial_page_->DontProceed(); |
186 } | 199 } |
187 | 200 |
188 void OfflineLoadPage::OnOnlineStateChanged(bool online) { | 201 void OfflineLoadPage::OnOnlineStateChanged(bool online) { |
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
190 DVLOG(1) << "OnlineStateObserver notification received: state=" | 203 DVLOG(1) << "OnlineStateObserver notification received: state=" |
191 << (online ? "online" : "offline"); | 204 << (online ? "online" : "offline"); |
192 if (online) { | 205 if (online) { |
193 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); | 206 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
194 Proceed(); | 207 Proceed(); |
195 } | 208 } |
196 } | 209 } |
197 | 210 |
198 bool OfflineLoadPage::ShowActivationMessage() { | 211 bool OfflineLoadPage::ShowActivationMessage() { |
199 CrosLibrary* cros = CrosLibrary::Get(); | 212 CrosLibrary* cros = CrosLibrary::Get(); |
200 if (!cros || !cros->GetNetworkLibrary()->cellular_available()) | 213 if (!cros || !cros->GetNetworkLibrary()->cellular_available()) |
201 return false; | 214 return false; |
202 | 215 |
203 const CellularNetworkVector& cell_networks = | 216 const CellularNetworkVector& cell_networks = |
204 cros->GetNetworkLibrary()->cellular_networks(); | 217 cros->GetNetworkLibrary()->cellular_networks(); |
205 for (size_t i = 0; i < cell_networks.size(); ++i) { | 218 for (size_t i = 0; i < cell_networks.size(); ++i) { |
206 chromeos::ActivationState activation_state = | 219 chromeos::ActivationState activation_state = |
207 cell_networks[i]->activation_state(); | 220 cell_networks[i]->activation_state(); |
208 if (activation_state == ACTIVATION_STATE_ACTIVATED) | 221 if (activation_state == ACTIVATION_STATE_ACTIVATED) |
209 return false; | 222 return false; |
210 } | 223 } |
211 return true; | 224 return true; |
212 } | 225 } |
213 | 226 |
214 } // namespace chromeos | 227 } // namespace chromeos |
OLD | NEW |