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 "base/lazy_instance.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "chrome/browser/extensions/extension_system_factory.h" | |
11 #include "chrome/browser/profiles/profile_dependency_manager.h" | |
12 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | |
13 | |
14 namespace extensions { | |
15 | |
16 // Default settings for instantiations of ProfileKeyedAPIFactory. | |
17 struct DefaultProfileKeyedAPIFactoryTraits { | |
18 // Flags that control ProfileKeyedAPIFactory behavior. | |
19 // See ProfileKeyedBaseFactory for usage. | |
20 static const bool kServiceRedirectedInIncognito = false; | |
21 static const bool kServiceIsNULLWhileTesting = false; | |
22 }; | |
23 | |
24 // Instantiations of ProfileKeyedAPIFactory must specialize this traits class | |
25 // and declare a static const char* name() function (used in the | |
26 // ProfileKeyedBase constructor). They can also override flags from | |
27 // DefaultProfileKeyedAPIFactoryTraits. | |
28 template <typename T> | |
29 struct ProfileKeyedAPIFactoryTraits | |
30 : public DefaultProfileKeyedAPIFactoryTraits { | |
31 }; | |
32 | |
33 // A template for factories for Profile-keyed services that manage | |
34 // extension APIs. | |
35 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
| |
36 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { | |
37 public: | |
38 typedef ProfileKeyedAPIFactoryTraits<T> Traits; | |
39 | |
40 static T* GetForProfile(Profile* profile) { | |
41 return static_cast<T*>( | |
42 GetInstance()->GetServiceForProfile(profile, true)); | |
43 } | |
44 | |
45 static ProfileKeyedAPIFactory<T>* GetInstance() { | |
46 if (!instance_.Get()) | |
47 instance_.Get().reset(new ProfileKeyedAPIFactory<T>); | |
48 return instance_.Get().get(); | |
49 } | |
50 | |
51 // Declare dependencies on other factories. | |
52 // By default, ExtensionSystemFactory is the only dependency; however, | |
53 // 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.
| |
54 void DeclareFactoryDependencies() { | |
55 DependsOn(extensions::ExtensionSystemFactory::GetInstance()); | |
56 } | |
57 | |
58 ProfileKeyedAPIFactory<T>() | |
59 : ProfileKeyedServiceFactory(Traits::name(), | |
60 ProfileDependencyManager::GetInstance()) { | |
61 DeclareFactoryDependencies(); | |
62 } | |
63 | |
64 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.
| |
65 } | |
66 | |
67 private: | |
68 friend class scoped_ptr<ProfileKeyedAPIFactory<T> >; | |
69 | |
70 // ProfileKeyedServiceFactory implementation. | |
71 virtual ProfileKeyedService* BuildServiceInstanceFor( | |
72 Profile* profile) const OVERRIDE { | |
73 return new T(profile); | |
74 } | |
75 | |
76 // ProfileKeyedBaseFactory implementation. | |
77 // These can be effectively overridden with template specializations. | |
78 virtual bool ServiceRedirectedInIncognito() const OVERRIDE { | |
79 return Traits::kServiceRedirectedInIncognito; | |
80 } | |
81 | |
82 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { | |
83 return true; | |
84 } | |
85 | |
86 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { | |
87 return Traits::kServiceIsNULLWhileTesting; | |
88 } | |
89 | |
90 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
| |
91 instance_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory<T>); | |
94 }; | |
95 | |
96 template <typename T> | |
97 base::LazyInstance<scoped_ptr<ProfileKeyedAPIFactory<T> > > | |
98 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.
| |
99 | |
100 } // namespace extensions | |
101 | |
102 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | |
OLD | NEW |