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.
5 #include "chrome/browser/extensions/chrome_extensions_browser_client.h"
7 #include "base/command_line.h"
8 #include "base/version.h"
9 #include "chrome/browser/app_mode/app_mode_utils.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/activity_log/activity_log.h"
12 #include "chrome/browser/extensions/chrome_app_sorting.h"
13 #include "chrome/browser/extensions/extension_host.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system_factory.h"
16 #include "chrome/browser/extensions/extension_util.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
20 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/chrome_version_info.h"
24 #include "chrome/common/extensions/features/feature_channel.h"
25 #include "chrome/common/pref_names.h"
26 #include "extensions/browser/extension_prefs.h"
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/browser/pref_names.h"
30 #if defined(OS_CHROMEOS)
31 #include "chromeos/chromeos_switches.h"
34 namespace extensions
{
36 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
37 // Only set if it hasn't already been set (e.g. by a test).
38 if (GetCurrentChannel() == GetDefaultChannel())
39 SetCurrentChannel(chrome::VersionInfo::GetChannel());
42 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
44 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
45 return g_browser_process
->IsShuttingDown();
48 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
49 const CommandLine
& command_line
,
50 content::BrowserContext
* context
) {
51 Profile
* profile
= static_cast<Profile
*>(context
);
52 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
53 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
56 bool ChromeExtensionsBrowserClient::IsValidContext(
57 content::BrowserContext
* context
) {
58 Profile
* profile
= static_cast<Profile
*>(context
);
59 return g_browser_process
->profile_manager()->IsValidProfile(profile
);
62 bool ChromeExtensionsBrowserClient::IsSameContext(
63 content::BrowserContext
* first
,
64 content::BrowserContext
* second
) {
65 return static_cast<Profile
*>(first
)->IsSameProfile(
66 static_cast<Profile
*>(second
));
69 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
70 content::BrowserContext
* context
) {
71 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
74 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
75 content::BrowserContext
* context
) {
76 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
79 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
80 content::BrowserContext
* context
) {
81 return static_cast<Profile
*>(context
)->GetOriginalProfile();
84 bool ChromeExtensionsBrowserClient::IsGuestSession(
85 content::BrowserContext
* context
) {
86 return static_cast<Profile
*>(context
)->IsGuestSession();
89 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
90 const std::string
& extension_id
,
91 content::BrowserContext
* context
) const {
92 return util::IsIncognitoEnabled(extension_id
, context
);
95 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
96 const extensions::Extension
* extension
,
97 content::BrowserContext
* context
) const {
98 return util::CanCrossIncognito(extension
, context
);
101 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
102 content::BrowserContext
* context
) {
103 return static_cast<Profile
*>(context
)->GetPrefs();
106 bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
107 content::BrowserContext
* context
) const {
108 Profile
* profile
= static_cast<Profile
*>(context
);
110 // The profile may not be valid yet if it is still being initialized.
111 // In that case, defer loading, since it depends on an initialized profile.
112 // http://crbug.com/222473
113 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
))
116 #if defined(OS_ANDROID)
119 // There are no browser windows open and the browser process was
120 // started to show the app launcher.
121 return chrome::GetTotalBrowserCountForProfile(profile
) == 0 &&
122 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList
);
126 bool ChromeExtensionsBrowserClient::IsBackgroundPageAllowed(
127 content::BrowserContext
* context
) const {
128 #if defined(OS_CHROMEOS)
129 // Returns true if current session is Chrome OS Guest mode session and current
130 // browser context is *not* off-the-record. Such context is artificial and
131 // background page shouldn't be created in it.
132 const CommandLine
* command_line
= CommandLine::ForCurrentProcess();
133 if (command_line
->HasSwitch(chromeos::switches::kGuestSession
) &&
134 !context
->IsOffTheRecord()) {
141 void ChromeExtensionsBrowserClient::OnExtensionHostCreated(
142 content::WebContents
* web_contents
) {
143 PrefsTabHelper::CreateForWebContents(web_contents
);
146 void ChromeExtensionsBrowserClient::OnRenderViewCreatedForBackgroundPage(
147 ExtensionHost
* host
) {
148 ExtensionService
* service
=
149 ExtensionSystem::Get(host
->browser_context())->extension_service();
151 service
->DidCreateRenderViewForBackgroundPage(host
);
154 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
155 content::BrowserContext
* context
) {
156 Profile
* profile
= static_cast<Profile
*>(context
);
158 // Unit tests may not provide prefs; assume everything is up-to-date.
159 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
160 if (!extension_prefs
)
163 // If we're inside a browser test, then assume prefs are all up-to-date.
164 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
167 PrefService
* pref_service
= extension_prefs
->pref_service();
168 base::Version last_version
;
169 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
170 std::string last_version_str
=
171 pref_service
->GetString(pref_names::kLastChromeVersion
);
172 last_version
= base::Version(last_version_str
);
175 chrome::VersionInfo current_version_info
;
176 std::string current_version
= current_version_info
.Version();
177 pref_service
->SetString(pref_names::kLastChromeVersion
,
180 // If there was no version string in prefs, assume we're out of date.
181 if (!last_version
.IsValid())
184 return last_version
.IsOlderThan(current_version
);
187 scoped_ptr
<AppSorting
> ChromeExtensionsBrowserClient::CreateAppSorting() {
188 return scoped_ptr
<AppSorting
>(new ChromeAppSorting()).Pass();
191 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
192 return chrome::IsRunningInForcedAppMode();
195 content::JavaScriptDialogManager
*
196 ChromeExtensionsBrowserClient::GetJavaScriptDialogManager() {
197 return GetJavaScriptDialogManagerInstance();
200 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
201 content::BrowserContext
* context
) {
202 // The ActivityLog monitors and records function calls and events.
203 return ActivityLog::GetInstance(context
);
206 ExtensionSystemProvider
*
207 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
208 return ExtensionSystemFactory::GetInstance();
211 } // namespace extensions