Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / offline / offline_load_page.cc
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/browser/ui/zoom/zoom_controller.h"
24 #include "chrome/common/extensions/extension_constants.h"
25 #include "chrome/common/localized_error.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/grit/browser_resources.h"
29 #include "components/error_page/common/error_page_params.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/interstitial_page.h"
32 #include "content/public/browser/notification_types.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/renderer_preferences.h"
35 #include "extensions/browser/extension_registry.h"
36 #include "extensions/browser/extension_system.h"
37 #include "extensions/common/extension.h"
38 #include "extensions/common/extension_icon_set.h"
39 #include "extensions/common/manifest_handlers/icons_handler.h"
40 #include "net/base/escape.h"
41 #include "net/base/net_errors.h"
42 #include "ui/base/resource/resource_bundle.h"
43 #include "ui/base/webui/jstemplate_builder.h"
44 #include "ui/base/webui/web_ui_util.h"
45
46 using content::BrowserThread;
47 using content::InterstitialPage;
48 using content::WebContents;
49
50 namespace chromeos {
51
52 OfflineLoadPage::OfflineLoadPage(WebContents* web_contents,
53                                  const GURL& url,
54                                  const CompletionCallback& callback)
55     : callback_(callback),
56       proceeded_(false),
57       web_contents_(web_contents),
58       url_(url) {
59   net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
60   interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this);
61 }
62
63 OfflineLoadPage::~OfflineLoadPage() {
64   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65   net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
66 }
67
68 void OfflineLoadPage::Show() {
69   interstitial_page_->Show();
70 }
71
72 std::string OfflineLoadPage::GetHTMLContents() {
73   // Use a local error page.
74   int resource_id;
75   base::DictionaryValue error_strings;
76
77   // The offline page for app has icons and slightly different message.
78   Profile* profile = Profile::FromBrowserContext(
79       web_contents_->GetBrowserContext());
80   DCHECK(profile);
81   const extensions::Extension* extension = extensions::ExtensionRegistry::Get(
82       profile)->enabled_extensions().GetHostedAppByURL(url_);
83   if (extension && !extension->from_bookmark()) {
84     LocalizedError::GetAppErrorStrings(url_, extension, &error_strings);
85     resource_id = IDR_OFFLINE_APP_LOAD_HTML;
86   } else {
87     const std::string locale = g_browser_process->GetApplicationLocale();
88     const std::string accept_languages =
89         profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
90     LocalizedError::GetStrings(net::ERR_INTERNET_DISCONNECTED,
91                                net::kErrorDomain, url_, false, false, locale,
92                                accept_languages,
93                                scoped_ptr<error_page::ErrorPageParams>(),
94                                &error_strings);
95     resource_id = IDR_OFFLINE_NET_LOAD_HTML;
96   }
97
98   const base::StringPiece template_html(
99       ResourceBundle::GetSharedInstance().GetRawDataResource(
100           resource_id));
101   // "t" is the id of the templates root node.
102   return webui::GetTemplatesHtml(template_html, &error_strings, "t");
103 }
104
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_);
111 }
112
113 void OfflineLoadPage::OnProceed() {
114   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
115   proceeded_ = true;
116   NotifyBlockingPageComplete(true);
117 }
118
119 void OfflineLoadPage::OnDontProceed() {
120   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
121   // Ignore if it's already proceeded.
122   if (proceeded_)
123     return;
124   NotifyBlockingPageComplete(false);
125 }
126
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);
132   }
133   // TODO(oshima): record action for metrics.
134   if (command == "open_network_settings") {
135     ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings("");
136   } else if (command == "open_connectivity_diagnostics") {
137     Profile* profile = Profile::FromBrowserContext(
138         web_contents_->GetBrowserContext());
139     const extensions::Extension* extension =
140         extensions::ExtensionRegistry::Get(profile)->GetExtensionById(
141             "kodldpbjkkmmnilagfdheibampofhaom",
142             extensions::ExtensionRegistry::EVERYTHING);
143     apps::LaunchPlatformAppWithUrl(profile, extension, "",
144                                    GURL::EmptyGURL(), GURL::EmptyGURL());
145
146   } else {
147     LOG(WARNING) << "Unknown command:" << cmd;
148   }
149 }
150
151 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) {
152   BrowserThread::PostTask(
153       BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed));
154 }
155
156 void OfflineLoadPage::OnConnectionTypeChanged(
157     net::NetworkChangeNotifier::ConnectionType type) {
158   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
159   const bool online = type != net::NetworkChangeNotifier::CONNECTION_NONE;
160   DVLOG(1) << "ConnectionTypeObserver notification received: state="
161            << (online ? "online" : "offline");
162   if (online) {
163     net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
164     interstitial_page_->Proceed();
165   }
166 }
167
168 }  // namespace chromeos