Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / signin / gaia_auth_extension_loader.cc
blob69508b5c9159382e9d883c5b1925fc2ab0e99595
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"
27 #endif
29 using content::BrowserContext;
30 using content::BrowserThread;
32 namespace {
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);
51 return;
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;
61 #endif
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());
73 if (partition) {
74 partition->ClearData(
75 content::StoragePartition::REMOVE_DATA_MASK_ALL,
76 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
77 GURL(),
78 content::StoragePartition::OriginMatcherFunction(),
79 base::Time(),
80 base::Time::Max(),
81 base::Bind(&base::DoNothing));
83 GetComponentLoader(context)->Remove(extensions::kGaiaAuthExtensionId);
86 } // namespace
88 namespace extensions {
90 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(BrowserContext* context)
91 : browser_context_(context),
92 load_count_(0),
93 last_data_id_(0),
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_);
104 ++load_count_;
107 void GaiaAuthExtensionLoader::UnloadIfNeededAsync() {
108 base::ThreadTaskRunnerHandle::Get()->PostTask(
109 FROM_HERE,
110 base::Bind(&GaiaAuthExtensionLoader::UnloadIfNeeded,
111 weak_ptr_factory_.GetWeakPtr()));
114 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
115 --load_count_;
116 if (load_count_ == 0) {
117 UnloadGaiaAuthExtension(browser_context_);
118 data_.clear();
122 int GaiaAuthExtensionLoader::AddData(const std::string& data) {
123 ++last_data_id_;
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())
131 return false;
133 *data = it->second;
134 return true;
137 void GaiaAuthExtensionLoader::Shutdown() {
138 if (load_count_ > 0) {
139 UnloadGaiaAuthExtension(browser_context_);
140 load_count_ = 0;
142 data_.clear();
145 // static
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;
154 // static
155 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>*
156 GaiaAuthExtensionLoader::GetFactoryInstance() {
157 return g_factory.Pointer();
160 } // namespace extensions