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"
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"
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"
28 using content::BrowserThread;
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();
39 void LoadGaiaAuthExtension(Profile* profile) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
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);
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);
56 if (!base::ReadFileToString(manifest_path, &manifest))
58 component_loader->Add(manifest,
59 base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
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;
71 int manifest_resource_id = IDR_GAIA_AUTH_DESKTOP_MANIFEST;
74 component_loader->Add(manifest_resource_id,
75 base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
78 void UnloadGaiaAuthExtension(Profile* profile) {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
81 const char kGaiaAuthId[] = "mfffpogegjflfpflabcdkioaeobkgjik";
82 GetComponentLoader(profile)->Remove(kGaiaAuthId);
87 namespace extensions {
89 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(Profile* profile)
90 : profile_(profile), load_count_(0) {}
92 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
93 DCHECK_EQ(0, load_count_);
96 void GaiaAuthExtensionLoader::LoadIfNeeded() {
98 LoadGaiaAuthExtension(profile_);
102 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
104 if (load_count_ == 0)
105 UnloadGaiaAuthExtension(profile_);
108 void GaiaAuthExtensionLoader::Shutdown() {
109 if (load_count_ > 0) {
110 UnloadGaiaAuthExtension(profile_);
116 GaiaAuthExtensionLoader* GaiaAuthExtensionLoader::Get(Profile* profile) {
117 return ProfileKeyedAPIFactory<GaiaAuthExtensionLoader>::GetForProfile(
121 static base::LazyInstance<ProfileKeyedAPIFactory<GaiaAuthExtensionLoader> >
122 g_factory = LAZY_INSTANCE_INITIALIZER;
125 ProfileKeyedAPIFactory<GaiaAuthExtensionLoader>*
126 GaiaAuthExtensionLoader::GetFactoryInstance() {
127 return g_factory.Pointer();
130 } // namespace extensions