OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "ios/chrome/browser/web/chrome_web_client.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/ios/ios_util.h" |
| 10 #include "base/mac/bundle_locations.h" |
| 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "components/dom_distiller/core/url_constants.h" |
| 13 #include "components/prefs/pref_service.h" |
| 14 #include "components/strings/grit/components_strings.h" |
| 15 #include "components/task_scheduler_util/initialization/browser_util.h" |
| 16 #include "components/task_scheduler_util/variations/browser_variations_util.h" |
| 17 #include "components/version_info/version_info.h" |
| 18 #include "ios/chrome/browser/application_context.h" |
| 19 #include "ios/chrome/browser/browser_about_rewriter.h" |
| 20 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 21 #include "ios/chrome/browser/chrome_switches.h" |
| 22 #include "ios/chrome/browser/chrome_url_constants.h" |
| 23 #include "ios/chrome/browser/experimental_flags.h" |
| 24 #include "ios/chrome/browser/ios_chrome_main_parts.h" |
| 25 #include "ios/chrome/browser/pref_names.h" |
| 26 #include "ios/chrome/browser/ssl/ios_ssl_error_handler.h" |
| 27 #import "ios/chrome/browser/ui/chrome_web_view_factory.h" |
| 28 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 29 #include "ios/public/provider/chrome/browser/voice/audio_session_controller.h" |
| 30 #include "ios/public/provider/chrome/browser/voice/voice_search_provider.h" |
| 31 #include "ios/web/public/browser_url_rewriter.h" |
| 32 #include "ios/web/public/user_agent.h" |
| 33 #include "net/http/http_util.h" |
| 34 #include "ui/base/l10n/l10n_util.h" |
| 35 #include "ui/base/resource/resource_bundle.h" |
| 36 #include "url/gurl.h" |
| 37 |
| 38 namespace { |
| 39 // Returns an autoreleased string containing the JavaScript loaded from a |
| 40 // bundled resource file with the given name (excluding extension). |
| 41 NSString* GetPageScript(NSString* script_file_name) { |
| 42 DCHECK(script_file_name); |
| 43 NSString* path = |
| 44 [base::mac::FrameworkBundle() pathForResource:script_file_name |
| 45 ofType:@"js"]; |
| 46 DCHECK(path) << "Script file not found: " |
| 47 << base::SysNSStringToUTF8(script_file_name) << ".js"; |
| 48 NSError* error = nil; |
| 49 NSString* content = [NSString stringWithContentsOfFile:path |
| 50 encoding:NSUTF8StringEncoding |
| 51 error:&error]; |
| 52 DCHECK(!error) << "Error fetching script: " |
| 53 << base::SysNSStringToUTF8(error.description); |
| 54 DCHECK(content); |
| 55 return content; |
| 56 } |
| 57 } |
| 58 |
| 59 ChromeWebClient::ChromeWebClient() {} |
| 60 |
| 61 ChromeWebClient::~ChromeWebClient() {} |
| 62 |
| 63 web::WebMainParts* ChromeWebClient::CreateWebMainParts() { |
| 64 return new IOSChromeMainParts(*base::CommandLine::ForCurrentProcess()); |
| 65 } |
| 66 |
| 67 void ChromeWebClient::PreWebViewCreation() const { |
| 68 // Initialize the audio session to allow a web page's audio to continue |
| 69 // playing after the app is backgrounded. |
| 70 VoiceSearchProvider* voice_provider = |
| 71 ios::GetChromeBrowserProvider()->GetVoiceSearchProvider(); |
| 72 if (voice_provider) { |
| 73 AudioSessionController* audio_controller = |
| 74 voice_provider->GetAudioSessionController(); |
| 75 if (audio_controller) { |
| 76 audio_controller->InitializeSessionIfNecessary(); |
| 77 } |
| 78 } |
| 79 } |
| 80 |
| 81 void ChromeWebClient::AddAdditionalSchemes( |
| 82 std::vector<url::SchemeWithType>* additional_standard_schemes) const { |
| 83 url::SchemeWithType scheme = {kChromeUIScheme, url::SCHEME_WITHOUT_PORT}; |
| 84 additional_standard_schemes->push_back(scheme); |
| 85 } |
| 86 |
| 87 std::string ChromeWebClient::GetAcceptLangs(web::BrowserState* state) const { |
| 88 ios::ChromeBrowserState* chrome_browser_state = |
| 89 ios::ChromeBrowserState::FromBrowserState(state); |
| 90 return chrome_browser_state->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 91 } |
| 92 |
| 93 std::string ChromeWebClient::GetApplicationLocale() const { |
| 94 DCHECK(GetApplicationContext()); |
| 95 return GetApplicationContext()->GetApplicationLocale(); |
| 96 } |
| 97 |
| 98 bool ChromeWebClient::IsAppSpecificURL(const GURL& url) const { |
| 99 return url.SchemeIs(kChromeUIScheme); |
| 100 } |
| 101 |
| 102 base::string16 ChromeWebClient::GetPluginNotSupportedText() const { |
| 103 return l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_SUPPORTED); |
| 104 } |
| 105 |
| 106 std::string ChromeWebClient::GetProduct() const { |
| 107 std::string product("CriOS/"); |
| 108 product += version_info::GetVersionNumber(); |
| 109 return product; |
| 110 } |
| 111 |
| 112 std::string ChromeWebClient::GetUserAgent(bool desktop_user_agent) const { |
| 113 // Using desktop user agent overrides a command-line user agent, so that |
| 114 // request desktop site can still work when using an overridden UA. |
| 115 if (desktop_user_agent) { |
| 116 return base::SysNSStringToUTF8(ChromeWebView::kDesktopUserAgent); |
| 117 } |
| 118 |
| 119 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 120 if (command_line->HasSwitch(switches::kUserAgent)) { |
| 121 std::string user_agent = |
| 122 command_line->GetSwitchValueASCII(switches::kUserAgent); |
| 123 if (net::HttpUtil::IsValidHeaderValue(user_agent)) |
| 124 return user_agent; |
| 125 LOG(WARNING) << "Ignored invalid value for flag --" << switches::kUserAgent; |
| 126 } |
| 127 |
| 128 std::string product = GetProduct(); |
| 129 return web::BuildUserAgentFromProduct(product); |
| 130 } |
| 131 |
| 132 base::string16 ChromeWebClient::GetLocalizedString(int message_id) const { |
| 133 return l10n_util::GetStringUTF16(message_id); |
| 134 } |
| 135 |
| 136 base::StringPiece ChromeWebClient::GetDataResource( |
| 137 int resource_id, |
| 138 ui::ScaleFactor scale_factor) const { |
| 139 return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale( |
| 140 resource_id, scale_factor); |
| 141 } |
| 142 |
| 143 base::RefCountedMemory* ChromeWebClient::GetDataResourceBytes( |
| 144 int resource_id) const { |
| 145 return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id); |
| 146 } |
| 147 |
| 148 void ChromeWebClient::GetAdditionalWebUISchemes( |
| 149 std::vector<std::string>* additional_schemes) { |
| 150 additional_schemes->push_back(dom_distiller::kDomDistillerScheme); |
| 151 } |
| 152 |
| 153 void ChromeWebClient::PostBrowserURLRewriterCreation( |
| 154 web::BrowserURLRewriter* rewriter) { |
| 155 rewriter->AddURLRewriter(&WillHandleWebBrowserAboutURL); |
| 156 } |
| 157 |
| 158 NSString* ChromeWebClient::GetEarlyPageScript() const { |
| 159 return GetPageScript(@"print"); |
| 160 } |
| 161 |
| 162 void ChromeWebClient::AllowCertificateError( |
| 163 web::WebState* web_state, |
| 164 int cert_error, |
| 165 const net::SSLInfo& info, |
| 166 const GURL& request_url, |
| 167 bool overridable, |
| 168 const base::Callback<void(bool)>& callback) { |
| 169 IOSSSLErrorHandler::HandleSSLError(web_state, cert_error, info, request_url, |
| 170 overridable, callback); |
| 171 } |
| 172 |
| 173 void ChromeWebClient::GetTaskSchedulerInitializationParams( |
| 174 std::vector<base::SchedulerWorkerPoolParams>* params_vector, |
| 175 base::TaskScheduler::WorkerPoolIndexForTraitsCallback* |
| 176 index_to_traits_callback) { |
| 177 DCHECK(params_vector); |
| 178 DCHECK(index_to_traits_callback); |
| 179 // If this call fails, web will fall back to the default params. |
| 180 *params_vector = task_scheduler_util::variations:: |
| 181 GetBrowserSchedulerWorkerPoolParamsFromVariations(); |
| 182 *index_to_traits_callback = base::Bind( |
| 183 &task_scheduler_util::initialization::BrowserWorkerPoolIndexForTraits); |
| 184 } |
| 185 |
| 186 void ChromeWebClient::PerformExperimentalTaskSchedulerRedirections() { |
| 187 task_scheduler_util::variations:: |
| 188 MaybePerformBrowserTaskSchedulerRedirection(); |
| 189 } |
OLD | NEW |