Home | History | Annotate | Download | only in predictors
      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
      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 "chrome/browser/predictors/resource_prefetch_common.h"
     12 #include "chrome/browser/predictors/resource_prefetcher.h"
     13 
     14 namespace net {
     15 class URLRequestContextGetter;
     16 }
     17 
     18 namespace predictors {
     19 
     20 struct NavigationID;
     21 class ResourcePrefetchPredictor;
     22 
     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> {
     31  public:
     32   // The |predictor| should be alive till ShutdownOnIOThread is called.
     33   ResourcePrefetcherManager(ResourcePrefetchPredictor* predictor,
     34                             const ResourcePrefetchPredictorConfig& config,
     35                             net::URLRequestContextGetter* getter);
     36 
     37   // UI thread.
     38   void ShutdownOnUIThread();
     39 
     40   // --- IO Thread methods.
     41 
     42   // The prefetchers need to be deleted on the IO thread.
     43   void ShutdownOnIOThread();
     44 
     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
     47   // based).
     48   void MaybeAddPrefetch(const NavigationID& navigation_id,
     49                         PrefetchKeyType key_type,
     50                         scoped_ptr<ResourcePrefetcher::RequestVector> requests);
     51 
     52   // Stops the ResourcePrefetcher for the input navigation, if one was in
     53   // progress.
     54   void MaybeRemovePrefetch(const NavigationID& navigation_id);
     55 
     56   // ResourcePrefetcher::Delegate methods.
     57   virtual void ResourcePrefetcherFinished(
     58       ResourcePrefetcher* prefetcher,
     59       ResourcePrefetcher::RequestVector* requests) OVERRIDE;
     60   virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE;
     61 
     62  private:
     63   friend class base::RefCountedThreadSafe<ResourcePrefetcherManager>;
     64   friend class MockResourcePrefetcherManager;
     65 
     66   typedef std::map<std::string, ResourcePrefetcher*> PrefetcherMap;
     67 
     68   virtual ~ResourcePrefetcherManager();
     69 
     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);
     75 
     76   ResourcePrefetchPredictor* predictor_;
     77   const ResourcePrefetchPredictorConfig config_;
     78   net::URLRequestContextGetter* const context_getter_;
     79 
     80   PrefetcherMap prefetcher_map_;  // Owns the ResourcePrefetcher pointers.
     81 
     82   DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcherManager);
     83 };
     84 
     85 }  // namespace predictors
     86 
     87 #endif  // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_MANAGER_H_
     88