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