| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/keyed_service/content/refcounted_browser_context_keyed_serv
ice_factory.h" | 5 #include "components/keyed_service/content/refcounted_browser_context_keyed_serv
ice_factory.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "components/keyed_service/content/refcounted_browser_context_keyed_serv
ice.h" | 9 #include "components/keyed_service/content/refcounted_browser_context_keyed_serv
ice.h" |
| 10 #include "components/keyed_service/core/keyed_service.h" | 10 #include "components/keyed_service/core/keyed_service.h" |
| 11 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
| 12 | 12 |
| 13 void RefcountedBrowserContextKeyedServiceFactory::SetTestingFactory( | 13 void RefcountedBrowserContextKeyedServiceFactory::SetTestingFactory( |
| 14 content::BrowserContext* context, | 14 content::BrowserContext* context, |
| 15 TestingFactoryFunction testing_factory) { | 15 TestingFactoryFunction testing_factory) { |
| 16 if (!context) { |
| 17 testing_factories_[NULL] = testing_factory; |
| 18 return; |
| 19 } |
| 20 |
| 16 // Destroying the context may cause us to lose data about whether |context| | 21 // Destroying the context may cause us to lose data about whether |context| |
| 17 // has our preferences registered on it (since the context object itself | 22 // has our preferences registered on it (since the context object itself |
| 18 // isn't dead). See if we need to readd it once we've gone through normal | 23 // isn't dead). See if we need to readd it once we've gone through normal |
| 19 // destruction. | 24 // destruction. |
| 20 bool add_context = ArePreferencesSetOn(context); | 25 bool add_context = ArePreferencesSetOn(context); |
| 21 | 26 |
| 22 // We have to go through the shutdown and destroy mechanisms because there | 27 // We have to go through the shutdown and destroy mechanisms because there |
| 23 // are unit tests that create a service on a context and then change the | 28 // are unit tests that create a service on a context and then change the |
| 24 // testing service mid-test. | 29 // testing service mid-test. |
| 25 BrowserContextShutdown(context); | 30 BrowserContextShutdown(context); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 RefCountedStorage::const_iterator it = mapping_.find(context); | 69 RefCountedStorage::const_iterator it = mapping_.find(context); |
| 65 if (it != mapping_.end()) | 70 if (it != mapping_.end()) |
| 66 return it->second; | 71 return it->second; |
| 67 | 72 |
| 68 // Object not found. | 73 // Object not found. |
| 69 if (!create) | 74 if (!create) |
| 70 return NULL; // And we're forbidden from creating one. | 75 return NULL; // And we're forbidden from creating one. |
| 71 | 76 |
| 72 // Create new object. | 77 // Create new object. |
| 73 // Check to see if we have a per-BrowserContext testing factory that we should | 78 // Check to see if we have a per-BrowserContext testing factory that we should |
| 74 // use instead of default behavior. | 79 // use instead of default behavior. Check for a global factory as well. |
| 75 scoped_refptr<RefcountedBrowserContextKeyedService> service; | 80 scoped_refptr<RefcountedBrowserContextKeyedService> service; |
| 76 BrowserContextOverriddenTestingFunctions::const_iterator jt = | 81 BrowserContextOverriddenTestingFunctions::const_iterator jt = |
| 77 testing_factories_.find(context); | 82 testing_factories_.find(context); |
| 83 if (jt == testing_factories_.end()) |
| 84 jt = testing_factories_.find(NULL); |
| 78 if (jt != testing_factories_.end()) { | 85 if (jt != testing_factories_.end()) { |
| 79 if (jt->second) { | 86 if (jt->second) { |
| 80 if (!context->IsOffTheRecord()) | 87 if (!context->IsOffTheRecord()) |
| 81 RegisterUserPrefsOnBrowserContextForTest(context); | 88 RegisterUserPrefsOnBrowserContextForTest(context); |
| 82 service = jt->second(context); | 89 service = jt->second(context); |
| 83 } | 90 } |
| 84 } else { | 91 } else { |
| 85 service = BuildServiceInstanceFor(context); | 92 service = BuildServiceInstanceFor(context); |
| 86 } | 93 } |
| 87 | 94 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 127 |
| 121 void RefcountedBrowserContextKeyedServiceFactory::SetEmptyTestingFactory( | 128 void RefcountedBrowserContextKeyedServiceFactory::SetEmptyTestingFactory( |
| 122 content::BrowserContext* context) { | 129 content::BrowserContext* context) { |
| 123 SetTestingFactory(context, NULL); | 130 SetTestingFactory(context, NULL); |
| 124 } | 131 } |
| 125 | 132 |
| 126 void RefcountedBrowserContextKeyedServiceFactory::CreateServiceNow( | 133 void RefcountedBrowserContextKeyedServiceFactory::CreateServiceNow( |
| 127 content::BrowserContext* context) { | 134 content::BrowserContext* context) { |
| 128 GetServiceForBrowserContext(context, true); | 135 GetServiceForBrowserContext(context, true); |
| 129 } | 136 } |
| OLD | NEW |