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_PREDICTORS_RESOURCE_PREFETCHER_MANAGER_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_MANAGER_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "chrome/browser/predictors/resource_prefetcher.h" |
| 13 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 14 |
| 15 namespace net { |
| 16 class URLRequestContextGetter; |
| 17 } |
| 18 |
| 19 namespace predictors { |
| 20 |
| 21 struct NavigationID; |
| 22 class ResourcePrefetchPredictor; |
| 23 |
| 24 // Manages prefetches for multple navigations. |
| 25 // - Created and owned by the resource prefetch predictor. |
| 26 // - Needs to be refcounted as it is de-referenced on two different threads. |
| 27 // - Created on the UI thread, but most functions are called in the IO thread. |
| 28 // - Will only allow one inflight prefresh per main frame URL. |
| 29 class ResourcePrefetcherManager |
| 30 : public ResourcePrefetcher::Delegate, |
| 31 public base::RefCountedThreadSafe<ResourcePrefetcherManager> { |
| 32 public: |
| 33 // The |predictor| should be alive till ShutdownOnIOThread is called. |
| 34 ResourcePrefetcherManager(ResourcePrefetchPredictor* predictor, |
| 35 const ResourcePrefetchPredictorConfig& config, |
| 36 net::URLRequestContextGetter* getter); |
| 37 |
| 38 // UI thread. |
| 39 void ShutdownOnUIThread(); |
| 40 |
| 41 // --- IO Thread methods. |
| 42 |
| 43 // The prefetchers need to be deleted on the IO thread. |
| 44 void ShutdownOnIOThread(); |
| 45 |
| 46 // Will create a new ResourcePrefetcher for the main frame url of the input |
| 47 // navigation if there isn't one already for the same URL. |
| 48 void MaybeAddPrefetch(const NavigationID& navigation_id, |
| 49 scoped_ptr<ResourcePrefetcher::RequestVector> requests); |
| 50 |
| 51 // Stops the ResourcePrefetcher for the input navigation, if one was in |
| 52 // progress. |
| 53 void MaybeRemovePrefetch(const NavigationID& navigation_id); |
| 54 |
| 55 // ResourcePrefetcher::Delegate methods. |
| 56 virtual void ResourcePrefetcherFinished( |
| 57 ResourcePrefetcher* prefetcher, |
| 58 ResourcePrefetcher::RequestVector* requests) OVERRIDE; |
| 59 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE; |
| 60 |
| 61 private: |
| 62 friend class base::RefCountedThreadSafe<ResourcePrefetcherManager>; |
| 63 friend class MockResourcePrefetcherManager; |
| 64 |
| 65 typedef std::map<GURL, ResourcePrefetcher*> PrefetcherMap; |
| 66 |
| 67 virtual ~ResourcePrefetcherManager(); |
| 68 |
| 69 // UI Thread. |predictor_| needs to be called on the UI thread. |
| 70 void ResourcePrefetcherFinishedOnUI( |
| 71 const NavigationID& navigation_id, |
| 72 scoped_ptr<ResourcePrefetcher::RequestVector> requests); |
| 73 |
| 74 ResourcePrefetchPredictor* predictor_; |
| 75 const ResourcePrefetchPredictorConfig const config_; |
| 76 net::URLRequestContextGetter* const context_getter_; |
| 77 |
| 78 PrefetcherMap prefetcher_map_; // Owns the ResourcePrefetcher pointers. |
| 79 STLValueDeleter<PrefetcherMap> prefetcher_map_deleter_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcherManager); |
| 82 }; |
| 83 |
| 84 } // namespace predictors |
| 85 |
| 86 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_MANAGER_H_ |
OLD | NEW |