1 // Copyright 2013 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_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ 6 #define COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ 7 8 #include <list> 9 10 #include "base/callback.h" 11 #include "base/compiler_specific.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "components/keyed_service/core/keyed_service.h" 16 #include "components/precache/core/precache_fetcher.h" 17 #include "url/gurl.h" 18 19 namespace base { 20 class Time; 21 } 22 23 namespace content { 24 class BrowserContext; 25 } 26 27 namespace precache { 28 29 class PrecacheDatabase; 30 class URLListProvider; 31 32 // Class that manages all precaching-related activities. Owned by the 33 // BrowserContext that it is constructed for. Use 34 // PrecacheManagerFactory::GetForBrowserContext to get an instance of this 35 // class. All methods must be called on the UI thread unless indicated 36 // otherwise. 37 // TODO(sclittle): Delete precache history when browsing history is deleted. 38 // http://crbug.com/326549 39 class PrecacheManager : public KeyedService, 40 public PrecacheFetcher::PrecacheDelegate, 41 public base::SupportsWeakPtr<PrecacheManager> { 42 public: 43 typedef base::Closure PrecacheCompletionCallback; 44 45 explicit PrecacheManager(content::BrowserContext* browser_context); 46 virtual ~PrecacheManager(); 47 48 // Returns true if precaching is enabled as part of a field trial or by the 49 // command line flag. This method can be called on any thread. 50 static bool IsPrecachingEnabled(); 51 52 // Returns true if precaching is allowed for the browser context. 53 bool IsPrecachingAllowed(); 54 55 // Starts precaching resources that the user is predicted to fetch in the 56 // future. If precaching is already currently in progress, then this method 57 // does nothing. The |precache_completion_callback| will be run when 58 // precaching finishes, but will not be run if precaching is canceled. 59 void StartPrecaching( 60 const PrecacheCompletionCallback& precache_completion_callback, 61 URLListProvider* url_list_provider); 62 63 // Cancels precaching if it is in progress. 64 void CancelPrecaching(); 65 66 // Returns true if precaching is currently in progress, or false otherwise. 67 bool IsPrecaching() const; 68 69 // Update precache-related metrics in response to a URL being fetched. 70 void RecordStatsForFetch(const GURL& url, 71 const base::Time& fetch_time, 72 int64 size, 73 bool was_cached); 74 75 private: 76 // From KeyedService. 77 virtual void Shutdown() OVERRIDE; 78 79 // From PrecacheFetcher::PrecacheDelegate. 80 virtual void OnDone() OVERRIDE; 81 82 void OnURLsReceived(const std::list<GURL>& urls); 83 84 // The browser context that owns this PrecacheManager. 85 content::BrowserContext* browser_context_; 86 87 // The PrecacheFetcher used to precache resources. Should only be used on the 88 // UI thread. 89 scoped_ptr<PrecacheFetcher> precache_fetcher_; 90 91 // The callback that will be run if precaching finishes without being 92 // canceled. 93 PrecacheCompletionCallback precache_completion_callback_; 94 95 // The PrecacheDatabase for tracking precache metrics. Should only be used on 96 // the DB thread. 97 scoped_refptr<PrecacheDatabase> precache_database_; 98 99 // Flag indicating whether or not precaching is currently in progress. 100 bool is_precaching_; 101 102 DISALLOW_COPY_AND_ASSIGN(PrecacheManager); 103 }; 104 105 } // namespace precache 106 107 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ 108