Chromium Code Reviews| Index: chrome/browser/extensions/api/profile_keyed_api_factory.h |
| diff --git a/chrome/browser/extensions/api/profile_keyed_api_factory.h b/chrome/browser/extensions/api/profile_keyed_api_factory.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8cad6a17bf6ae42dfdf6da17ccec3c144103c4de |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/profile_keyed_api_factory.h |
| @@ -0,0 +1,102 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
| +#define CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
| + |
| +#include "base/lazy_instance.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "chrome/browser/extensions/extension_system_factory.h" |
| +#include "chrome/browser/profiles/profile_dependency_manager.h" |
| +#include "chrome/browser/profiles/profile_keyed_service_factory.h" |
| + |
| +namespace extensions { |
| + |
| +// Default settings for instantiations of ProfileKeyedAPIFactory. |
| +struct DefaultProfileKeyedAPIFactoryTraits { |
| + // Flags that control ProfileKeyedAPIFactory behavior. |
| + // See ProfileKeyedBaseFactory for usage. |
| + static const bool kServiceRedirectedInIncognito = false; |
| + static const bool kServiceIsNULLWhileTesting = false; |
| +}; |
| + |
| +// Instantiations of ProfileKeyedAPIFactory must specialize this traits class |
| +// and declare a static const char* name() function (used in the |
| +// ProfileKeyedBase constructor). They can also override flags from |
| +// DefaultProfileKeyedAPIFactoryTraits. |
| +template <typename T> |
| +struct ProfileKeyedAPIFactoryTraits |
| + : public DefaultProfileKeyedAPIFactoryTraits { |
| +}; |
| + |
| +// A template for factories for Profile-keyed services that manage |
| +// extension APIs. |
| +template <typename T> |
|
Jeffrey Yasskin
2012/12/21 01:43:50
Document what T is.
Can you make the trait fields
Yoyo Zhou
2012/12/21 22:41:33
Expanded the comment.
If I make them static membe
|
| +class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { |
| + public: |
| + typedef ProfileKeyedAPIFactoryTraits<T> Traits; |
| + |
| + static T* GetForProfile(Profile* profile) { |
| + return static_cast<T*>( |
| + GetInstance()->GetServiceForProfile(profile, true)); |
| + } |
| + |
| + static ProfileKeyedAPIFactory<T>* GetInstance() { |
| + if (!instance_.Get()) |
| + instance_.Get().reset(new ProfileKeyedAPIFactory<T>); |
| + return instance_.Get().get(); |
| + } |
| + |
| + // Declare dependencies on other factories. |
| + // By default, ExtensionSystemFactory is the only dependency; however, |
| + // specializations can override this. |
|
Jeffrey Yasskin
2012/12/21 01:43:50
Give some example code for how they'd override it.
Yoyo Zhou
2012/12/21 22:41:33
Done.
|
| + void DeclareFactoryDependencies() { |
| + DependsOn(extensions::ExtensionSystemFactory::GetInstance()); |
| + } |
| + |
| + ProfileKeyedAPIFactory<T>() |
| + : ProfileKeyedServiceFactory(Traits::name(), |
| + ProfileDependencyManager::GetInstance()) { |
| + DeclareFactoryDependencies(); |
| + } |
| + |
| + virtual ~ProfileKeyedAPIFactory<T>() { |
|
Jeffrey Yasskin
2012/12/21 01:43:50
s/<T>//. C++ expands the plain name of the current
Yoyo Zhou
2012/12/21 22:41:33
Done throughout.
|
| + } |
| + |
| + private: |
| + friend class scoped_ptr<ProfileKeyedAPIFactory<T> >; |
| + |
| + // ProfileKeyedServiceFactory implementation. |
| + virtual ProfileKeyedService* BuildServiceInstanceFor( |
| + Profile* profile) const OVERRIDE { |
| + return new T(profile); |
| + } |
| + |
| + // ProfileKeyedBaseFactory implementation. |
| + // These can be effectively overridden with template specializations. |
| + virtual bool ServiceRedirectedInIncognito() const OVERRIDE { |
| + return Traits::kServiceRedirectedInIncognito; |
| + } |
| + |
| + virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { |
| + return true; |
| + } |
| + |
| + virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { |
| + return Traits::kServiceIsNULLWhileTesting; |
| + } |
| + |
| + static base::LazyInstance<scoped_ptr<ProfileKeyedAPIFactory<T> > > |
|
Jeffrey Yasskin
2012/12/21 01:43:50
Why LazyInstance<scoped_ptr>? Do you want LazyInst
Yoyo Zhou
2012/12/21 22:41:33
Can't be done here:
invalid application of 'sizeo
|
| + instance_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory<T>); |
| +}; |
| + |
| +template <typename T> |
| +base::LazyInstance<scoped_ptr<ProfileKeyedAPIFactory<T> > > |
| +ProfileKeyedAPIFactory<T>::instance_ = LAZY_INSTANCE_INITIALIZER; |
|
Jeffrey Yasskin
2012/12/21 01:43:50
This is going to be sad if it's #included from mul
Yoyo Zhou
2012/12/21 22:41:33
Moved the lazy instances to each .cc file.
|
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |