OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/chromeos/offline/offline_load_page.h" | |
6 | |
7 #include "apps/launcher.h" | |
8 #include "ash/shell.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/common/extension.h" | |
36 #include "net/base/escape.h" | |
37 #include "net/base/net_errors.h" | |
38 #include "ui/base/resource/resource_bundle.h" | |
39 #include "ui/base/webui/jstemplate_builder.h" | |
40 #include "ui/base/webui/web_ui_util.h" | |
41 | |
42 using content::BrowserThread; | |
43 using content::InterstitialPage; | |
44 using content::WebContents; | |
45 | |
46 namespace chromeos { | |
47 | |
48 OfflineLoadPage::OfflineLoadPage(WebContents* web_contents, | |
49 const GURL& url, | |
50 const CompletionCallback& callback) | |
51 : callback_(callback), | |
52 proceeded_(false), | |
53 web_contents_(web_contents), | |
54 url_(url) { | |
55 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | |
56 interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this); | |
57 } | |
58 | |
59 OfflineLoadPage::~OfflineLoadPage() { | |
60 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
61 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | |
62 } | |
63 | |
64 void OfflineLoadPage::Show() { | |
65 interstitial_page_->Show(); | |
66 } | |
67 | |
68 std::string OfflineLoadPage::GetHTMLContents() { | |
69 // Use a local error page. | |
70 int resource_id; | |
71 base::DictionaryValue error_strings; | |
72 | |
73 Profile* profile = Profile::FromBrowserContext( | |
74 web_contents_->GetBrowserContext()); | |
75 DCHECK(profile); | |
76 const std::string& locale = g_browser_process->GetApplicationLocale(); | |
77 const std::string accept_languages = | |
78 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
79 LocalizedError::GetStrings(net::ERR_INTERNET_DISCONNECTED, net::kErrorDomain, | |
80 url_, false, false, locale, accept_languages, | |
81 scoped_ptr<error_page::ErrorPageParams>(), | |
82 &error_strings); | |
83 resource_id = IDR_OFFLINE_NET_LOAD_HTML; | |
84 | |
85 std::string template_html = ResourceBundle::GetSharedInstance() | |
86 .GetRawDataResource(resource_id) | |
87 .as_string(); | |
88 webui::AppendWebUiCssTextDefaults(&template_html); | |
89 // "t" is the id of the templates root node. | |
90 return webui::GetTemplatesHtml(template_html, &error_strings, "t"); | |
91 } | |
92 | |
93 void OfflineLoadPage::OverrideRendererPrefs( | |
94 content::RendererPreferences* prefs) { | |
95 Profile* profile = Profile::FromBrowserContext( | |
96 web_contents_->GetBrowserContext()); | |
97 renderer_preferences_util::UpdateFromSystemSettings( | |
98 prefs, profile, web_contents_); | |
99 } | |
100 | |
101 void OfflineLoadPage::OnProceed() { | |
102 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
103 proceeded_ = true; | |
104 NotifyBlockingPageComplete(true); | |
105 } | |
106 | |
107 void OfflineLoadPage::OnDontProceed() { | |
108 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
109 // Ignore if it's already proceeded. | |
110 if (proceeded_) | |
111 return; | |
112 NotifyBlockingPageComplete(false); | |
113 } | |
114 | |
115 void OfflineLoadPage::CommandReceived(const std::string& cmd) { | |
116 std::string command(cmd); | |
117 // The Jasonified response has quotes, remove them. | |
118 if (command.length() > 1 && command[0] == '"') { | |
119 command = command.substr(1, command.length() - 2); | |
120 } | |
121 // TODO(oshima): record action for metrics. | |
122 if (command == "open_network_settings") { | |
123 ash::Shell::GetInstance() | |
124 ->system_tray_delegate() | |
125 ->ShowNetworkSettingsForGuid(""); | |
126 } else if (command == "open_connectivity_diagnostics") { | |
127 Profile* profile = Profile::FromBrowserContext( | |
128 web_contents_->GetBrowserContext()); | |
129 const extensions::Extension* extension = | |
130 extensions::ExtensionRegistry::Get(profile)->GetExtensionById( | |
131 "kodldpbjkkmmnilagfdheibampofhaom", | |
132 extensions::ExtensionRegistry::EVERYTHING); | |
133 apps::LaunchPlatformAppWithUrl(profile, extension, "", | |
134 GURL::EmptyGURL(), GURL::EmptyGURL()); | |
135 | |
136 } else { | |
137 LOG(WARNING) << "Unknown command:" << cmd; | |
138 } | |
139 } | |
140 | |
141 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) { | |
142 BrowserThread::PostTask( | |
143 BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed)); | |
144 } | |
145 | |
146 void OfflineLoadPage::OnConnectionTypeChanged( | |
147 net::NetworkChangeNotifier::ConnectionType type) { | |
148 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
149 const bool online = type != net::NetworkChangeNotifier::CONNECTION_NONE; | |
150 DVLOG(1) << "ConnectionTypeObserver notification received: state=" | |
151 << (online ? "online" : "offline"); | |
152 if (online) { | |
153 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | |
154 interstitial_page_->Proceed(); | |
155 } | |
156 } | |
157 | |
158 } // namespace chromeos | |
OLD | NEW |