| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ | 5 #ifndef COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ |
| 6 #define COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ | 6 #define COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 // BrowserContext that it is constructed for. Use | 47 // BrowserContext that it is constructed for. Use |
| 48 // PrecacheManagerFactory::GetForBrowserContext to get an instance of this | 48 // PrecacheManagerFactory::GetForBrowserContext to get an instance of this |
| 49 // class. All methods must be called on the UI thread unless indicated | 49 // class. All methods must be called on the UI thread unless indicated |
| 50 // otherwise. | 50 // otherwise. |
| 51 // TODO(sclittle): Delete precache history when browsing history is deleted. | 51 // TODO(sclittle): Delete precache history when browsing history is deleted. |
| 52 // http://crbug.com/326549 | 52 // http://crbug.com/326549 |
| 53 class PrecacheManager : public KeyedService, | 53 class PrecacheManager : public KeyedService, |
| 54 public PrecacheFetcher::PrecacheDelegate, | 54 public PrecacheFetcher::PrecacheDelegate, |
| 55 public base::SupportsWeakPtr<PrecacheManager> { | 55 public base::SupportsWeakPtr<PrecacheManager> { |
| 56 public: | 56 public: |
| 57 typedef base::Closure PrecacheCompletionCallback; | 57 typedef base::Callback<void(bool)> PrecacheCompletionCallback; |
| 58 | 58 |
| 59 PrecacheManager(content::BrowserContext* browser_context, | 59 PrecacheManager(content::BrowserContext* browser_context, |
| 60 const sync_driver::SyncService* const sync_service); | 60 const sync_driver::SyncService* const sync_service); |
| 61 ~PrecacheManager() override; | 61 ~PrecacheManager() override; |
| 62 | 62 |
| 63 // Returns true if precaching is allowed for the browser context based on user | 63 // Returns true if precaching is allowed for the browser context based on user |
| 64 // settings, and enabled as part of a field trial or by commandline flag. | 64 // settings, and enabled as part of a field trial or by commandline flag. |
| 65 // Virtual for testing. | 65 // Virtual for testing. |
| 66 virtual bool ShouldRun() const; | 66 virtual bool ShouldRun() const; |
| 67 | 67 |
| 68 // Returns true if precaching is allowed for the browser context based on user | 68 // Returns true if precaching is allowed for the browser context based on user |
| 69 // settings. Virtual for testing. | 69 // settings. Virtual for testing. |
| 70 virtual bool WouldRun() const; | 70 virtual bool WouldRun() const; |
| 71 | 71 |
| 72 // Starts precaching resources that the user is predicted to fetch in the | 72 // Starts precaching resources that the user is predicted to fetch in the |
| 73 // future. If precaching is already currently in progress, then this method | 73 // future. If precaching is already currently in progress, then this method |
| 74 // does nothing. The |precache_completion_callback| will be run when | 74 // does nothing. The |precache_completion_callback| will be passed true when |
| 75 // precaching finishes, but will not be run if precaching is canceled. | 75 // precaching finishes, and passed false when precaching abort due to failed |
| 76 // preconditions, but will not be run if precaching is canceled. |
| 76 void StartPrecaching( | 77 void StartPrecaching( |
| 77 const PrecacheCompletionCallback& precache_completion_callback, | 78 const PrecacheCompletionCallback& precache_completion_callback, |
| 78 const history::HistoryService& history_service); | 79 const history::HistoryService& history_service); |
| 79 | 80 |
| 80 // Cancels precaching if it is in progress. | 81 // Cancels precaching if it is in progress. |
| 81 void CancelPrecaching(); | 82 void CancelPrecaching(); |
| 82 | 83 |
| 83 // Returns true if precaching is currently in progress, or false otherwise. | 84 // Returns true if precaching is currently in progress, or false otherwise. |
| 84 bool IsPrecaching() const; | 85 bool IsPrecaching() const; |
| 85 | 86 |
| 86 // Update precache-related metrics in response to a URL being fetched. | 87 // Update precache-related metrics in response to a URL being fetched. |
| 87 void RecordStatsForFetch(const GURL& url, | 88 void RecordStatsForFetch(const GURL& url, |
| 88 const base::Time& fetch_time, | 89 const base::Time& fetch_time, |
| 89 int64 size, | 90 int64 size, |
| 90 bool was_cached); | 91 bool was_cached); |
| 91 | 92 |
| 92 // Posts a task to the DB thread to delete all history entries from the | 93 // Posts a task to the DB thread to delete all history entries from the |
| 93 // database. Does not wait for completion of this task. | 94 // database. Does not wait for completion of this task. |
| 94 void ClearHistory(); | 95 void ClearHistory(); |
| 95 | 96 |
| 96 private: | 97 private: |
| 98 enum class AllowedType { |
| 99 ALLOWED, |
| 100 DISALLOWED, |
| 101 PENDING |
| 102 }; |
| 103 |
| 97 // From KeyedService. | 104 // From KeyedService. |
| 98 void Shutdown() override; | 105 void Shutdown() override; |
| 99 | 106 |
| 100 // From PrecacheFetcher::PrecacheDelegate. | 107 // From PrecacheFetcher::PrecacheDelegate. |
| 101 void OnDone() override; | 108 void OnDone() override; |
| 102 | 109 |
| 103 // From history::HistoryService::TopHosts. | 110 // From history::HistoryService::TopHosts. |
| 104 void OnHostsReceived(const history::TopHostsList& host_counts); | 111 void OnHostsReceived(const history::TopHostsList& host_counts); |
| 105 | 112 |
| 106 // Returns true if precaching is enabled as part of a field trial or by the | 113 // Returns true if precaching is enabled as part of a field trial or by the |
| 107 // command line flag. This has a different meaning from the | 114 // command line flag. This has a different meaning from the |
| 108 // "is_precaching_enabled" pref set in PrecacheServiceLauncher. This method | 115 // "is_precaching_enabled" pref set in PrecacheServiceLauncher. This method |
| 109 // can be called on any thread. | 116 // can be called on any thread. |
| 110 static bool IsPrecachingEnabled(); | 117 static bool IsPrecachingEnabled(); |
| 111 | 118 |
| 112 // Returns true if precaching is allowed for the browser context. | 119 // Returns true if precaching is allowed for the browser context. |
| 113 bool IsPrecachingAllowed() const; | 120 AllowedType PrecachingAllowed() const; |
| 114 | 121 |
| 115 // The browser context that owns this PrecacheManager. | 122 // The browser context that owns this PrecacheManager. |
| 116 content::BrowserContext* const browser_context_; | 123 content::BrowserContext* const browser_context_; |
| 117 | 124 |
| 118 // The sync service corresponding to the browser context. Used to determine | 125 // The sync service corresponding to the browser context. Used to determine |
| 119 // whether precache can run. May be null. | 126 // whether precache can run. May be null. |
| 120 const sync_driver::SyncService* const sync_service_; | 127 const sync_driver::SyncService* const sync_service_; |
| 121 | 128 |
| 122 // The PrecacheFetcher used to precache resources. Should only be used on the | 129 // The PrecacheFetcher used to precache resources. Should only be used on the |
| 123 // UI thread. | 130 // UI thread. |
| 124 scoped_ptr<PrecacheFetcher> precache_fetcher_; | 131 scoped_ptr<PrecacheFetcher> precache_fetcher_; |
| 125 | 132 |
| 126 // The callback that will be run if precaching finishes without being | 133 // The callback that will be run if precaching finishes without being |
| 127 // canceled. | 134 // canceled. |
| 128 PrecacheCompletionCallback precache_completion_callback_; | 135 PrecacheCompletionCallback precache_completion_callback_; |
| 129 | 136 |
| 130 // The PrecacheDatabase for tracking precache metrics. Should only be used on | 137 // The PrecacheDatabase for tracking precache metrics. Should only be used on |
| 131 // the DB thread. | 138 // the DB thread. |
| 132 const scoped_refptr<PrecacheDatabase> precache_database_; | 139 const scoped_refptr<PrecacheDatabase> precache_database_; |
| 133 | 140 |
| 134 // Flag indicating whether or not precaching is currently in progress. | 141 // Flag indicating whether or not precaching is currently in progress. |
| 135 bool is_precaching_; | 142 bool is_precaching_; |
| 136 | 143 |
| 137 DISALLOW_COPY_AND_ASSIGN(PrecacheManager); | 144 DISALLOW_COPY_AND_ASSIGN(PrecacheManager); |
| 138 }; | 145 }; |
| 139 | 146 |
| 140 } // namespace precache | 147 } // namespace precache |
| 141 | 148 |
| 142 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ | 149 #endif // COMPONENTS_PRECACHE_CONTENT_PRECACHE_MANAGER_H_ |
| OLD | NEW |