1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_ 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <set> 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/threading/non_thread_safe.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/dependency_node.h" 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service_export.h" 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class BrowserContextDependencyManager; 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class PrefService; 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace content { 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class BrowserContext; 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace user_prefs { 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class PrefRegistrySyncable; 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Base class for Factories that take a BrowserContext object and return some 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// service. 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Unless you're trying to make a new type of Factory, you probably don't want 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// this class, but its subclasses: BrowserContextKeyedServiceFactory and 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// RefcountedBrowserContextKeyedServiceFactory. This object describes general 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// dependency management between Factories; subclasses react to lifecycle 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// events and implement memory management. 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class KEYED_SERVICE_EXPORT BrowserContextKeyedBaseFactory 347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) : public base::NonThreadSafe, 357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) NON_EXPORTED_BASE(public DependencyNode) { 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Registers preferences used in this service on the pref service of 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // |context|. This is the public interface and is safe to be called multiple 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // times because testing code can have multiple services of the same type 40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // attached to a single |context|. Only test code is allowed to call this 41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // method. 42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // TODO(gab): This method can be removed entirely when 43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // PrefService::DeprecatedGetPrefRegistry() is phased out. 44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) void RegisterUserPrefsOnBrowserContextForTest( 45a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) content::BrowserContext* context); 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef NDEBUG 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns our name. We don't keep track of this in release mode. 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* name() const { return service_name_; } 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) protected: 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) BrowserContextKeyedBaseFactory(const char* name, 5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) BrowserContextDependencyManager* manager); 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual ~BrowserContextKeyedBaseFactory(); 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The main public interface for declaring dependencies between services 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // created by factories. 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void DependsOn(BrowserContextKeyedBaseFactory* rhs); 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Calls RegisterProfilePrefs() after doing house keeping required to work 62a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // alongside RegisterUserPrefsOnBrowserContextForTest(). 63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // TODO(gab): This method can be replaced by RegisterProfilePrefs() directly 64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // once RegisterUserPrefsOnBrowserContextForTest() is phased out. 65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) void RegisterProfilePrefsIfNecessaryForContext( 66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const content::BrowserContext* context, 67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) user_prefs::PrefRegistrySyncable* registry); 68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Interface for people building a concrete FooServiceFactory: -------------- 7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Finds which browser context (if any) to use. 7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual content::BrowserContext* GetBrowserContextToUse( 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) content::BrowserContext* context) const; 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // By default, we create instances of a service lazily and wait until 7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // GetForBrowserContext() is called on our subclass. Some services need to be 7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // created as soon as the BrowserContext has been brought up. 7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual bool ServiceIsCreatedWithBrowserContext() const; 7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // By default, TestingBrowserContexts will be treated like normal contexts. 8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // You can override this so that by default, the service associated with the 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // TestingBrowserContext is NULL. (This is just a shortcut around 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // SetTestingFactory() to make sure our contexts don't directly refer to the 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // services they use.) 8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual bool ServiceIsNULLWhileTesting() const; 8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Interface for people building a type of BrowserContextKeyedFactory: ------- 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // A helper object actually listens for notifications about BrowserContext 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // destruction, calculates the order in which things are destroyed and then 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // does a two pass shutdown. 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // It is up to the individual factory types to determine what this two pass 9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // shutdown means. The general framework guarantees the following: 9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // 9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // - Each BrowserContextShutdown() is called in dependency order (and you may 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // reach out to other services during this phase). 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // 9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // - Each BrowserContextDestroyed() is called in dependency order. We will 10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // NOTREACHED() if you attempt to GetForBrowserContext() any other service. 10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // You should delete/deref/do other final memory management things during 10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // this phase. You must also call the base class method as the last thing 10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // you do. 10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual void BrowserContextShutdown(content::BrowserContext* context) = 0; 10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual void BrowserContextDestroyed(content::BrowserContext* context); 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Returns whether we've registered the preferences on this context. 10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool ArePreferencesSetOn(content::BrowserContext* context) const; 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Mark context as Preferences set. 11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void MarkPreferencesSetOn(content::BrowserContext* context); 11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Which BrowserContextDependencyManager we should communicate with. 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // In real code, this will always be 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // BrowserContextDependencyManager::GetInstance(), but unit tests will want 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // to use their own copy. 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) BrowserContextDependencyManager* dependency_manager_; 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private: 12090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) friend class BrowserContextDependencyManager; 12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) friend class BrowserContextDependencyManagerUnittests; 12290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Registers any user preferences on this service. This is called by 124a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // RegisterProfilePrefsIfNecessary() and should be overriden by any service 125a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // that wants to register profile-specific preferences. 126a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) virtual void RegisterProfilePrefs( 127a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) user_prefs::PrefRegistrySyncable* registry) {} 128a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // These two methods are for tight integration with the 13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // BrowserContextDependencyManager. 13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 13290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Because of ServiceIsNULLWhileTesting(), we need a way to tell different 13390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // subclasses that they should disable testing. 13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual void SetEmptyTestingFactory(content::BrowserContext* context) = 0; 13590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Returns true if a testing factory function has been set for |context|. 137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual bool HasTestingFactory(content::BrowserContext* context) = 0; 138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // We also need a generalized, non-returning method that generates the object 14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // now for when we're creating the context. 14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual void CreateServiceNow(content::BrowserContext* context) = 0; 14290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 14390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // BrowserContexts that have this service's preferences registered on them. 144a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) std::set<const content::BrowserContext*> registered_preferences_; 14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if !defined(NDEBUG) 14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // A static string passed in to our constructor. Should be unique across all 14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // services. This is used only for debugging in debug mode. (We can print 14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // pretty graphs with GraphViz with this information.) 15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const char* service_name_; 15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif 15290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_ 155