Upstream version 5.34.104.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/extensions/extension_service.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/renderer_preferences_util.h"
23 #include "chrome/browser/tab_contents/tab_util.h"
24 #include "chrome/common/extensions/extension_constants.h"
25 #include "chrome/common/extensions/extension_icon_set.h"
26 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
27 #include "chrome/common/localized_error.h"
28 #include "chrome/common/pref_names.h"
29 #include "chrome/common/url_constants.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 "extensions/browser/extension_system.h"
35 #include "extensions/common/extension.h"
36 #include "grit/browser_resources.h"
37 #include "grit/chromium_strings.h"
38 #include "grit/generated_resources.h"
39 #include "grit/google_chrome_strings.h"
40 #include "grit/theme_resources.h"
41 #include "net/base/escape.h"
42 #include "net/base/net_errors.h"
43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/base/resource/resource_bundle.h"
45 #include "ui/base/webui/jstemplate_builder.h"
46 #include "ui/base/webui/web_ui_util.h"
47
48 using content::BrowserThread;
49 using content::InterstitialPage;
50 using content::WebContents;
51
52 namespace chromeos {
53
54 OfflineLoadPage::OfflineLoadPage(WebContents* web_contents,
55                                  const GURL& url,
56                                  const CompletionCallback& callback)
57     : callback_(callback),
58       proceeded_(false),
59       web_contents_(web_contents),
60       url_(url) {
61   net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
62   interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this);
63 }
64
65 OfflineLoadPage::~OfflineLoadPage() {
66   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67   net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
68 }
69
70 void OfflineLoadPage::Show() {
71   interstitial_page_->Show();
72 }
73
74 std::string OfflineLoadPage::GetHTMLContents() {
75   // Use a local error page.
76   int resource_id;
77   base::DictionaryValue error_strings;
78
79   // The offline page for app has icons and slightly different message.
80   Profile* profile = Profile::FromBrowserContext(
81       web_contents_->GetBrowserContext());
82   DCHECK(profile);
83   const extensions::Extension* extension = NULL;
84   ExtensionService* extensions_service =
85       extensions::ExtensionSystem::Get(profile)->extension_service();
86
87   // Extension service does not exist in test.
88   if (extensions_service)
89     extension = extensions_service->extensions()->GetHostedAppByURL(url_);
90
91   if (extension && !extension->from_bookmark()) {
92     LocalizedError::GetAppErrorStrings(url_, extension, &error_strings);
93     resource_id = IDR_OFFLINE_APP_LOAD_HTML;
94   } else {
95     const std::string locale = g_browser_process->GetApplicationLocale();
96     const std::string accept_languages =
97         profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
98     LocalizedError::GetStrings(net::ERR_INTERNET_DISCONNECTED,
99                                net::kErrorDomain, url_, false, false, locale,
100                                accept_languages, &error_strings);
101     resource_id = IDR_OFFLINE_NET_LOAD_HTML;
102   }
103
104   const base::StringPiece template_html(
105       ResourceBundle::GetSharedInstance().GetRawDataResource(
106           resource_id));
107   // "t" is the id of the templates root node.
108   return webui::GetTemplatesHtml(template_html, &error_strings, "t");
109 }
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
118 void OfflineLoadPage::OnProceed() {
119   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
120   proceeded_ = true;
121   NotifyBlockingPageComplete(true);
122 }
123
124 void OfflineLoadPage::OnDontProceed() {
125   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126   // Ignore if it's already proceeded.
127   if (proceeded_)
128     return;
129   NotifyBlockingPageComplete(false);
130 }
131
132 void OfflineLoadPage::CommandReceived(const std::string& cmd) {
133   std::string command(cmd);
134   // The Jasonified response has quotes, remove them.
135   if (command.length() > 1 && command[0] == '"') {
136     command = command.substr(1, command.length() - 2);
137   }
138   // TODO(oshima): record action for metrics.
139   if (command == "open_network_settings") {
140     ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings("");
141   } else if (command == "open_connectivity_diagnostics") {
142     Profile* profile = Profile::FromBrowserContext(
143         web_contents_->GetBrowserContext());
144     const extensions::Extension* extension = profile->GetExtensionService()->
145         GetInstalledExtension("kodldpbjkkmmnilagfdheibampofhaom");
146     apps::LaunchPlatformAppWithUrl(profile, extension, "",
147                                    GURL::EmptyGURL(), GURL::EmptyGURL());
148
149   } else {
150     LOG(WARNING) << "Unknown command:" << cmd;
151   }
152 }
153
154 void OfflineLoadPage::NotifyBlockingPageComplete(bool proceed) {
155   BrowserThread::PostTask(
156       BrowserThread::IO, FROM_HERE, base::Bind(callback_, proceed));
157 }
158
159 void OfflineLoadPage::OnConnectionTypeChanged(
160     net::NetworkChangeNotifier::ConnectionType type) {
161   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162   const bool online = type != net::NetworkChangeNotifier::CONNECTION_NONE;
163   DVLOG(1) << "ConnectionTypeObserver notification received: state="
164            << (online ? "online" : "offline");
165   if (online) {
166     net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
167     interstitial_page_->Proceed();
168   }
169 }
170
171 }  // namespace chromeos