Upstream version 5.34.104.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 <string>
8
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "chrome/browser/extensions/component_loader.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "extensions/browser/extension_system.h"
19 #include "grit/browser_resources.h"
20
21 #if defined(OS_CHROMEOS)
22 #include "base/file_util.h"
23 #include "chrome/browser/chromeos/system/input_device_settings.h"
24 #include "chromeos/chromeos_constants.h"
25 #include "chromeos/chromeos_switches.h"
26 #endif
27
28 using content::BrowserThread;
29
30 namespace {
31
32 extensions::ComponentLoader* GetComponentLoader(Profile* profile) {
33   extensions::ExtensionSystem* extension_system =
34       extensions::ExtensionSystem::Get(profile);
35   ExtensionService* extension_service = extension_system->extension_service();
36   return extension_service->component_loader();
37 }
38
39 void LoadGaiaAuthExtension(Profile* profile) {
40   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
41
42   extensions::ComponentLoader* component_loader = GetComponentLoader(profile);
43   const CommandLine* command_line = CommandLine::ForCurrentProcess();
44   if (command_line->HasSwitch(switches::kAuthExtensionPath)) {
45     base::FilePath auth_extension_path =
46         command_line->GetSwitchValuePath(switches::kAuthExtensionPath);
47     component_loader->Add(IDR_GAIA_AUTH_MANIFEST, auth_extension_path);
48     return;
49   }
50
51 #if defined(OS_CHROMEOS)
52   if (command_line->HasSwitch(chromeos::switches::kGAIAAuthExtensionManifest)) {
53     const base::FilePath manifest_path = command_line->GetSwitchValuePath(
54         chromeos::switches::kGAIAAuthExtensionManifest);
55     std::string manifest;
56     if (!base::ReadFileToString(manifest_path, &manifest))
57       NOTREACHED();
58     component_loader->Add(manifest,
59                           base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
60     return;
61   }
62
63   int manifest_resource_id = IDR_GAIA_AUTH_MANIFEST;
64   if (chromeos::system::InputDeviceSettings::Get()
65           ->ForceKeyboardDrivenUINavigation()) {
66     manifest_resource_id = IDR_GAIA_AUTH_KEYBOARD_MANIFEST;
67   } else if (!command_line->HasSwitch(chromeos::switches::kDisableSamlSignin)) {
68     manifest_resource_id = IDR_GAIA_AUTH_SAML_MANIFEST;
69   }
70 #else
71   int manifest_resource_id = IDR_GAIA_AUTH_DESKTOP_MANIFEST;
72 #endif
73
74   component_loader->Add(manifest_resource_id,
75                         base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
76 }
77
78 void UnloadGaiaAuthExtension(Profile* profile) {
79   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80
81   const char kGaiaAuthId[] = "mfffpogegjflfpflabcdkioaeobkgjik";
82   GetComponentLoader(profile)->Remove(kGaiaAuthId);
83 }
84
85 }  // namespace
86
87 namespace extensions {
88
89 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(Profile* profile)
90     : profile_(profile), load_count_(0) {}
91
92 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
93   DCHECK_EQ(0, load_count_);
94 }
95
96 void GaiaAuthExtensionLoader::LoadIfNeeded() {
97   if (load_count_ == 0)
98     LoadGaiaAuthExtension(profile_);
99   ++load_count_;
100 }
101
102 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
103   --load_count_;
104   if (load_count_ == 0)
105     UnloadGaiaAuthExtension(profile_);
106 }
107
108 void GaiaAuthExtensionLoader::Shutdown() {
109   if (load_count_ > 0) {
110     UnloadGaiaAuthExtension(profile_);
111     load_count_ = 0;
112   }
113 }
114
115 // static
116 GaiaAuthExtensionLoader* GaiaAuthExtensionLoader::Get(Profile* profile) {
117   return ProfileKeyedAPIFactory<GaiaAuthExtensionLoader>::GetForProfile(
118       profile);
119 }
120
121 static base::LazyInstance<ProfileKeyedAPIFactory<GaiaAuthExtensionLoader> >
122 g_factory = LAZY_INSTANCE_INITIALIZER;
123
124 // static
125 ProfileKeyedAPIFactory<GaiaAuthExtensionLoader>*
126 GaiaAuthExtensionLoader::GetFactoryInstance() {
127   return g_factory.Pointer();
128 }
129
130 } // namespace extensions