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 "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chrome/browser/extensions/component_loader.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/signin/signin_promo.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/storage_partition.h"
22 #include "extensions/browser/extension_system.h"
23 #include "grit/browser_resources.h"
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/system/input_device_settings.h"
29 using content::BrowserContext
;
30 using content::BrowserThread
;
34 extensions::ComponentLoader
* GetComponentLoader(BrowserContext
* context
) {
35 extensions::ExtensionSystem
* extension_system
=
36 extensions::ExtensionSystem::Get(context
);
37 ExtensionService
* extension_service
= extension_system
->extension_service();
38 return extension_service
->component_loader();
41 void LoadGaiaAuthExtension(BrowserContext
* context
) {
42 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
44 extensions::ComponentLoader
* component_loader
= GetComponentLoader(context
);
45 const base::CommandLine
* command_line
=
46 base::CommandLine::ForCurrentProcess();
47 if (command_line
->HasSwitch(switches::kAuthExtensionPath
)) {
48 base::FilePath auth_extension_path
=
49 command_line
->GetSwitchValuePath(switches::kAuthExtensionPath
);
50 component_loader
->Add(IDR_GAIA_AUTH_MANIFEST
, auth_extension_path
);
54 int manifest_resource_id
= IDR_GAIA_AUTH_MANIFEST
;
56 #if defined(OS_CHROMEOS)
57 if (chromeos::system::InputDeviceSettings::Get()
58 ->ForceKeyboardDrivenUINavigation()) {
59 manifest_resource_id
= IDR_GAIA_AUTH_KEYBOARD_MANIFEST
;
63 component_loader
->Add(manifest_resource_id
,
64 base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
67 void UnloadGaiaAuthExtension(BrowserContext
* context
) {
68 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
70 content::StoragePartition
* partition
=
71 content::BrowserContext::GetStoragePartitionForSite(
72 context
, signin::GetSigninPartitionURL());
75 content::StoragePartition::REMOVE_DATA_MASK_ALL
,
76 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
,
78 content::StoragePartition::OriginMatcherFunction(),
81 base::Bind(&base::DoNothing
));
83 GetComponentLoader(context
)->Remove(extensions::kGaiaAuthExtensionId
);
88 namespace extensions
{
90 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(BrowserContext
* context
)
91 : browser_context_(context
),
94 weak_ptr_factory_(this) {
97 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
98 DCHECK_EQ(0, load_count_
);
101 void GaiaAuthExtensionLoader::LoadIfNeeded() {
102 if (load_count_
== 0)
103 LoadGaiaAuthExtension(browser_context_
);
107 void GaiaAuthExtensionLoader::UnloadIfNeededAsync() {
108 base::ThreadTaskRunnerHandle::Get()->PostTask(
110 base::Bind(&GaiaAuthExtensionLoader::UnloadIfNeeded
,
111 weak_ptr_factory_
.GetWeakPtr()));
114 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
116 if (load_count_
== 0) {
117 UnloadGaiaAuthExtension(browser_context_
);
122 int GaiaAuthExtensionLoader::AddData(const std::string
& data
) {
124 data_
[last_data_id_
] = data
;
125 return last_data_id_
;
128 bool GaiaAuthExtensionLoader::GetData(int data_id
, std::string
* data
) {
129 auto it
= data_
.find(data_id
);
130 if (it
== data_
.end())
137 void GaiaAuthExtensionLoader::Shutdown() {
138 if (load_count_
> 0) {
139 UnloadGaiaAuthExtension(browser_context_
);
146 GaiaAuthExtensionLoader
* GaiaAuthExtensionLoader::Get(BrowserContext
* context
) {
147 return BrowserContextKeyedAPIFactory
<GaiaAuthExtensionLoader
>::Get(context
);
150 static base::LazyInstance
<
151 BrowserContextKeyedAPIFactory
<GaiaAuthExtensionLoader
> > g_factory
=
152 LAZY_INSTANCE_INITIALIZER
;
155 BrowserContextKeyedAPIFactory
<GaiaAuthExtensionLoader
>*
156 GaiaAuthExtensionLoader::GetFactoryInstance() {
157 return g_factory
.Pointer();
160 } // namespace extensions