02198472188c31ba066aabd81d9fe7a4b9d2477d
[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 "ash/shell.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/system/tray/system_tray_delegate.h"
10 #include "base/i18n/rtl.h"
11 #include "base/metrics/histogram.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_system.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.h"
25 #include "chrome/common/extensions/extension_constants.h"
26 #include "chrome/common/extensions/extension_icon_set.h"
27 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
28 #include "chrome/common/localized_error.h"
29 #include "chrome/common/pref_names.h"
30 #include "chrome/common/url_constants.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/interstitial_page.h"
33 #include "content/public/browser/notification_types.h"
34 #include "content/public/browser/web_contents.h"
35 #include "grit/browser_resources.h"
36 #include "grit/chromium_strings.h"
37 #include "grit/generated_resources.h"
38 #include "grit/google_chrome_strings.h"
39 #include "grit/theme_resources.h"
40 #include "net/base/escape.h"
41 #include "net/base/net_errors.h"
42 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/resource/resource_bundle.h"
44 #include "ui/base/webui/jstemplate_builder.h"
45 #include "ui/base/webui/web_ui_util.h"
46
47 using content::BrowserThread;
48 using content::InterstitialPage;
49 using content::WebContents;
50
51 namespace {
52
53 // A utility function to set the dictionary's value given by |resource_id|.
54 void SetString(DictionaryValue* strings, const char* name, int resource_id) {
55   strings->SetString(name, l10n_util::GetStringUTF16(resource_id));
56 }
57
58 }  // namespace
59
60 namespace chromeos {
61
62 OfflineLoadPage::OfflineLoadPage(WebContents* web_contents,
63                                  const GURL& url,
64                                  const CompletionCallback& callback)
65     : callback_(callback),
66       proceeded_(false),
67       web_contents_(web_contents),
68       url_(url) {
69   net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
70   interstitial_page_ = InterstitialPage::Create(web_contents, true, url, this);
71 }
72
73 OfflineLoadPage::~OfflineLoadPage() {
74   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75   net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
76 }
77
78 void OfflineLoadPage::Show() {
79   interstitial_page_->Show();
80 }
81
82 std::string OfflineLoadPage::GetHTMLContents() {
83   // Use a local error page.
84   int resource_id;
85   base::DictionaryValue error_strings;
86
87   // The offline page for app has icons and slightly different message.
88   Profile* profile = Profile::FromBrowserContext(
89       web_contents_->GetBrowserContext());
90   DCHECK(profile);
91   const extensions::Extension* extension = NULL;
92   ExtensionService* extensions_service =
93       extensions::ExtensionSystem::Get(profile)->extension_service();
94
95   // Extension service does not exist in test.
96   if (extensions_service)
97     extension = extensions_service->extensions()->GetHostedAppByURL(url_);
98
99   if (extension && !extension->from_bookmark()) {
100     LocalizedError::GetAppErrorStrings(url_, extension, &error_strings);
101     resource_id = IDR_OFFLINE_APP_LOAD_HTML;
102   } else {
103     const std::string locale = g_browser_process->GetApplicationLocale();
104     const std::string accept_languages =
105         profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
106     LocalizedError::GetStrings(net::ERR_INTERNET_DISCONNECTED,
107                                net::kErrorDomain, url_, false, locale,
108                                accept_languages, &error_strings);
109     resource_id = IDR_OFFLINE_NET_LOAD_HTML;
110   }
111
112   const base::StringPiece template_html(
113       ResourceBundle::GetSharedInstance().GetRawDataResource(
114           resource_id));
115   // "t" is the id of the templates root node.
116   return webui::GetTemplatesHtml(template_html, &error_strings, "t");
117 }
118
119  void OfflineLoadPage::OverrideRendererPrefs(
120       content::RendererPreferences* prefs) {
121   Profile* profile = Profile::FromBrowserContext(
122       web_contents_->GetBrowserContext());
123   renderer_preferences_util::UpdateFromSystemSettings(prefs, profile);
124 }
125
126 void OfflineLoadPage::OnProceed() {
127   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
128   proceeded_ = true;
129   NotifyBlockingPageComplete(true);
130 }
131
132 void OfflineLoadPage::OnDontProceed() {
133   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134   // Ignore if it's already proceeded.
135   if (proceeded_)
136     return;
137   NotifyBlockingPageComplete(false);
138 }
139
140 void OfflineLoadPage::CommandReceived(const std::string& cmd) {
141   std::string command(cmd);
142   // The Jasonified response has quotes, remove them.
143   if (command.length() > 1 && command[0] == '"') {
144     command = command.substr(1, command.length() - 2);
145   }
146   // TODO(oshima): record action for metrics.
147   if (command == "open_network_settings") {
148     ash::Shell::GetInstance()->system_tray_delegate()->ShowNetworkSettings("");
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