Home | History | Annotate | Download | only in extensions
      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/chrome_app_sorting.h"
     12 #include "chrome/browser/extensions/extension_prefs.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #include "chrome/browser/profiles/profile_manager.h"
     15 #include "chrome/browser/ui/app_modal_dialogs/javascript_dialog_manager.h"
     16 #include "chrome/browser/ui/browser_finder.h"
     17 #include "chrome/common/chrome_switches.h"
     18 #include "chrome/common/chrome_version_info.h"
     19 #include "chrome/common/pref_names.h"
     20 
     21 #if defined(OS_CHROMEOS)
     22 #include "chromeos/chromeos_switches.h"
     23 #endif
     24 
     25 namespace extensions {
     26 
     27 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {}
     28 
     29 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
     30 
     31 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
     32   return g_browser_process->IsShuttingDown();
     33 }
     34 
     35 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
     36     const CommandLine& command_line,
     37     content::BrowserContext* context) {
     38   Profile* profile = static_cast<Profile*>(context);
     39   return command_line.HasSwitch(switches::kDisableExtensions) ||
     40       profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions);
     41 }
     42 
     43 bool ChromeExtensionsBrowserClient::IsValidContext(
     44     content::BrowserContext* context) {
     45   Profile* profile = static_cast<Profile*>(context);
     46   return g_browser_process->profile_manager()->IsValidProfile(profile);
     47 }
     48 
     49 bool ChromeExtensionsBrowserClient::IsSameContext(
     50     content::BrowserContext* first,
     51     content::BrowserContext* second) {
     52   return static_cast<Profile*>(first)->IsSameProfile(
     53       static_cast<Profile*>(second));
     54 }
     55 
     56 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
     57     content::BrowserContext* context) {
     58   return static_cast<Profile*>(context)->HasOffTheRecordProfile();
     59 }
     60 
     61 content::BrowserContext* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
     62     content::BrowserContext* context) {
     63   return static_cast<Profile*>(context)->GetOffTheRecordProfile();
     64 }
     65 
     66 content::BrowserContext* ChromeExtensionsBrowserClient::GetOriginalContext(
     67     content::BrowserContext* context) {
     68   return static_cast<Profile*>(context)->GetOriginalProfile();
     69 }
     70 
     71 PrefService* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
     72     content::BrowserContext* context) {
     73   return static_cast<Profile*>(context)->GetPrefs();
     74 }
     75 
     76 bool ChromeExtensionsBrowserClient::DeferLoadingBackgroundHosts(
     77     content::BrowserContext* context) const {
     78   Profile* profile = static_cast<Profile*>(context);
     79 
     80   // The profile may not be valid yet if it is still being initialized.
     81   // In that case, defer loading, since it depends on an initialized profile.
     82   // http://crbug.com/222473
     83   if (!g_browser_process->profile_manager()->IsValidProfile(profile))
     84     return true;
     85 
     86 #if defined(OS_ANDROID)
     87   return false;
     88 #else
     89   // There are no browser windows open and the browser process was
     90   // started to show the app launcher.
     91   return chrome::GetTotalBrowserCountForProfile(profile) == 0 &&
     92          CommandLine::ForCurrentProcess()->HasSwitch(switches::kShowAppList);
     93 #endif
     94 }
     95 
     96 bool ChromeExtensionsBrowserClient::IsBackgroundPageAllowed(
     97     content::BrowserContext* context) const {
     98 #if defined(OS_CHROMEOS)
     99   // Returns true if current session is Chrome OS Guest mode session and current
    100   // browser context is *not* off-the-record. Such context is artificial and
    101   // background page shouldn't be created in it.
    102   const CommandLine* command_line = CommandLine::ForCurrentProcess();
    103   if (command_line->HasSwitch(chromeos::switches::kGuestSession) &&
    104       !context->IsOffTheRecord()) {
    105     return false;
    106   }
    107 #endif
    108   return true;
    109 }
    110 
    111 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
    112     content::BrowserContext* context) {
    113   Profile* profile = static_cast<Profile*>(context);
    114 
    115   // Unit tests may not provide prefs; assume everything is up-to-date.
    116   ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile);
    117   if (!extension_prefs)
    118     return false;
    119 
    120   // If we're inside a browser test, then assume prefs are all up-to-date.
    121   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
    122     return false;
    123 
    124   PrefService* pref_service = extension_prefs->pref_service();
    125   base::Version last_version;
    126   if (pref_service->HasPrefPath(prefs::kExtensionsLastChromeVersion)) {
    127     std::string last_version_str =
    128         pref_service->GetString(prefs::kExtensionsLastChromeVersion);
    129     last_version = base::Version(last_version_str);
    130   }
    131 
    132   chrome::VersionInfo current_version_info;
    133   std::string current_version = current_version_info.Version();
    134   pref_service->SetString(prefs::kExtensionsLastChromeVersion,
    135                           current_version);
    136 
    137   // If there was no version string in prefs, assume we're out of date.
    138   if (!last_version.IsValid())
    139     return true;
    140 
    141   return last_version.IsOlderThan(current_version);
    142 }
    143 
    144 scoped_ptr<AppSorting> ChromeExtensionsBrowserClient::CreateAppSorting() {
    145   return scoped_ptr<AppSorting>(new ChromeAppSorting()).Pass();
    146 }
    147 
    148 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
    149   return chrome::IsRunningInForcedAppMode();
    150 }
    151 
    152 content::JavaScriptDialogManager*
    153 ChromeExtensionsBrowserClient::GetJavaScriptDialogManager() {
    154   return GetJavaScriptDialogManagerInstance();
    155 }
    156 
    157 }  // namespace extensions
    158