Upstream version 10.39.225.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   GetComponentLoader(context)->Remove(extensions::kGaiaAuthExtensionId);
80 }
81
82 }  // namespace
83
84 namespace extensions {
85
86 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(BrowserContext* context)
87     : browser_context_(context), load_count_(0) {}
88
89 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
90   DCHECK_EQ(0, load_count_);
91 }
92
93 void GaiaAuthExtensionLoader::LoadIfNeeded() {
94   if (load_count_ == 0)
95     LoadGaiaAuthExtension(browser_context_);
96   ++load_count_;
97 }
98
99 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
100   --load_count_;
101   if (load_count_ == 0)
102     UnloadGaiaAuthExtension(browser_context_);
103 }
104
105 void GaiaAuthExtensionLoader::Shutdown() {
106   if (load_count_ > 0) {
107     UnloadGaiaAuthExtension(browser_context_);
108     load_count_ = 0;
109   }
110 }
111
112 // static
113 GaiaAuthExtensionLoader* GaiaAuthExtensionLoader::Get(BrowserContext* context) {
114   return BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>::Get(context);
115 }
116
117 static base::LazyInstance<
118     BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader> > g_factory =
119     LAZY_INSTANCE_INITIALIZER;
120
121 // static
122 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>*
123 GaiaAuthExtensionLoader::GetFactoryInstance() {
124   return g_factory.Pointer();
125 }
126
127 } // namespace extensions