Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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/test_extension_system.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "chrome/browser/extensions/blacklist.h"
     10 #include "chrome/browser/extensions/error_console/error_console.h"
     11 #include "chrome/browser/extensions/event_router.h"
     12 #include "chrome/browser/extensions/extension_info_map.h"
     13 #include "chrome/browser/extensions/extension_pref_value_map.h"
     14 #include "chrome/browser/extensions/extension_pref_value_map_factory.h"
     15 #include "chrome/browser/extensions/extension_prefs.h"
     16 #include "chrome/browser/extensions/extension_prefs_factory.h"
     17 #include "chrome/browser/extensions/extension_process_manager.h"
     18 #include "chrome/browser/extensions/extension_service.h"
     19 #include "chrome/browser/extensions/extension_system.h"
     20 #include "chrome/browser/extensions/management_policy.h"
     21 #include "chrome/browser/extensions/standard_management_policy_provider.h"
     22 #include "chrome/browser/extensions/state_store.h"
     23 #include "chrome/browser/extensions/user_script_master.h"
     24 #include "chrome/browser/profiles/profile.h"
     25 #include "chrome/browser/value_store/testing_value_store.h"
     26 #include "chrome/common/chrome_switches.h"
     27 #include "content/public/browser/browser_thread.h"
     28 
     29 using content::BrowserThread;
     30 
     31 namespace extensions {
     32 
     33 TestExtensionSystem::TestExtensionSystem(Profile* profile)
     34     : profile_(profile),
     35       value_store_(NULL),
     36       info_map_(new ExtensionInfoMap()),
     37       error_console_(new ErrorConsole(profile)) {
     38 }
     39 
     40 TestExtensionSystem::~TestExtensionSystem() {
     41 }
     42 
     43 void TestExtensionSystem::Shutdown() {
     44   extension_process_manager_.reset();
     45 }
     46 
     47 void TestExtensionSystem::CreateExtensionProcessManager() {
     48   extension_process_manager_.reset(ExtensionProcessManager::Create(profile_));
     49 }
     50 
     51 ExtensionPrefs* TestExtensionSystem::CreateExtensionPrefs(
     52     const CommandLine* command_line,
     53     const base::FilePath& install_directory) {
     54   bool extensions_disabled =
     55       command_line && command_line->HasSwitch(switches::kDisableExtensions);
     56 
     57   // Note that the GetPrefs() creates a TestingPrefService, therefore
     58   // the extension controlled pref values set in ExtensionPrefs
     59   // are not reflected in the pref service. One would need to
     60   // inject a new ExtensionPrefStore(extension_pref_value_map, false).
     61 
     62   ExtensionPrefs* extension_prefs = ExtensionPrefs::Create(
     63       profile_->GetPrefs(),
     64       install_directory,
     65       ExtensionPrefValueMapFactory::GetForProfile(profile_),
     66       extensions_disabled);
     67     ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
     68         profile_,
     69         extension_prefs);
     70     return extension_prefs;
     71 }
     72 
     73 ExtensionService* TestExtensionSystem::CreateExtensionService(
     74     const CommandLine* command_line,
     75     const base::FilePath& install_directory,
     76     bool autoupdate_enabled) {
     77   if (!ExtensionPrefs::Get(profile_))
     78     CreateExtensionPrefs(command_line, install_directory);
     79   // The ownership of |value_store_| is immediately transferred to state_store_,
     80   // but we keep a naked pointer to the TestingValueStore.
     81   value_store_ = new TestingValueStore();
     82   state_store_.reset(new StateStore(profile_, value_store_));
     83   blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
     84   standard_management_policy_provider_.reset(
     85       new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
     86   management_policy_.reset(new ManagementPolicy());
     87   management_policy_->RegisterProvider(
     88       standard_management_policy_provider_.get());
     89   extension_service_.reset(new ExtensionService(profile_,
     90                                                 command_line,
     91                                                 install_directory,
     92                                                 ExtensionPrefs::Get(profile_),
     93                                                 blacklist_.get(),
     94                                                 autoupdate_enabled,
     95                                                 true,
     96                                                 &ready_));
     97   extension_service_->ClearProvidersForTesting();
     98   return extension_service_.get();
     99 }
    100 
    101 ExtensionService* TestExtensionSystem::extension_service() {
    102   return extension_service_.get();
    103 }
    104 
    105 ManagementPolicy* TestExtensionSystem::management_policy() {
    106   return management_policy_.get();
    107 }
    108 
    109 void TestExtensionSystem::SetExtensionService(ExtensionService* service) {
    110   extension_service_.reset(service);
    111 }
    112 
    113 UserScriptMaster* TestExtensionSystem::user_script_master() {
    114   return NULL;
    115 }
    116 
    117 ExtensionProcessManager* TestExtensionSystem::process_manager() {
    118   return extension_process_manager_.get();
    119 }
    120 
    121 StateStore* TestExtensionSystem::state_store() {
    122   return state_store_.get();
    123 }
    124 
    125 StateStore* TestExtensionSystem::rules_store() {
    126   return state_store_.get();
    127 }
    128 
    129 ExtensionInfoMap* TestExtensionSystem::info_map() {
    130   return info_map_.get();
    131 }
    132 
    133 LazyBackgroundTaskQueue*
    134 TestExtensionSystem::lazy_background_task_queue() {
    135   return NULL;
    136 }
    137 
    138 EventRouter* TestExtensionSystem::event_router() {
    139   return NULL;
    140 }
    141 
    142 ExtensionWarningService* TestExtensionSystem::warning_service() {
    143   return NULL;
    144 }
    145 
    146 Blacklist* TestExtensionSystem::blacklist() {
    147   return blacklist_.get();
    148 }
    149 
    150 const OneShotEvent& TestExtensionSystem::ready() const {
    151   return ready_;
    152 }
    153 
    154 ErrorConsole* TestExtensionSystem::error_console() {
    155   return error_console_.get();
    156 }
    157 
    158 // static
    159 BrowserContextKeyedService* TestExtensionSystem::Build(
    160     content::BrowserContext* profile) {
    161   return new TestExtensionSystem(static_cast<Profile*>(profile));
    162 }
    163 
    164 }  // namespace extensions
    165