OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | |
7 | |
8 #include "chrome/browser/extensions/extension_system_factory.h" | |
9 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
10 #include "chrome/browser/profiles/profile_keyed_service.h" | |
11 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | |
12 | |
13 namespace extensions { | |
14 | |
15 // 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
| |
16 // 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.
| |
17 // ProfileKeyedBase constructor). | |
Jeffrey Yasskin
2012/12/22 00:16:04
"ProfileKeyedBaseFactory"?
Yoyo Zhou
2012/12/26 17:46:33
Done.
| |
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 }; | |
25 | |
26 // A template for factories for ProfileKeyedServices that manage extension APIs. | |
27 // T is a ProfileKeyedService that uses this factory template instead of | |
28 // its own separate factory definition to manage its per-profile instances. | |
29 template <typename T> | |
30 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { | |
31 public: | |
32 static T* GetForProfile(Profile* profile) { | |
33 return static_cast<T*>( | |
34 GetInstance()->GetServiceForProfile(profile, true)); | |
35 } | |
36 | |
37 // Users of this factory template must manage their own instances | |
38 // (typically using LazyInstance or Singleton), because those cannot be | |
39 // 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.
| |
40 // in a header file). | |
41 static ProfileKeyedAPIFactory* GetInstance(); | |
42 | |
43 // Declare dependencies on other factories. | |
44 // By default, ExtensionSystemFactory is the only dependency; however, | |
45 // specializations can override this. For instance: | |
46 // | |
47 // template <> | |
48 // 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.
| |
49 // DependsOn(ExtensionSystemFactory::GetInstance()); | |
50 // DependsOn(ProfileSyncServiceFactory::GetInstance()); | |
51 // } | |
52 void DeclareFactoryDependencies() { | |
53 DependsOn(ExtensionSystemFactory::GetInstance()); | |
54 } | |
55 | |
56 ProfileKeyedAPIFactory() | |
57 : ProfileKeyedServiceFactory(T::service_name(), | |
58 ProfileDependencyManager::GetInstance()) { | |
59 DeclareFactoryDependencies(); | |
60 } | |
61 | |
62 virtual ~ProfileKeyedAPIFactory() { | |
63 } | |
64 | |
65 private: | |
66 // ProfileKeyedServiceFactory implementation. | |
67 virtual ProfileKeyedService* BuildServiceInstanceFor( | |
68 Profile* profile) const OVERRIDE { | |
69 return new T(profile); | |
70 } | |
71 | |
72 // ProfileKeyedBaseFactory implementation. | |
73 // These can be effectively overridden with template specializations. | |
74 virtual bool ServiceRedirectedInIncognito() const OVERRIDE { | |
75 return T::kServiceRedirectedInIncognito; | |
76 } | |
77 | |
78 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { | |
79 return true; | |
80 } | |
81 | |
82 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { | |
83 return T::kServiceIsNULLWhileTesting; | |
84 } | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); | |
87 }; | |
88 | |
89 } // namespace extensions | |
90 | |
91 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | |
OLD | NEW |