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

Side by Side Diff: components/browser_context_keyed_service/browser_context_keyed_service_factory.cc

Issue 23068005: Convert UserPolicySigninService to use OAuth2TokenService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed code review feedback. Created 7 years, 4 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h" 5 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" 11 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h"
12 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" 12 #include "components/browser_context_keyed_service/browser_context_keyed_service .h"
13 #include "content/public/browser/browser_context.h" 13 #include "content/public/browser/browser_context.h"
14 14
15 void BrowserContextKeyedServiceFactory::SetTestingFactory( 15 void BrowserContextKeyedServiceFactory::SetTestingFactory(
16 content::BrowserContext* context, FactoryFunction factory) { 16 content::BrowserContext* context, FactoryFunction factory) {
Mattias Nissler (ping if slow) 2013/08/19 14:03:22 drive-by nit: args on separate lines
Andrew T Wilson (Slow) 2013/08/20 09:28:35 As a rule I don't fix style violations on already-
17 // Should not set a testing factory if we already have a global factory.
Mattias Nissler (ping if slow) 2013/08/19 14:03:22 why?
Andrew T Wilson (Slow) 2013/08/20 09:28:35 Because one clobbers the other - having a test set
18 DCHECK(!have_global_factory_);
19 CleanupExistingServices(context);
20 factories_[context] = factory;
21 }
22
23 void BrowserContextKeyedServiceFactory::CleanupExistingServices(
24 content::BrowserContext* context) {
17 // Destroying the context may cause us to lose data about whether |context| 25 // Destroying the context may cause us to lose data about whether |context|
18 // has our preferences registered on it (since the context object itself 26 // has our preferences registered on it (since the context object itself
19 // isn't dead). See if we need to readd it once we've gone through normal 27 // isn't dead). See if we need to readd it once we've gone through normal
20 // destruction. 28 // destruction.
21 bool add_context = ArePreferencesSetOn(context); 29 bool add_context = ArePreferencesSetOn(context);
22 30
23 // We have to go through the shutdown and destroy mechanisms because there 31 // We have to go through the shutdown and destroy mechanisms because there
24 // are unit tests that create a service on a context and then change the 32 // are unit tests that create a service on a context and then change the
25 // testing service mid-test. 33 // testing service mid-test.
26 BrowserContextShutdown(context); 34 BrowserContextShutdown(context);
27 BrowserContextDestroyed(context); 35 BrowserContextDestroyed(context);
28 36
29 if (add_context) 37 if (add_context)
30 MarkPreferencesSetOn(context); 38 MarkPreferencesSetOn(context);
39 }
31 40
32 factories_[context] = factory; 41 void BrowserContextKeyedServiceFactory::SetGlobalTestingFactory(
42 FactoryFunction factory) {
43 // Should not call SetGlobalTestingFactory() if factories already exist or
44 // if any services have been created already for any profile.
45 DCHECK(factories_.empty());
46
47 // Walk any existing contexts and clean up any services that have already
48 // been created.
49 while (!mapping_.empty()) {
50 BrowserContextKeyedServices::iterator it = mapping_.begin();
51 // Cleanup any existing services, and remove the service from |mapping_|.
52 CleanupExistingServices(it->first);
53 }
54
55 global_factory_ = factory;
56 have_global_factory_ = true;
57 }
58
59 void BrowserContextKeyedServiceFactory::ResetGlobalTestingFactory() {
60 have_global_factory_ = false;
33 } 61 }
34 62
35 BrowserContextKeyedService* 63 BrowserContextKeyedService*
36 BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse( 64 BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
37 content::BrowserContext* context, 65 content::BrowserContext* context,
38 FactoryFunction factory) { 66 FactoryFunction factory) {
39 DCHECK(factory); 67 DCHECK(factory);
40 SetTestingFactory(context, factory); 68 SetTestingFactory(context, factory);
41 return GetServiceForBrowserContext(context, true); 69 return GetServiceForBrowserContext(context, true);
42 } 70 }
43 71
44 BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory( 72 BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory(
45 const char* name, BrowserContextDependencyManager* manager) 73 const char* name, BrowserContextDependencyManager* manager)
46 : BrowserContextKeyedBaseFactory(name, manager) { 74 : BrowserContextKeyedBaseFactory(name, manager),
75 have_global_factory_(false) {
47 } 76 }
48 77
49 BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() { 78 BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() {
50 DCHECK(mapping_.empty()); 79 DCHECK(mapping_.empty());
51 } 80 }
52 81
53 BrowserContextKeyedService* 82 BrowserContextKeyedService*
54 BrowserContextKeyedServiceFactory::GetServiceForBrowserContext( 83 BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
55 content::BrowserContext* context, 84 content::BrowserContext* context,
56 bool create) { 85 bool create) {
57 context = GetBrowserContextToUse(context); 86 context = GetBrowserContextToUse(context);
58 if (!context) 87 if (!context)
59 return NULL; 88 return NULL;
60 89
61 // NOTE: If you modify any of the logic below, make sure to update the 90 // NOTE: If you modify any of the logic below, make sure to update the
62 // refcounted version in refcounted_context_keyed_service_factory.cc! 91 // refcounted version in refcounted_context_keyed_service_factory.cc!
63 BrowserContextKeyedServices::const_iterator it = mapping_.find(context); 92 BrowserContextKeyedServices::const_iterator it = mapping_.find(context);
64 if (it != mapping_.end()) 93 if (it != mapping_.end())
65 return it->second; 94 return it->second;
66 95
67 // Object not found. 96 // Object not found.
68 if (!create) 97 if (!create)
69 return NULL; // And we're forbidden from creating one. 98 return NULL; // And we're forbidden from creating one.
70 99
71 // Create new object. 100 // Create new object.
72 // Check to see if we have a per-BrowserContext testing factory that we should 101 // Check to see if we have a per-BrowserContext testing factory that we should
73 // use instead of default behavior. 102 // use instead of default behavior.
74 BrowserContextKeyedService* service = NULL; 103 BrowserContextKeyedService* service = NULL;
75 BrowserContextOverriddenFunctions::const_iterator jt = 104 bool have_factory = have_global_factory_;
76 factories_.find(context); 105 FactoryFunction factory = global_factory_;
77 if (jt != factories_.end()) { 106 if (!have_factory) {
78 if (jt->second) { 107 BrowserContextOverriddenFunctions::const_iterator jt =
108 factories_.find(context);
109 if (jt != factories_.end()) {
110 have_factory = true;
111 factory = jt->second;
112 }
113 }
114 if (have_factory) {
115 if (factory) {
79 if (!context->IsOffTheRecord()) 116 if (!context->IsOffTheRecord())
80 RegisterUserPrefsOnBrowserContext(context); 117 RegisterUserPrefsOnBrowserContext(context);
81 service = jt->second(context); 118 service = factory(context);
82 } 119 }
83 } else { 120 } else {
84 service = BuildServiceInstanceFor(context); 121 service = BuildServiceInstanceFor(context);
85 } 122 }
86 123
87 Associate(context, service); 124 Associate(context, service);
88 return service; 125 return service;
89 } 126 }
90 127
91 void BrowserContextKeyedServiceFactory::Associate( 128 void BrowserContextKeyedServiceFactory::Associate(
(...skipping 29 matching lines...) Expand all
121 158
122 void BrowserContextKeyedServiceFactory::SetEmptyTestingFactory( 159 void BrowserContextKeyedServiceFactory::SetEmptyTestingFactory(
123 content::BrowserContext* context) { 160 content::BrowserContext* context) {
124 SetTestingFactory(context, NULL); 161 SetTestingFactory(context, NULL);
125 } 162 }
126 163
127 void BrowserContextKeyedServiceFactory::CreateServiceNow( 164 void BrowserContextKeyedServiceFactory::CreateServiceNow(
128 content::BrowserContext* context) { 165 content::BrowserContext* context) {
129 GetServiceForBrowserContext(context, true); 166 GetServiceForBrowserContext(context, true);
130 } 167 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698