Home | History | Annotate | Download | only in signin
      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 "base/file_util.h"
     24 #include "chrome/browser/chromeos/profiles/profile_helper.h"
     25 #include "chrome/browser/chromeos/system/input_device_settings.h"
     26 #include "chromeos/chromeos_constants.h"
     27 #include "chromeos/chromeos_switches.h"
     28 #include "components/signin/core/common/profile_management_switches.h"
     29 #endif
     30 
     31 using content::BrowserContext;
     32 using content::BrowserThread;
     33 
     34 namespace {
     35 
     36 extensions::ComponentLoader* GetComponentLoader(BrowserContext* context) {
     37   extensions::ExtensionSystem* extension_system =
     38       extensions::ExtensionSystem::Get(context);
     39   ExtensionService* extension_service = extension_system->extension_service();
     40   return extension_service->component_loader();
     41 }
     42 
     43 void LoadGaiaAuthExtension(BrowserContext* context) {
     44   DCHECK_CURRENTLY_ON(BrowserThread::UI);
     45 
     46   extensions::ComponentLoader* component_loader = GetComponentLoader(context);
     47   const CommandLine* command_line = CommandLine::ForCurrentProcess();
     48   if (command_line->HasSwitch(switches::kAuthExtensionPath)) {
     49     base::FilePath auth_extension_path =
     50         command_line->GetSwitchValuePath(switches::kAuthExtensionPath);
     51     component_loader->Add(IDR_GAIA_AUTH_MANIFEST, auth_extension_path);
     52     return;
     53   }
     54 
     55 #if defined(OS_CHROMEOS)
     56   int manifest_resource_id = IDR_GAIA_AUTH_MANIFEST;
     57   if (chromeos::system::InputDeviceSettings::Get()
     58           ->ForceKeyboardDrivenUINavigation()) {
     59     manifest_resource_id = IDR_GAIA_AUTH_KEYBOARD_MANIFEST;
     60   } else if (!command_line->HasSwitch(chromeos::switches::kDisableSamlSignin) ||
     61              (switches::IsNewProfileManagement() &&
     62               context->GetPath() !=
     63                   chromeos::ProfileHelper::GetSigninProfileDir())) {
     64     manifest_resource_id = IDR_GAIA_AUTH_SAML_MANIFEST;
     65   }
     66 #else
     67   int manifest_resource_id = IDR_GAIA_AUTH_SAML_MANIFEST;
     68 #endif
     69 
     70   component_loader->Add(manifest_resource_id,
     71                         base::FilePath(FILE_PATH_LITERAL("gaia_auth")));
     72 }
     73 
     74 void UnloadGaiaAuthExtension(BrowserContext* context) {
     75   DCHECK_CURRENTLY_ON(BrowserThread::UI);
     76 
     77   content::StoragePartition* partition =
     78       content::BrowserContext::GetStoragePartitionForSite(
     79           context, GURL(chrome::kChromeUIChromeSigninURL));
     80   if (partition) {
     81     partition->ClearData(
     82         content::StoragePartition::REMOVE_DATA_MASK_ALL,
     83         content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
     84         GURL(),
     85         content::StoragePartition::OriginMatcherFunction(),
     86         base::Time(),
     87         base::Time::Max(),
     88         base::Bind(&base::DoNothing));
     89   }
     90 
     91   const char kGaiaAuthId[] = "mfffpogegjflfpflabcdkioaeobkgjik";
     92   GetComponentLoader(context)->Remove(kGaiaAuthId);
     93 }
     94 
     95 }  // namespace
     96 
     97 namespace extensions {
     98 
     99 GaiaAuthExtensionLoader::GaiaAuthExtensionLoader(BrowserContext* context)
    100     : browser_context_(context), load_count_(0) {}
    101 
    102 GaiaAuthExtensionLoader::~GaiaAuthExtensionLoader() {
    103   DCHECK_EQ(0, load_count_);
    104 }
    105 
    106 void GaiaAuthExtensionLoader::LoadIfNeeded() {
    107   if (load_count_ == 0)
    108     LoadGaiaAuthExtension(browser_context_);
    109   ++load_count_;
    110 }
    111 
    112 void GaiaAuthExtensionLoader::UnloadIfNeeded() {
    113   --load_count_;
    114   if (load_count_ == 0)
    115     UnloadGaiaAuthExtension(browser_context_);
    116 }
    117 
    118 void GaiaAuthExtensionLoader::Shutdown() {
    119   if (load_count_ > 0) {
    120     UnloadGaiaAuthExtension(browser_context_);
    121     load_count_ = 0;
    122   }
    123 }
    124 
    125 // static
    126 GaiaAuthExtensionLoader* GaiaAuthExtensionLoader::Get(BrowserContext* context) {
    127   return BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>::Get(context);
    128 }
    129 
    130 static base::LazyInstance<
    131     BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader> > g_factory =
    132     LAZY_INSTANCE_INITIALIZER;
    133 
    134 // static
    135 BrowserContextKeyedAPIFactory<GaiaAuthExtensionLoader>*
    136 GaiaAuthExtensionLoader::GetFactoryInstance() {
    137   return g_factory.Pointer();
    138 }
    139 
    140 } // namespace extensions
    141