Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / chrome_extensions_browser_client.cc
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/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"
29
30 #if defined(OS_CHROMEOS)
31 #include "chromeos/chromeos_switches.h"
32 #endif
33
34 namespace extensions {
35
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());
40 }
41
42 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
43
44 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
45   return g_browser_process->IsShuttingDown();
46 }
47
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);
54 }
55
56 bool ChromeExtensionsBrowserClient::IsValidContext(
57     content::BrowserContext* context) {
58   Profile* profile = static_cast<Profile*>(context);
59   return g_browser_process->profile_manager()->IsValidProfile(profile);
60 }
61
62 bool ChromeExtensionsBrowserClient::IsSameContext(
63     content::BrowserContext* first,
64     content::BrowserContext* second) {
65   return static_cast<Profile*>(first)->IsSameProfile(
66       static_cast<Profile*>(second));
67 }
68
69 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
70     content::BrowserContext* context) {
71   return static_cast<Profile*>(context)->HasOffTheRecordProfile();
72 }
73
74 content::BrowserContext* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
75     content::BrowserContext* context) {
76   return static_cast<Profile*>(context)->GetOffTheRecordProfile();
77 }
78
79 content::BrowserContext* ChromeExtensionsBrowserClient::GetOriginalContext(
80     content::BrowserContext* context) {
81   return static_cast<Profile*>(context)->GetOriginalProfile();
82 }
83
84 bool ChromeExtensionsBrowserClient::IsGuestSession(
85     content::BrowserContext* context) {
86   return static_cast<Profile*>(context)->IsGuestSession();
87 }
88
89 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
90     const std::string& extension_id,
91     content::BrowserContext* context) const {
92   return util::IsIncognitoEnabled(extension_id, context);
93 }
94
95 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
96     const extensions::Extension* extension,
97     content::BrowserContext* context) const {
98   return util::CanCrossIncognito(extension, context);
99 }
100
101 PrefService* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
102     content::BrowserContext* context) {
103   return static_cast<Profile*>(context)->GetPrefs();
104 }
105
106 bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
107     content::BrowserContext* context) const {
108   Profile* profile = static_cast<Profile*>(context);
109
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))
114     return true;
115
116 #if defined(OS_ANDROID)
117   return false;
118 #else
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);
123 #endif
124 }
125
126 bool ChromeExtensionsBrowserClient::IsBackgroundPageAllowed(
127     content::BrowserContext* context) const {
128   // Returns true if current session is Guest mode session and current
129   // browser context is *not* off-the-record. Such context is artificial and
130   // background page shouldn't be created in it.
131   return !static_cast<Profile*>(context)->IsGuestSession() ||
132          context->IsOffTheRecord();
133 }
134
135 void ChromeExtensionsBrowserClient::OnExtensionHostCreated(
136     content::WebContents* web_contents) {
137   PrefsTabHelper::CreateForWebContents(web_contents);
138 }
139
140 void ChromeExtensionsBrowserClient::OnRenderViewCreatedForBackgroundPage(
141     ExtensionHost* host) {
142   ExtensionService* service =
143       ExtensionSystem::Get(host->browser_context())->extension_service();
144   if (service)
145     service->DidCreateRenderViewForBackgroundPage(host);
146 }
147
148 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
149     content::BrowserContext* context) {
150   Profile* profile = static_cast<Profile*>(context);
151
152   // Unit tests may not provide prefs; assume everything is up-to-date.
153   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile);
154   if (!extension_prefs)
155     return false;
156
157   // If we're inside a browser test, then assume prefs are all up-to-date.
158   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
159     return false;
160
161   PrefService* pref_service = extension_prefs->pref_service();
162   base::Version last_version;
163   if (pref_service->HasPrefPath(pref_names::kLastChromeVersion)) {
164     std::string last_version_str =
165         pref_service->GetString(pref_names::kLastChromeVersion);
166     last_version = base::Version(last_version_str);
167   }
168
169   chrome::VersionInfo current_version_info;
170   std::string current_version = current_version_info.Version();
171   pref_service->SetString(pref_names::kLastChromeVersion,
172                           current_version);
173
174   // If there was no version string in prefs, assume we're out of date.
175   if (!last_version.IsValid())
176     return true;
177
178   return last_version.IsOlderThan(current_version);
179 }
180
181 scoped_ptr<AppSorting> ChromeExtensionsBrowserClient::CreateAppSorting() {
182   return scoped_ptr<AppSorting>(new ChromeAppSorting()).Pass();
183 }
184
185 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
186   return chrome::IsRunningInForcedAppMode();
187 }
188
189 content::JavaScriptDialogManager*
190 ChromeExtensionsBrowserClient::GetJavaScriptDialogManager() {
191   return GetJavaScriptDialogManagerInstance();
192 }
193
194 ApiActivityMonitor* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
195     content::BrowserContext* context) {
196   // The ActivityLog monitors and records function calls and events.
197   return ActivityLog::GetInstance(context);
198 }
199
200 ExtensionSystemProvider*
201 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
202   return ExtensionSystemFactory::GetInstance();
203 }
204
205 }  // namespace extensions