1 // Copyright 2019 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 COMPONENTS_PERFORMANCE_MANAGER_PERSISTENCE_SITE_DATA_SITE_DATA_CACHE_FACTORY_H_ 6 #define COMPONENTS_PERFORMANCE_MANAGER_PERSISTENCE_SITE_DATA_SITE_DATA_CACHE_FACTORY_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "base/callback.h" 12 #include "base/containers/flat_map.h" 13 #include "base/files/file_path.h" 14 #include "base/location.h" 15 #include "base/macros.h" 16 #include "base/optional.h" 17 #include "base/sequence_checker.h" 18 #include "base/sequenced_task_runner.h" 19 #include "components/performance_manager/persistence/site_data/site_data_cache.h" 20 #include "content/public/browser/browser_context.h" 21 22 namespace content { 23 class BrowserContext; 24 } 25 26 namespace performance_manager { 27 28 class SiteDataCacheInspector; 29 30 // This class is responsible for tracking the SiteDataCache instances associated 31 // with each browser context. Instances of this class should be created and used 32 // from the same sequence (this is enforced via a sequence checker). It is the 33 // counterpart of the SiteDataCacheFacadeFactory living on the UI thread. 34 class SiteDataCacheFactory { 35 public: 36 SiteDataCacheFactory(); 37 ~SiteDataCacheFactory(); 38 39 // Returns a pointer to the global instance. 40 static SiteDataCacheFactory* GetInstance(); 41 42 // Returns a pointer to the data cache associated with |browser_context_id|, 43 // or null if there's no cache for this context yet. 44 SiteDataCache* GetDataCacheForBrowserContext( 45 const std::string& browser_context_id) const; 46 47 // Returns the data cache inspector associated with |browser_context_id|, or 48 // null if there's no data cache inspector for this context yet. 49 SiteDataCacheInspector* GetInspectorForBrowserContext( 50 const std::string& browser_context_id) const; 51 52 // Sets the inspector instance associated with a given browser context. 53 // If |inspector| is nullptr the association is cleared. 54 // The caller must ensure that |inspector|'s registration is cleared before 55 // |inspector| or |browser_context| are deleted. 56 // The intent is for this to be called from the SiteDataCache implementation 57 // class' constructors and destructors. 58 void SetDataCacheInspectorForBrowserContext( 59 SiteDataCacheInspector* inspector, 60 const std::string& browser_context_id); 61 62 // Testing functions to check if the data cache associated with 63 // |browser_context_id| is recording. 64 bool IsDataCacheRecordingForTesting(const std::string& browser_context_id); 65 66 // Set the cache for a given browser context, this will replace any existing 67 // cache. 68 void SetCacheForTesting(const std::string& browser_context_id, 69 std::unique_ptr<SiteDataCache> cache); 70 71 void SetCacheInspectorForTesting(const std::string& browser_context_id, 72 SiteDataCacheInspector* inspector); 73 74 // Implementation of the corresponding *OnUIThread public static functions 75 // that runs on this object's task runner. 76 void OnBrowserContextCreated(const std::string& browser_context_id, 77 const base::FilePath& context_path, 78 base::Optional<std::string> parent_context_id); 79 void OnBrowserContextDestroyed(const std::string& browser_context_id); 80 81 private: 82 // A map that associates a BrowserContext's ID with a SiteDataCache. This 83 // object owns the caches. 84 base::flat_map<std::string, std::unique_ptr<SiteDataCache>> data_cache_map_; 85 86 // A map that associates a BrowserContext's ID with a SiteDataCacheInspector. 87 base::flat_map<std::string, SiteDataCacheInspector*> 88 data_cache_inspector_map_; 89 90 SEQUENCE_CHECKER(sequence_checker_); 91 92 DISALLOW_COPY_AND_ASSIGN(SiteDataCacheFactory); 93 }; 94 95 } // namespace performance_manager 96 97 #endif // COMPONENTS_PERFORMANCE_MANAGER_PERSISTENCE_SITE_DATA_SITE_DATA_CACHE_FACTORY_H_ 98