Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_extensions_browser_client.cc
blob5ed24955ffa598af80931afecb8a1d539ad10cb0
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/chrome_app_sorting.h"
12 #include "chrome/browser/extensions/extension_prefs.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/extensions/extension_system_factory.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
18 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/pref_names.h"
23 #include "extensions/browser/pref_names.h"
25 #if defined(OS_CHROMEOS)
26 #include "chromeos/chromeos_switches.h"
27 #endif
29 namespace extensions {
31 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {}
33 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
35 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
36 return g_browser_process->IsShuttingDown();
39 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
40 const CommandLine& command_line,
41 content::BrowserContext* context) {
42 Profile* profile = static_cast<Profile*>(context);
43 return command_line.HasSwitch(switches::kDisableExtensions) ||
44 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions);
47 bool ChromeExtensionsBrowserClient::IsValidContext(
48 content::BrowserContext* context) {
49 Profile* profile = static_cast<Profile*>(context);
50 return g_browser_process->profile_manager()->IsValidProfile(profile);
53 bool ChromeExtensionsBrowserClient::IsSameContext(
54 content::BrowserContext* first,
55 content::BrowserContext* second) {
56 return static_cast<Profile*>(first)->IsSameProfile(
57 static_cast<Profile*>(second));
60 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
61 content::BrowserContext* context) {
62 return static_cast<Profile*>(context)->HasOffTheRecordProfile();
65 content::BrowserContext* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
66 content::BrowserContext* context) {
67 return static_cast<Profile*>(context)->GetOffTheRecordProfile();
70 content::BrowserContext* ChromeExtensionsBrowserClient::GetOriginalContext(
71 content::BrowserContext* context) {
72 return static_cast<Profile*>(context)->GetOriginalProfile();
75 PrefService* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
76 content::BrowserContext* context) {
77 return static_cast<Profile*>(context)->GetPrefs();
80 bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
81 content::BrowserContext* context) const {
82 Profile* profile = static_cast<Profile*>(context);
84 // The profile may not be valid yet if it is still being initialized.
85 // In that case, defer loading, since it depends on an initialized profile.
86 // http://crbug.com/222473
87 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
88 return true;
90 #if defined(OS_ANDROID)
91 return false;
92 #else
93 // There are no browser windows open and the browser process was
94 // started to show the app launcher.
95 return chrome::GetTotalBrowserCountForProfile(profile) == 0 &&
96 CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList);
97 #endif
100 bool ChromeExtensionsBrowserClient::IsBackgroundPageAllowed(
101 content::BrowserContext* context) const {
102 #if defined(OS_CHROMEOS)
103 // Returns true if current session is Chrome OS Guest mode session and current
104 // browser context is *not* off-the-record. Such context is artificial and
105 // background page shouldn't be created in it.
106 const CommandLine* command_line = CommandLine::ForCurrentProcess();
107 if (command_line->HasSwitch(chromeos::switches::kGuestSession) &&
108 !context->IsOffTheRecord()) {
109 return false;
111 #endif
112 return true;
115 void ChromeExtensionsBrowserClient::OnExtensionHostCreated(
116 content::WebContents* web_contents) {
117 PrefsTabHelper::CreateForWebContents(web_contents);
120 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
121 content::BrowserContext* context) {
122 Profile* profile = static_cast<Profile*>(context);
124 // Unit tests may not provide prefs; assume everything is up-to-date.
125 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile);
126 if (!extension_prefs)
127 return false;
129 // If we're inside a browser test, then assume prefs are all up-to-date.
130 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
131 return false;
133 PrefService* pref_service = extension_prefs->pref_service();
134 base::Version last_version;
135 if (pref_service->HasPrefPath(pref_names::kLastChromeVersion)) {
136 std::string last_version_str =
137 pref_service->GetString(pref_names::kLastChromeVersion);
138 last_version = base::Version(last_version_str);
141 chrome::VersionInfo current_version_info;
142 std::string current_version = current_version_info.Version();
143 pref_service->SetString(pref_names::kLastChromeVersion,
144 current_version);
146 // If there was no version string in prefs, assume we're out of date.
147 if (!last_version.IsValid())
148 return true;
150 return last_version.IsOlderThan(current_version);
153 scoped_ptr<AppSorting> ChromeExtensionsBrowserClient::CreateAppSorting() {
154 return scoped_ptr<AppSorting>(new ChromeAppSorting()).Pass();
157 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
158 return chrome::IsRunningInForcedAppMode();
161 content::JavaScriptDialogManager*
162 ChromeExtensionsBrowserClient::GetJavaScriptDialogManager() {
163 return GetJavaScriptDialogManagerInstance();
166 std::vector<BrowserContextKeyedServiceFactory*>
167 ChromeExtensionsBrowserClient::GetExtensionSystemDependencies() {
168 std::vector<BrowserContextKeyedServiceFactory*> dependencies;
169 dependencies.push_back(ExtensionSystemSharedFactory::GetInstance());
170 return dependencies;
173 ExtensionSystem* ChromeExtensionsBrowserClient::CreateExtensionSystem(
174 content::BrowserContext* context) {
175 return new ExtensionSystemImpl(static_cast<Profile*>(context));
178 } // namespace extensions