#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_H_
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/prefs/pref_change_registrar.h"
#include "base/strings/string16.h"
#include "chrome/browser/extensions/blacklist.h"
#include "chrome/browser/extensions/extension_sync_service.h"
#include "chrome/browser/extensions/pending_extension_manager.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/extension_function_histogram_value.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/external_provider_interface.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest.h"
#include "extensions/common/one_shot_event.h"
class ExtensionErrorUI;
class GURL;
class Profile;
namespace base {
class CommandLine;
class SequencedTaskRunner;
class Version;
}
namespace extensions {
class BrowserEventRouter;
class ComponentLoader;
class CrxInstaller;
class ExtensionActionStorageManager;
class ExtensionGarbageCollector;
class ExtensionRegistry;
class ExtensionSystem;
class ExtensionToolbarModel;
class ExtensionUpdater;
class PendingExtensionManager;
class RendererStartupHelper;
class SharedModuleService;
class UpdateObserver;
}
using extensions::ExtensionIdSet;
namespace syncer {
class SyncErrorFactory;
}
class ExtensionServiceInterface
: public base::SupportsWeakPtr<ExtensionServiceInterface> {
public:
virtual ~ExtensionServiceInterface() {}
virtual const extensions::ExtensionSet* extensions() const = 0;
virtual extensions::PendingExtensionManager* pending_extension_manager() = 0;
virtual bool UpdateExtension(
const std::string& id,
const base::FilePath& path,
bool file_ownership_passed,
const GURL& download_url,
extensions::CrxInstaller** out_crx_installer) = 0;
virtual const extensions::Extension* GetExtensionById(
const std::string& id,
bool include_disabled) const = 0;
virtual const extensions::Extension* GetInstalledExtension(
const std::string& id) const = 0;
virtual const extensions::Extension* GetPendingExtensionUpdate(
const std::string& extension_id) const = 0;
virtual void FinishDelayedInstallation(const std::string& extension_id) = 0;
virtual bool IsExtensionEnabled(const std::string& extension_id) const = 0;
virtual void CheckManagementPolicy() = 0;
virtual void CheckForUpdatesSoon() = 0;
virtual void AddExtension(const extensions::Extension* extension) = 0;
virtual void AddComponentExtension(
const extensions::Extension* extension) = 0;
virtual void UnloadExtension(
const std::string& extension_id,
extensions::UnloadedExtensionInfo::Reason reason) = 0;
virtual void RemoveComponentExtension(const std::string& extension_id) = 0;
virtual bool is_ready() = 0;
virtual base::SequencedTaskRunner* GetFileTaskRunner() = 0;
};
class ExtensionService
: public ExtensionServiceInterface,
public extensions::ExternalProviderInterface::VisitorInterface,
public content::NotificationObserver,
public extensions::Blacklist::Observer {
public:
const extensions::Extension* GetInstalledApp(const GURL& url) const;
bool IsInstalledApp(const GURL& url) const;
static bool UninstallExtensionHelper(ExtensionService* extensions_service,
const std::string& extension_id);
ExtensionService(Profile* profile,
const base::CommandLine* command_line,
const base::FilePath& install_directory,
extensions::ExtensionPrefs* extension_prefs,
extensions::Blacklist* blacklist,
bool autoupdate_enabled,
bool extensions_enabled,
extensions::OneShotEvent* ready);
virtual ~ExtensionService();
virtual const extensions::ExtensionSet* extensions() const OVERRIDE;
const extensions::ExtensionSet* delayed_installs() const;
virtual extensions::PendingExtensionManager*
pending_extension_manager() OVERRIDE;
const base::FilePath& install_directory() const { return install_directory_; }
bool IsBeingReloaded(const std::string& extension_id) const;
void SetBeingReloaded(const std::string& extension_id, bool value);
void Init();
void Shutdown();
virtual const extensions::Extension* GetExtensionById(
const std::string& id, bool include_disabled) const OVERRIDE;
virtual const extensions::Extension* GetInstalledExtension(
const std::string& id) const OVERRIDE;
virtual bool UpdateExtension(
const std::string& id,
const base::FilePath& extension_path,
bool file_ownership_passed,
const GURL& download_url,
extensions::CrxInstaller** out_crx_installer) OVERRIDE;
void ReloadExtension(const std::string extension_id);
virtual bool UninstallExtension(std::string extension_id,
bool external_uninstall,
base::string16* error);
virtual bool IsExtensionEnabled(
const std::string& extension_id) const OVERRIDE;
virtual void EnableExtension(const std::string& extension_id);
virtual void DisableExtension(const std::string& extension_id,
extensions::Extension::DisableReason disable_reason);
void DisableUserExtensions(const std::vector<std::string>& except_ids);
void GrantPermissionsAndEnableExtension(
const extensions::Extension* extension);
void GrantPermissions(
const extensions::Extension* extension);
void CheckForExternalUpdates();
virtual void UnloadExtension(
const std::string& extension_id,
extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
virtual void RemoveComponentExtension(const std::string& extension_id)
OVERRIDE;
void UnloadAllExtensionsForTest();
void ReloadExtensionsForTest();
virtual void OnLoadedInstalledExtensions();
virtual void AddExtension(const extensions::Extension* extension) OVERRIDE;
virtual void AddComponentExtension(const extensions::Extension* extension)
OVERRIDE;
void OnExtensionInstalled(
const extensions::Extension* extension,
const syncer::StringOrdinal& page_ordinal,
bool has_requirement_errors,
extensions::BlacklistState blacklist_state,
bool wait_for_idle);
void MaybeFinishDelayedInstallations();
void MaybeFinishDelayedInstallation(const std::string& extension_id);
virtual void FinishDelayedInstallation(
const std::string& extension_id) OVERRIDE;
virtual const extensions::Extension* GetPendingExtensionUpdate(
const std::string& extension_id) const OVERRIDE;
virtual void CheckManagementPolicy() OVERRIDE;
virtual void CheckForUpdatesSoon() OVERRIDE;
void set_extensions_enabled(bool enabled) { extensions_enabled_ = enabled; }
bool extensions_enabled() { return extensions_enabled_; }
void set_show_extensions_prompts(bool enabled) {
show_extensions_prompts_ = enabled;
}
bool show_extensions_prompts() {
return show_extensions_prompts_;
}
Profile* profile();
content::BrowserContext* GetBrowserContext() const;
void set_extension_sync_service(
ExtensionSyncService* extension_sync_service) {
extension_sync_service_ = extension_sync_service;
}
virtual bool is_ready() OVERRIDE;
virtual base::SequencedTaskRunner* GetFileTaskRunner() OVERRIDE;
extensions::ComponentLoader* component_loader() {
return component_loader_.get();
}
extensions::ExtensionUpdater* updater();
void ReportExtensionLoadError(const base::FilePath& extension_path,
const std::string& error,
bool be_noisy);
void NotifyLoadRetry(bool retry, const base::FilePath& extension_path);
void DidCreateRenderViewForBackgroundPage(extensions::ExtensionHost* host);
void CheckExternalUninstall(const std::string& id);
void SetFileTaskRunnerForTesting(base::SequencedTaskRunner* task_runner);
void ClearProvidersForTesting();
void AddProviderForTesting(
extensions::ExternalProviderInterface* test_provider);
virtual bool OnExternalExtensionFileFound(
const std::string& id,
const base::Version* version,
const base::FilePath& path,
extensions::Manifest::Location location,
int creation_flags,
bool mark_acknowledged) OVERRIDE;
virtual bool OnExternalExtensionUpdateUrlFound(
const std::string& id,
const std::string& install_parameter,
const GURL& update_url,
extensions::Manifest::Location location,
int creation_flags,
bool mark_acknowledged) OVERRIDE;
virtual void OnExternalProviderReady(
const extensions::ExternalProviderInterface* provider) OVERRIDE;
bool AreAllExternalProvidersReady() const;
void OnAllExternalProvidersReady();
void IdentifyAlertableExtensions();
bool PopulateExtensionErrorUI(ExtensionErrorUI* extension_error_ui);
void UpdateExternalExtensionAlert();
void AcknowledgeExternalExtension(const std::string& id);
bool IsUnacknowledgedExternalExtension(
const extensions::Extension* extension);
void ReconcileKnownDisabled();
void HandleExtensionAlertDetails();
void HandleExtensionAlertClosed();
void HandleExtensionAlertAccept();
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
void OnGarbageCollectIsolatedStorageStart();
void OnGarbageCollectIsolatedStorageFinished();
static void RecordPermissionMessagesHistogram(
const extensions::Extension* e, const char* histogram);
#if defined(UNIT_TEST)
void TrackTerminatedExtensionForTest(const extensions::Extension* extension) {
TrackTerminatedExtension(extension);
}
void FinishInstallationForTest(const extensions::Extension* extension) {
FinishInstallation(extension);
}
#endif
base::WeakPtr<ExtensionService> AsWeakPtr() { return base::AsWeakPtr(this); }
extensions::ExtensionGarbageCollector* garbage_collector() {
return garbage_collector_.get();
}
bool browser_terminating() const { return browser_terminating_; }
extensions::SharedModuleService* shared_module_service() {
return shared_module_service_.get();
}
void set_browser_terminating_for_test(bool value) {
browser_terminating_ = value;
}
void set_install_updates_when_idle_for_test(bool value) {
install_updates_when_idle_ = value;
}
void set_external_updates_finished_callback_for_test(
const base::Closure& callback) {
external_updates_finished_callback_ = callback;
}
void AddUpdateObserver(extensions::UpdateObserver* observer);
void RemoveUpdateObserver(extensions::UpdateObserver* observer);
private:
void LoadGreylistFromPrefs();
void SetReadyAndNotifyListeners();
void OnExtensionInstallPrefChanged();
void TrackTerminatedExtension(const extensions::Extension* extension);
void UntrackTerminatedExtension(const std::string& id);
void AddNewOrUpdatedExtension(const extensions::Extension* extension,
extensions::Extension::State initial_state,
extensions::BlacklistState blacklist_state,
const syncer::StringOrdinal& page_ordinal,
const std::string& install_parameter);
void NotifyExtensionLoaded(const extensions::Extension* extension);
void NotifyExtensionUnloaded(
const extensions::Extension* extension,
extensions::UnloadedExtensionInfo::Reason reason);
void FinishInstallation(const extensions::Extension* extension);
void UpdateActivePermissions(const extensions::Extension* extension);
void CheckPermissionsIncrease(const extensions::Extension* extension,
bool is_extension_installed);
void UpdateActiveExtensionsInCrashReporter();
bool ShouldEnableOnInstall(const extensions::Extension* extension);
bool ShouldDelayExtensionUpdate(const std::string& extension_id,
bool wait_for_idle) const;
virtual void OnBlacklistUpdated() OVERRIDE;
void ManageBlacklist(
const extensions::Blacklist::BlacklistStateMap& blacklisted_ids);
void UpdateBlockedExtensions(const ExtensionIdSet& blocked,
const ExtensionIdSet& unchanged);
void UpdateGreylistedExtensions(
const ExtensionIdSet& greylist,
const ExtensionIdSet& unchanged,
const extensions::Blacklist::BlacklistStateMap& state_map);
void UnloadAllExtensionsInternal();
Profile* profile_;
extensions::ExtensionSystem* system_;
extensions::ExtensionPrefs* extension_prefs_;
extensions::Blacklist* blacklist_;
ExtensionSyncService* extension_sync_service_;
extensions::ExtensionRegistry* registry_;
extensions::ExtensionSet greylist_;
extensions::ExtensionSet delayed_installs_;
extensions::PendingExtensionManager pending_extension_manager_;
base::FilePath install_directory_;
bool extensions_enabled_;
bool show_extensions_prompts_;
bool install_updates_when_idle_;
extensions::OneShotEvent* const ready_;
scoped_ptr<extensions::ExtensionUpdater> updater_;
typedef std::map<std::string, base::FilePath> UnloadedExtensionPathMap;
UnloadedExtensionPathMap unloaded_extension_paths_;
std::set<std::string> reloading_extensions_;
typedef std::map<std::string, scoped_refptr<content::DevToolsAgentHost> >
OrphanedDevTools;
OrphanedDevTools orphaned_dev_tools_;
content::NotificationRegistrar registrar_;
PrefChangeRegistrar pref_change_registrar_;
scoped_ptr<extensions::ComponentLoader> component_loader_;
extensions::ProviderCollection external_extension_providers_;
bool update_once_all_providers_are_ready_;
base::Closure external_updates_finished_callback_;
bool browser_terminating_;
bool installs_delayed_for_gc_;
bool is_first_run_;
std::set<std::string> extensions_being_reloaded_;
std::set<std::string> extensions_being_terminated_;
scoped_ptr<ExtensionErrorUI> extension_error_ui_;
scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
#if defined(ENABLE_EXTENSIONS)
scoped_ptr<extensions::ExtensionActionStorageManager>
extension_action_storage_manager_;
#endif
scoped_ptr<extensions::ManagementPolicy::Provider>
shared_module_policy_provider_;
scoped_ptr<extensions::ExtensionGarbageCollector> garbage_collector_;
scoped_ptr<extensions::SharedModuleService> shared_module_service_;
ObserverList<extensions::UpdateObserver, true> update_observers_;
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
InstallAppsWithUnlimtedStorage);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
InstallAppsAndCheckStorageProtection);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest, SetUnsetBlacklistInPrefs);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
BlacklistedExtensionWillNotInstall);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
UnloadBlacklistedExtensionPolicy);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
WillNotLoadBlacklistedExtensionsFromDirectory);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
BlacklistedInPrefsFromStartup);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
GreylistedExtensionDisabled);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
GreylistDontEnableManuallyDisabled);
FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
GreylistUnknownDontChange);
DISALLOW_COPY_AND_ASSIGN(ExtensionService);
};
#endif