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_CURRENTLY_ON(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 const std::string
& locale
= g_browser_process
->GetApplicationLocale();
83 if (extension
&& !extension
->from_bookmark()) {
84 LocalizedError::GetAppErrorStrings(url_
, extension
, locale
, &error_strings
);
85 resource_id
= IDR_OFFLINE_APP_LOAD_HTML
;
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 std::string template_html
= ResourceBundle::GetSharedInstance()
98 .GetRawDataResource(resource_id
)
100 webui::AppendWebUiCssTextDefaults(&template_html
);
101 // "t" is the id of the templates root node.
102 return webui::GetTemplatesHtml(template_html
, &error_strings
, "t");
105 void OfflineLoadPage::OverrideRendererPrefs(
106 content::RendererPreferences
* prefs
) {
107 Profile
* profile
= Profile::FromBrowserContext(
108 web_contents_
->GetBrowserContext());
109 renderer_preferences_util::UpdateFromSystemSettings(
110 prefs
, profile
, web_contents_
);
113 void OfflineLoadPage::OnProceed() {
114 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
116 NotifyBlockingPageComplete(true);
119 void OfflineLoadPage::OnDontProceed() {
120 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
121 // Ignore if it's already proceeded.
124 NotifyBlockingPageComplete(false);
127 void OfflineLoadPage::CommandReceived(const std::string
& cmd
) {
128 std::string
command(cmd
);
129 // The Jasonified response has quotes, remove them.
130 if (command
.length() > 1 && command
[0] == '"') {
131 command
= command
.substr(1, command
.length() - 2);
133 // TODO(oshima): record action for metrics.
134 if (command
== "open_network_settings") {
135 ash::Shell::GetInstance()
136 ->system_tray_delegate()
137 ->ShowNetworkSettingsForGuid("");
138 } else if (command
== "open_connectivity_diagnostics") {
139 Profile
* profile
= Profile::FromBrowserContext(
140 web_contents_
->GetBrowserContext());
141 const extensions::Extension
* extension
=
142 extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
143 "kodldpbjkkmmnilagfdheibampofhaom",
144 extensions::ExtensionRegistry::EVERYTHING
);
145 apps::LaunchPlatformAppWithUrl(profile
, extension
, "",
146 GURL::EmptyGURL(), GURL::EmptyGURL());
149 LOG(WARNING
) << "Unknown command:" << cmd
;
153 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed
) {
154 BrowserThread::PostTask(
155 BrowserThread::IO
, FROM_HERE
, base::Bind(callback_
, proceed
));
158 void OfflineLoadPage::OnConnectionTypeChanged(
159 net::NetworkChangeNotifier::ConnectionType type
) {
160 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
161 const bool online
= type
!= net::NetworkChangeNotifier::CONNECTION_NONE
;
162 DVLOG(1) << "ConnectionTypeObserver notification received: state="
163 << (online
? "online" : "offline");
165 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
166 interstitial_page_
->Proceed();
170 } // namespace chromeos