Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / signin / gaia_auth_extension_loader.cc
blob0a2ffec0ebecd0324b180aedc2a7ddc46e3aa12c
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 component_loader->Add(IDR_GAIA_AUTH_MANIFEST,
55 base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
58 void UnloadGaiaAuthExtension(BrowserContext* context) {
59 DCHECK_CURRENTLY_ON(BrowserThread::UI);
61 content::StoragePartition* partition =
62 content::BrowserContext::GetStoragePartitionForSite(
63 context, signin::GetSigninPartitionURL());
64 if (partition) {
65 partition->ClearData(
66 content::StoragePartition::REMOVE_DATA_MASK_ALL,
67 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
68 GURL(),
69 content::StoragePartition::OriginMatcherFunction(),
70 base::Time(),
71 base::Time::Max(),
72 base::Bind(&base::DoNothing));
74 GetComponentLoader(context)->Remove(extensions::kGaiaAuthExtensionId);
77 } // namespace
79 namespace extensions {
81 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(BrowserContext* context)
82 : browser_context_(context),
83 load_count_(0),
84 last_data_id_(0),
85 weak_ptr_factory_(this) {
88 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
89 DCHECK_EQ(0, load_count_);
92 void GaiaAuthExtensionLoader::LoadIfNeeded() {
93 if (load_count_ == 0)
94 LoadGaiaAuthExtension(browser_context_);
95 ++load_count_;
98 void GaiaAuthExtensionLoader::UnloadIfNeededAsync() {
99 base::ThreadTaskRunnerHandle::Get()->PostTask(
100 FROM_HERE,
101 base::Bind(&GaiaAuthExtensionLoader::UnloadIfNeeded,
102 weak_ptr_factory_.GetWeakPtr()));
105 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
106 --load_count_;
107 if (load_count_ == 0) {
108 UnloadGaiaAuthExtension(browser_context_);
109 data_.clear();
113 int GaiaAuthExtensionLoader::AddData(const std::string& data) {
114 ++last_data_id_;
115 data_[last_data_id_] = data;
116 return last_data_id_;
119 bool GaiaAuthExtensionLoader::GetData(int data_id, std::string* data) {
120 auto it = data_.find(data_id);
121 if (it == data_.end())
122 return false;
124 *data = it->second;
125 return true;
128 void GaiaAuthExtensionLoader::Shutdown() {
129 if (load_count_ > 0) {
130 UnloadGaiaAuthExtension(browser_context_);
131 load_count_ = 0;
133 data_.clear();
136 // static
137 GaiaAuthExtensionLoader* GaiaAuthExtensionLoader::Get(BrowserContext* context) {
138 return BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>::Get(context);
141 static base::LazyInstance<
142 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader> > g_factory =
143 LAZY_INSTANCE_INITIALIZER;
145 // static
146 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>*
147 GaiaAuthExtensionLoader::GetFactoryInstance() {
148 return g_factory.Pointer();
151 } // namespace extensions