Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / signin / gaia_auth_extension_loader.cc
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.
4
5 #include "chrome/browser/extensions/signin/gaia_auth_extension_loader.h"
6
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"
21
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/system/input_device_settings.h"
24 #endif
25
26 using content::BrowserContext;
27 using content::BrowserThread;
28
29 namespace {
30
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();
36 }
37
38 void LoadGaiaAuthExtension(BrowserContext* context) {
39   DCHECK_CURRENTLY_ON(BrowserThread::UI);
40
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;
48   }
49
50   int manifest_resource_id = IDR_GAIA_AUTH_MANIFEST;
51
52 #if defined(OS_CHROMEOS)
53   if (chromeos::system::InputDeviceSettings::Get()
54           ->ForceKeyboardDrivenUINavigation()) {
55     manifest_resource_id = IDR_GAIA_AUTH_KEYBOARD_MANIFEST;
56   }
57 #endif
58
59   component_loader->Add(manifest_resource_id,
60                         base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
61 }
62
63 void UnloadGaiaAuthExtension(BrowserContext* context) {
64   DCHECK_CURRENTLY_ON(BrowserThread::UI);
65
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));
78   }
79
80   const char kGaiaAuthId[] = "mfffpogegjflfpflabcdkioaeobkgjik";
81   GetComponentLoader(context)->Remove(kGaiaAuthId);
82 }
83
84 }  // namespace
85
86 namespace extensions {
87
88 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(BrowserContext* context)
89     : browser_context_(context), load_count_(0) {}
90
91 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
92   DCHECK_EQ(0, load_count_);
93 }
94
95 void GaiaAuthExtensionLoader::LoadIfNeeded() {
96   if (load_count_ == 0)
97     LoadGaiaAuthExtension(browser_context_);
98   ++load_count_;
99 }
100
101 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
102   --load_count_;
103   if (load_count_ == 0)
104     UnloadGaiaAuthExtension(browser_context_);
105 }
106
107 void GaiaAuthExtensionLoader::Shutdown() {
108   if (load_count_ > 0) {
109     UnloadGaiaAuthExtension(browser_context_);
110     load_count_ = 0;
111   }
112 }
113
114 // static
115 GaiaAuthExtensionLoader* GaiaAuthExtensionLoader::Get(BrowserContext* context) {
116   return BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>::Get(context);
117 }
118
119 static base::LazyInstance<
120     BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader> > g_factory =
121     LAZY_INSTANCE_INITIALIZER;
122
123 // static
124 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>*
125 GaiaAuthExtensionLoader::GetFactoryInstance() {
126   return g_factory.Pointer();
127 }
128
129 } // namespace extensions