Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1735)

Unified Diff: chrome/browser/extensions/api/profile_keyed_api_factory.h

Issue 11649053: Banish boilerplate. Profile-keyed API factory for extension API classes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: version 2, no traits Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..fa9deb36e1fe5aea65ac2a4d38f507dfc43dedb7
--- /dev/null
+++ b/chrome/browser/extensions/api/profile_keyed_api_factory.h
@@ -0,0 +1,91 @@
+// 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 "chrome/browser/extensions/extension_system_factory.h"
+#include "chrome/browser/profiles/profile_dependency_manager.h"
+#include "chrome/browser/profiles/profile_keyed_service.h"
+#include "chrome/browser/profiles/profile_keyed_service_factory.h"
+
+namespace extensions {
+
+// Instantiations of ProfileKeyedAPIFactory should use this base class
Jeffrey Yasskin 2012/12/22 00:16:04 You should mention that the fields need to be acce
Yoyo Zhou 2012/12/26 17:46:33 Mentioned. I think it would be okay too if they we
Jeffrey Yasskin 2012/12/26 19:21:55 Yeah, ProfileKeyedBaseFactory isn't a friend of Pr
+// and also declare a static const char* service_name() function (used in the
Jeffrey Yasskin 2012/12/22 00:16:04 s/declare/define/
Yoyo Zhou 2012/12/26 17:46:33 Done.
+// ProfileKeyedBase constructor).
Jeffrey Yasskin 2012/12/22 00:16:04 "ProfileKeyedBaseFactory"?
Yoyo Zhou 2012/12/26 17:46:33 Done.
+class ProfileKeyedAPI : public ProfileKeyedService {
+ protected:
+ // Defaults for flags that control ProfileKeyedAPIFactory behavior.
+ // See ProfileKeyedBaseFactory for usage.
+ static const bool kServiceRedirectedInIncognito = false;
+ static const bool kServiceIsNULLWhileTesting = false;
+};
+
+// A template for factories for ProfileKeyedServices that manage extension APIs.
+// T is a ProfileKeyedService that uses this factory template instead of
+// its own separate factory definition to manage its per-profile instances.
+template <typename T>
+class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory {
+ public:
+ static T* GetForProfile(Profile* profile) {
+ return static_cast<T*>(
+ GetInstance()->GetServiceForProfile(profile, true));
+ }
+
+ // Users of this factory template must manage their own instances
+ // (typically using LazyInstance or Singleton), because those cannot be
+ // included in more than one translation unit (and thus cannot be initialized
Jeffrey Yasskin 2012/12/22 00:16:04 Please include the code users will need to write i
Yoyo Zhou 2012/12/26 17:46:33 Done.
+ // in a header file).
+ static ProfileKeyedAPIFactory* GetInstance();
+
+ // Declare dependencies on other factories.
+ // By default, ExtensionSystemFactory is the only dependency; however,
+ // specializations can override this. For instance:
+ //
+ // template <>
+ // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() {
Jeffrey Yasskin 2012/12/22 00:16:04 And please remind them to declare the specializati
Yoyo Zhou 2012/12/26 17:46:33 Done.
+ // DependsOn(ExtensionSystemFactory::GetInstance());
+ // DependsOn(ProfileSyncServiceFactory::GetInstance());
+ // }
+ void DeclareFactoryDependencies() {
+ DependsOn(ExtensionSystemFactory::GetInstance());
+ }
+
+ ProfileKeyedAPIFactory()
+ : ProfileKeyedServiceFactory(T::service_name(),
+ ProfileDependencyManager::GetInstance()) {
+ DeclareFactoryDependencies();
+ }
+
+ virtual ~ProfileKeyedAPIFactory() {
+ }
+
+ private:
+ // 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 T::kServiceRedirectedInIncognito;
+ }
+
+ virtual bool ServiceIsCreatedWithProfile() const OVERRIDE {
+ return true;
+ }
+
+ virtual bool ServiceIsNULLWhileTesting() const OVERRIDE {
+ return T::kServiceIsNULLWhileTesting;
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory);
+};
+
+} // namespace extensions
+
+#endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_

Powered by Google App Engine
This is Rietveld 408576698