MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / chrome / browser / extensions / signin / gaia_auth_extension_loader.cc
blob6f14e6e9a160668b7cac3d120b7fd406727378aa
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/signin/gaia_auth_extension_loader.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "chrome/browser/extensions/component_loader.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/common/chrome_constants.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/storage_partition.h"
19 #include "extensions/browser/extension_system.h"
20 #include "grit/browser_resources.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/system/input_device_settings.h"
24 #endif
26 using content::BrowserContext;
27 using content::BrowserThread;
29 namespace {
31 extensions::ComponentLoader* GetComponentLoader(BrowserContext* context) {
32 extensions::ExtensionSystem* extension_system =
33 extensions::ExtensionSystem::Get(context);
34 ExtensionService* extension_service = extension_system->extension_service();
35 return extension_service->component_loader();
38 void LoadGaiaAuthExtension(BrowserContext* context) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI);
41 extensions::ComponentLoader* component_loader = GetComponentLoader(context);
42 const CommandLine* command_line = CommandLine::ForCurrentProcess();
43 if (command_line->HasSwitch(switches::kAuthExtensionPath)) {
44 base::FilePath auth_extension_path =
45 command_line->GetSwitchValuePath(switches::kAuthExtensionPath);
46 component_loader->Add(IDR_GAIA_AUTH_MANIFEST, auth_extension_path);
47 return;
50 int manifest_resource_id = IDR_GAIA_AUTH_MANIFEST;
52 #if defined(OS_CHROMEOS)
53 if (chromeos::system::InputDeviceSettings::Get()
54 ->ForceKeyboardDrivenUINavigation()) {
55 manifest_resource_id = IDR_GAIA_AUTH_KEYBOARD_MANIFEST;
57 #endif
59 component_loader->Add(manifest_resource_id,
60 base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
63 void UnloadGaiaAuthExtension(BrowserContext* context) {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI);
66 content::StoragePartition* partition =
67 content::BrowserContext::GetStoragePartitionForSite(
68 context, GURL(chrome::kChromeUIChromeSigninURL));
69 if (partition) {
70 partition->ClearData(
71 content::StoragePartition::REMOVE_DATA_MASK_ALL,
72 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
73 GURL(),
74 content::StoragePartition::OriginMatcherFunction(),
75 base::Time(),
76 base::Time::Max(),
77 base::Bind(&base::DoNothing));
79 GetComponentLoader(context)->Remove(extensions::kGaiaAuthExtensionId);
82 } // namespace
84 namespace extensions {
86 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(BrowserContext* context)
87 : browser_context_(context), load_count_(0) {}
89 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
90 DCHECK_EQ(0, load_count_);
93 void GaiaAuthExtensionLoader::LoadIfNeeded() {
94 if (load_count_ == 0)
95 LoadGaiaAuthExtension(browser_context_);
96 ++load_count_;
99 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
100 --load_count_;
101 if (load_count_ == 0)
102 UnloadGaiaAuthExtension(browser_context_);
105 void GaiaAuthExtensionLoader::Shutdown() {
106 if (load_count_ > 0) {
107 UnloadGaiaAuthExtension(browser_context_);
108 load_count_ = 0;
112 // static
113 GaiaAuthExtensionLoader* GaiaAuthExtensionLoader::Get(BrowserContext* context) {
114 return BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>::Get(context);
117 static base::LazyInstance<
118 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader> > g_factory =
119 LAZY_INSTANCE_INITIALIZER;
121 // static
122 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>*
123 GaiaAuthExtensionLoader::GetFactoryInstance() {
124 return g_factory.Pointer();
127 } // namespace extensions