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 "apps/launcher.h"
9 #include "ash/shell_delegate.h"
10 #include "ash/system/tray/system_tray_delegate.h"
11 #include "base/i18n/rtl.h"
12 #include "base/metrics/histogram.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/values.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/renderer_preferences_util.h"
22 #include "chrome/browser/tab_contents/tab_util.h"
23 #include "chrome/common/extensions/extension_constants.h"
24 #include "chrome/common/localized_error.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/grit/browser_resources.h"
28 #include "components/error_page/common/error_page_params.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/interstitial_page.h"
31 #include "content/public/browser/notification_types.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/renderer_preferences.h"
34 #include "extensions/browser/extension_registry.h"
35 #include "extensions/browser/extension_system.h"
36 #include "extensions/common/extension.h"
37 #include "extensions/common/extension_icon_set.h"
38 #include "extensions/common/manifest_handlers/icons_handler.h"
39 #include "net/base/escape.h"
40 #include "net/base/net_errors.h"
41 #include "ui/base/resource/resource_bundle.h"
42 #include "ui/base/webui/jstemplate_builder.h"
43 #include "ui/base/webui/web_ui_util.h"
45 using content::BrowserThread
;
46 using content::InterstitialPage
;
47 using content::WebContents
;
51 OfflineLoadPage::OfflineLoadPage(WebContents
* web_contents
,
53 const CompletionCallback
& callback
)
54 : callback_(callback
),
56 web_contents_(web_contents
),
58 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
59 interstitial_page_
= InterstitialPage::Create(web_contents
, true, url
, this);
62 OfflineLoadPage::~OfflineLoadPage() {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
64 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
67 void OfflineLoadPage::Show() {
68 interstitial_page_
->Show();
71 std::string
OfflineLoadPage::GetHTMLContents() {
72 // Use a local error page.
74 base::DictionaryValue error_strings
;
76 // The offline page for app has icons and slightly different message.
77 Profile
* profile
= Profile::FromBrowserContext(
78 web_contents_
->GetBrowserContext());
80 const extensions::Extension
* extension
= extensions::ExtensionRegistry::Get(
81 profile
)->enabled_extensions().GetHostedAppByURL(url_
);
82 if (extension
&& !extension
->from_bookmark()) {
83 LocalizedError::GetAppErrorStrings(url_
, extension
, &error_strings
);
84 resource_id
= IDR_OFFLINE_APP_LOAD_HTML
;
86 const std::string locale
= g_browser_process
->GetApplicationLocale();
87 const std::string accept_languages
=
88 profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
89 LocalizedError::GetStrings(net::ERR_INTERNET_DISCONNECTED
,
90 net::kErrorDomain
, url_
, false, false, locale
,
92 scoped_ptr
<error_page::ErrorPageParams
>(),
94 resource_id
= IDR_OFFLINE_NET_LOAD_HTML
;
97 const base::StringPiece
template_html(
98 ResourceBundle::GetSharedInstance().GetRawDataResource(
100 // "t" is the id of the templates root node.
101 return webui::GetTemplatesHtml(template_html
, &error_strings
, "t");
104 void OfflineLoadPage::OverrideRendererPrefs(
105 content::RendererPreferences
* prefs
) {
106 Profile
* profile
= Profile::FromBrowserContext(
107 web_contents_
->GetBrowserContext());
108 renderer_preferences_util::UpdateFromSystemSettings(
109 prefs
, profile
, web_contents_
);
112 void OfflineLoadPage::OnProceed() {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
115 NotifyBlockingPageComplete(true);
118 void OfflineLoadPage::OnDontProceed() {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
120 // Ignore if it's already proceeded.
123 NotifyBlockingPageComplete(false);
126 void OfflineLoadPage::CommandReceived(const std::string
& cmd
) {
127 std::string
command(cmd
);
128 // The Jasonified response has quotes, remove them.
129 if (command
.length() > 1 && command
[0] == '"') {
130 command
= command
.substr(1, command
.length() - 2);
132 // TODO(oshima): record action for metrics.
133 if (command
== "open_network_settings") {
134 ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings("");
135 } else if (command
== "open_connectivity_diagnostics") {
136 Profile
* profile
= Profile::FromBrowserContext(
137 web_contents_
->GetBrowserContext());
138 const extensions::Extension
* extension
=
139 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
140 "kodldpbjkkmmnilagfdheibampofhaom",
141 extensions::ExtensionRegistry::EVERYTHING
);
142 apps::LaunchPlatformAppWithUrl(profile
, extension
, "",
143 GURL::EmptyGURL(), GURL::EmptyGURL());
146 LOG(WARNING
) << "Unknown command:" << cmd
;
150 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed
) {
151 BrowserThread::PostTask(
152 BrowserThread::IO
, FROM_HERE
, base::Bind(callback_
, proceed
));
155 void OfflineLoadPage::OnConnectionTypeChanged(
156 net::NetworkChangeNotifier::ConnectionType type
) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
158 const bool online
= type
!= net::NetworkChangeNotifier::CONNECTION_NONE
;
159 DVLOG(1) << "ConnectionTypeObserver notification received: state="
160 << (online
? "online" : "offline");
162 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
163 interstitial_page_
->Proceed();
167 } // namespace chromeos