• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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/extensions/chrome_extensions_browser_client.h"
6 
7 #include "base/command_line.h"
8 #include "base/path_service.h"
9 #include "base/version.h"
10 #include "chrome/browser/app_mode/app_mode_utils.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/extensions/activity_log/activity_log.h"
13 #include "chrome/browser/extensions/api/chrome_extensions_api_client.h"
14 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
15 #include "chrome/browser/extensions/api/generated_api_registration.h"
16 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h"
17 #include "chrome/browser/extensions/api/preference/preference_api.h"
18 #include "chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.h"
19 #include "chrome/browser/extensions/chrome_app_sorting.h"
20 #include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
21 #include "chrome/browser/extensions/chrome_extension_host_delegate.h"
22 #include "chrome/browser/extensions/chrome_process_manager_delegate.h"
23 #include "chrome/browser/extensions/chrome_url_request_util.h"
24 #include "chrome/browser/extensions/event_router_forwarder.h"
25 #include "chrome/browser/extensions/extension_system_factory.h"
26 #include "chrome/browser/extensions/extension_util.h"
27 #include "chrome/browser/external_protocol/external_protocol_handler.h"
28 #include "chrome/browser/net/chrome_net_log.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/chrome_version_info.h"
34 #include "chrome/common/extensions/features/feature_channel.h"
35 #include "chrome/common/pref_names.h"
36 #include "extensions/browser/api/generated_api_registration.h"
37 #include "extensions/browser/extension_function_registry.h"
38 #include "extensions/browser/extension_prefs.h"
39 #include "extensions/browser/pref_names.h"
40 #include "extensions/browser/url_request_util.h"
41 
42 #if defined(OS_CHROMEOS)
43 #include "chromeos/chromeos_switches.h"
44 #endif
45 
46 namespace extensions {
47 
ChromeExtensionsBrowserClient()48 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
49   process_manager_delegate_.reset(new ChromeProcessManagerDelegate);
50   api_client_.reset(new ChromeExtensionsAPIClient);
51   // Only set if it hasn't already been set (e.g. by a test).
52   if (GetCurrentChannel() == GetDefaultChannel())
53     SetCurrentChannel(chrome::VersionInfo::GetChannel());
54 }
55 
~ChromeExtensionsBrowserClient()56 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
57 
IsShuttingDown()58 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
59   return g_browser_process->IsShuttingDown();
60 }
61 
AreExtensionsDisabled(const CommandLine & command_line,content::BrowserContext * context)62 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
63     const CommandLine& command_line,
64     content::BrowserContext* context) {
65   Profile* profile = static_cast<Profile*>(context);
66   return command_line.HasSwitch(switches::kDisableExtensions) ||
67       profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions);
68 }
69 
IsValidContext(content::BrowserContext * context)70 bool ChromeExtensionsBrowserClient::IsValidContext(
71     content::BrowserContext* context) {
72   Profile* profile = static_cast<Profile*>(context);
73   return g_browser_process->profile_manager()->IsValidProfile(profile);
74 }
75 
IsSameContext(content::BrowserContext * first,content::BrowserContext * second)76 bool ChromeExtensionsBrowserClient::IsSameContext(
77     content::BrowserContext* first,
78     content::BrowserContext* second) {
79   return static_cast<Profile*>(first)->IsSameProfile(
80       static_cast<Profile*>(second));
81 }
82 
HasOffTheRecordContext(content::BrowserContext * context)83 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
84     content::BrowserContext* context) {
85   return static_cast<Profile*>(context)->HasOffTheRecordProfile();
86 }
87 
GetOffTheRecordContext(content::BrowserContext * context)88 content::BrowserContext* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
89     content::BrowserContext* context) {
90   return static_cast<Profile*>(context)->GetOffTheRecordProfile();
91 }
92 
GetOriginalContext(content::BrowserContext * context)93 content::BrowserContext* ChromeExtensionsBrowserClient::GetOriginalContext(
94     content::BrowserContext* context) {
95   return static_cast<Profile*>(context)->GetOriginalProfile();
96 }
97 
IsGuestSession(content::BrowserContext * context) const98 bool ChromeExtensionsBrowserClient::IsGuestSession(
99     content::BrowserContext* context) const {
100   return static_cast<Profile*>(context)->IsGuestSession();
101 }
102 
IsExtensionIncognitoEnabled(const std::string & extension_id,content::BrowserContext * context) const103 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
104     const std::string& extension_id,
105     content::BrowserContext* context) const {
106   return IsGuestSession(context)
107       || util::IsIncognitoEnabled(extension_id, context);
108 }
109 
CanExtensionCrossIncognito(const extensions::Extension * extension,content::BrowserContext * context) const110 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
111     const extensions::Extension* extension,
112     content::BrowserContext* context) const {
113   return IsGuestSession(context)
114       || util::CanCrossIncognito(extension, context);
115 }
116 
117 net::URLRequestJob*
MaybeCreateResourceBundleRequestJob(net::URLRequest * request,net::NetworkDelegate * network_delegate,const base::FilePath & directory_path,const std::string & content_security_policy,bool send_cors_header)118 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
119     net::URLRequest* request,
120     net::NetworkDelegate* network_delegate,
121     const base::FilePath& directory_path,
122     const std::string& content_security_policy,
123     bool send_cors_header) {
124   return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
125       request,
126       network_delegate,
127       directory_path,
128       content_security_policy,
129       send_cors_header);
130 }
131 
AllowCrossRendererResourceLoad(net::URLRequest * request,bool is_incognito,const Extension * extension,InfoMap * extension_info_map)132 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
133     net::URLRequest* request,
134     bool is_incognito,
135     const Extension* extension,
136     InfoMap* extension_info_map) {
137   bool allowed = false;
138   if (chrome_url_request_util::AllowCrossRendererResourceLoad(
139           request, is_incognito, extension, extension_info_map, &allowed))
140     return allowed;
141 
142   // Couldn't determine if resource is allowed. Block the load.
143   return false;
144 }
145 
GetPrefServiceForContext(content::BrowserContext * context)146 PrefService* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
147     content::BrowserContext* context) {
148   return static_cast<Profile*>(context)->GetPrefs();
149 }
150 
GetEarlyExtensionPrefsObservers(content::BrowserContext * context,std::vector<ExtensionPrefsObserver * > * observers) const151 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
152     content::BrowserContext* context,
153     std::vector<ExtensionPrefsObserver*>* observers) const {
154   observers->push_back(ContentSettingsService::Get(context));
155 }
156 
157 ProcessManagerDelegate*
GetProcessManagerDelegate() const158 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
159   return process_manager_delegate_.get();
160 }
161 
162 scoped_ptr<ExtensionHostDelegate>
CreateExtensionHostDelegate()163 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
164   return scoped_ptr<ExtensionHostDelegate>(new ChromeExtensionHostDelegate);
165 }
166 
DidVersionUpdate(content::BrowserContext * context)167 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
168     content::BrowserContext* context) {
169   Profile* profile = static_cast<Profile*>(context);
170 
171   // Unit tests may not provide prefs; assume everything is up-to-date.
172   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile);
173   if (!extension_prefs)
174     return false;
175 
176   // If we're inside a browser test, then assume prefs are all up-to-date.
177   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
178     return false;
179 
180   PrefService* pref_service = extension_prefs->pref_service();
181   base::Version last_version;
182   if (pref_service->HasPrefPath(pref_names::kLastChromeVersion)) {
183     std::string last_version_str =
184         pref_service->GetString(pref_names::kLastChromeVersion);
185     last_version = base::Version(last_version_str);
186   }
187 
188   chrome::VersionInfo current_version_info;
189   std::string current_version = current_version_info.Version();
190   pref_service->SetString(pref_names::kLastChromeVersion,
191                           current_version);
192 
193   // If there was no version string in prefs, assume we're out of date.
194   if (!last_version.IsValid())
195     return true;
196 
197   return last_version.IsOlderThan(current_version);
198 }
199 
PermitExternalProtocolHandler()200 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
201   ExternalProtocolHandler::PermitLaunchUrl();
202 }
203 
CreateAppSorting()204 scoped_ptr<AppSorting> ChromeExtensionsBrowserClient::CreateAppSorting() {
205   return scoped_ptr<AppSorting>(new ChromeAppSorting());
206 }
207 
IsRunningInForcedAppMode()208 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
209   return chrome::IsRunningInForcedAppMode();
210 }
211 
GetApiActivityMonitor(content::BrowserContext * context)212 ApiActivityMonitor* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
213     content::BrowserContext* context) {
214   // The ActivityLog monitors and records function calls and events.
215   return ActivityLog::GetInstance(context);
216 }
217 
218 ExtensionSystemProvider*
GetExtensionSystemFactory()219 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
220   return ExtensionSystemFactory::GetInstance();
221 }
222 
RegisterExtensionFunctions(ExtensionFunctionRegistry * registry) const223 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
224     ExtensionFunctionRegistry* registry) const {
225   // Preferences.
226   registry->RegisterFunction<extensions::GetPreferenceFunction>();
227   registry->RegisterFunction<extensions::SetPreferenceFunction>();
228   registry->RegisterFunction<extensions::ClearPreferenceFunction>();
229 
230   // Direct Preference Access for Component Extensions.
231   registry->RegisterFunction<
232       extensions::chromedirectsetting::GetDirectSettingFunction>();
233   registry->RegisterFunction<
234       extensions::chromedirectsetting::SetDirectSettingFunction>();
235   registry->RegisterFunction<
236       extensions::chromedirectsetting::ClearDirectSettingFunction>();
237 
238   // Generated APIs from lower-level modules.
239   extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry);
240 
241   // Generated APIs from Chrome.
242   extensions::api::GeneratedFunctionRegistry::RegisterAll(registry);
243 }
244 
245 ComponentExtensionResourceManager*
GetComponentExtensionResourceManager()246 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
247   if (!resource_manager_)
248     resource_manager_.reset(new ChromeComponentExtensionResourceManager());
249   return resource_manager_.get();
250 }
251 
BroadcastEventToRenderers(const std::string & event_name,scoped_ptr<base::ListValue> args)252 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
253     const std::string& event_name,
254     scoped_ptr<base::ListValue> args) {
255   g_browser_process->extension_event_router_forwarder()
256       ->BroadcastEventToRenderers(event_name, args.Pass(), GURL());
257 }
258 
GetNetLog()259 net::NetLog* ChromeExtensionsBrowserClient::GetNetLog() {
260   return g_browser_process->net_log();
261 }
262 
263 scoped_ptr<extensions::RuntimeAPIDelegate>
CreateRuntimeAPIDelegate(content::BrowserContext * context) const264 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
265     content::BrowserContext* context) const {
266   return scoped_ptr<extensions::RuntimeAPIDelegate>(
267       new ChromeRuntimeAPIDelegate(context));
268 }
269 
270 }  // namespace extensions
271