/chromium-webcl/src/chrome/browser/extensions/api/profile_keyed_api_factory.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 113 lines · 50 code · 17 blank · 46 comment · 0 complexity · 65429be90f7df25003aa2e5898457648 MD5 · raw file

  1. // Copyright (c) 2012 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. #ifndef CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_
  5. #define CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_
  6. #include "chrome/browser/extensions/extension_system_factory.h"
  7. #include "chrome/browser/profiles/profile.h"
  8. #include "chrome/browser/profiles/profile_dependency_manager.h"
  9. #include "chrome/browser/profiles/profile_keyed_service.h"
  10. #include "chrome/browser/profiles/profile_keyed_service_factory.h"
  11. namespace extensions {
  12. template <typename T>
  13. class ProfileKeyedAPIFactory;
  14. // Instantiations of ProfileKeyedAPIFactory should use this base class
  15. // and also define a static const char* service_name() function (used in the
  16. // ProfileKeyedBaseFactory constructor). These fields should be accessible
  17. // to the ProfileKeyedAPIFactory for the service.
  18. class ProfileKeyedAPI : public ProfileKeyedService {
  19. protected:
  20. // Defaults for flags that control ProfileKeyedAPIFactory behavior.
  21. // See ProfileKeyedBaseFactory for usage.
  22. static const bool kServiceRedirectedInIncognito = false;
  23. static const bool kServiceIsNULLWhileTesting = false;
  24. // Users of this factory template must define a GetFactoryInstance()
  25. // and manage their own instances (typically using LazyInstance or
  26. // Singleton), because those cannot be included in more than one
  27. // translation unit (and thus cannot be initialized in a header file).
  28. //
  29. // In the header file, declare GetFactoryInstance(), e.g.:
  30. // class ProcessesAPI {
  31. // ...
  32. // public:
  33. // static ProfileKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
  34. // };
  35. //
  36. // In the cc file, provide the implementation, e.g.:
  37. // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> >
  38. // g_factory = LAZY_INSTANCE_INITIALIZER;
  39. //
  40. // // static
  41. // ProfileKeyedAPIFactory<ProcessesAPI>*
  42. // ProcessesAPI::GetFactoryInstance() {
  43. // return &g_factory.Get();
  44. // }
  45. };
  46. // A template for factories for ProfileKeyedServices that manage extension APIs.
  47. // T is a ProfileKeyedService that uses this factory template instead of
  48. // its own separate factory definition to manage its per-profile instances.
  49. template <typename T>
  50. class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory {
  51. public:
  52. static T* GetForProfile(Profile* profile) {
  53. return static_cast<T*>(
  54. T::GetFactoryInstance()->GetServiceForProfile(profile, true));
  55. }
  56. // Declare dependencies on other factories.
  57. // By default, ExtensionSystemFactory is the only dependency; however,
  58. // specializations can override this. Declare your specialization in
  59. // your header file after the ProfileKeyedAPI class definition.
  60. // Then in the cc file (or inline in the header), define it, e.g.:
  61. // template <>
  62. // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() {
  63. // DependsOn(ExtensionSystemFactory::GetInstance());
  64. // DependsOn(ProfileSyncServiceFactory::GetInstance());
  65. // }
  66. void DeclareFactoryDependencies() {
  67. DependsOn(ExtensionSystemFactory::GetInstance());
  68. }
  69. ProfileKeyedAPIFactory()
  70. : ProfileKeyedServiceFactory(T::service_name(),
  71. ProfileDependencyManager::GetInstance()) {
  72. DeclareFactoryDependencies();
  73. }
  74. virtual ~ProfileKeyedAPIFactory() {
  75. }
  76. private:
  77. // ProfileKeyedServiceFactory implementation.
  78. virtual ProfileKeyedService* BuildServiceInstanceFor(
  79. content::BrowserContext* profile) const OVERRIDE {
  80. return new T(static_cast<Profile*>(profile));
  81. }
  82. // ProfileKeyedBaseFactory implementation.
  83. // These can be effectively overridden with template specializations.
  84. virtual bool ServiceRedirectedInIncognito() const OVERRIDE {
  85. return T::kServiceRedirectedInIncognito;
  86. }
  87. virtual bool ServiceIsCreatedWithProfile() const OVERRIDE {
  88. return true;
  89. }
  90. virtual bool ServiceIsNULLWhileTesting() const OVERRIDE {
  91. return T::kServiceIsNULLWhileTesting;
  92. }
  93. DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory);
  94. };
  95. } // namespace extensions
  96. #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_