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