Index: chrome/browser/predictors/resource_prefetch_predictor.h |
diff --git a/chrome/browser/predictors/resource_prefetch_predictor.h b/chrome/browser/predictors/resource_prefetch_predictor.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..99fd0c93f25d3d7d969fac3f663e9847f7476d07 |
--- /dev/null |
+++ b/chrome/browser/predictors/resource_prefetch_predictor.h |
@@ -0,0 +1,160 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
+#define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
+#pragma once |
+ |
+#include <map> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/time.h" |
+#include "chrome/browser/predictors/resource_prefetch_common.h" |
+#include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
+#include "chrome/browser/profiles/profile_keyed_service.h" |
+#include "content/public/browser/notification_observer.h" |
+#include "content/public/browser/notification_registrar.h" |
+#include "googleurl/src/gurl.h" |
+#include "webkit/glue/resource_type.h" |
+ |
+class PredictorsHandler; |
+class Profile; |
+ |
+namespace content { |
+class WebContents; |
+} |
+ |
+namespace history { |
+class URLrowsDatabase; |
+} |
+ |
+namespace net { |
+class URLRequest; |
+} |
+ |
+namespace predictors { |
+ |
+// Contains logic for learning what can be prefetched and for kicking off |
+// speculative prefetching. |
+// - The class is a profile keyed service owned by the profile. |
+// - All the non-static methods of this class need to be called on the UI |
+// thread. |
+// |
+// The overall flow of the resource prefetching algorithm is as follows: |
+// |
+// * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and |
+// redirects on the IO thread(via RDHostDelegate) and post tasks to the |
+// ResourcePrefetchPredictor on the UI thread. This is owned by the |
+// ProfileIOData for the profile. |
+// * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data |
+// to a sql database. Runs entirely on the DB thread. Owned by the |
+// PredictorDatabase. |
+// * ResourcePrefetchPredictor - Learns about resource requirements per URL in |
+// the UI thread through the ResourcePrefetchPredictorObserver and perisists |
+// it to disk in the DB thread through the ResourcePrefetchPredictorTables. |
+// Owned by profile. |
+// |
+// TODO(shishir): Implement the prefetching of resources. |
+class ResourcePrefetchPredictor |
+ : public ProfileKeyedService, |
+ public content::NotificationObserver, |
+ public base::SupportsWeakPtr<ResourcePrefetchPredictor> { |
+ public: |
+ explicit ResourcePrefetchPredictor(Profile* profile); |
+ virtual ~ResourcePrefetchPredictor(); |
+ |
+ // Stores the data that we need to get from the URLRequest. |
+ struct URLRequestSummary { |
+ URLRequestSummary(); |
+ URLRequestSummary(const URLRequestSummary& other); |
+ ~URLRequestSummary(); |
+ bool InitFromURLRequest(net::URLRequest* request, bool is_response); |
willchan no longer on Chromium
2012/05/31 02:08:58
Why is this defined here? It seems to only be used
Shishir
2012/06/02 01:19:29
Done.
|
+ |
+ NavigationID navigation_id_; |
willchan no longer on Chromium
2012/05/31 02:08:58
Remove the trailing underscores for structs.
Shishir
2012/06/02 01:19:29
Done.
|
+ GURL resource_url_; |
+ ResourceType::Type resource_type_; |
+ |
+ // Only for responses. |
+ std::string mime_type_; |
+ bool was_cached_; |
+ }; |
+ |
+ // Thread safe. |
+ static bool IsEnabled(); |
+ static bool ShouldRecordRequest(net::URLRequest* request, |
+ ResourceType::Type resource_type); |
+ static bool ShouldRecordResponse(net::URLRequest* response); |
+ static bool ShouldRecordRedirect(net::URLRequest* response); |
+ |
+ // UI thread. |
willchan no longer on Chromium
2012/05/31 02:08:58
Everything's on the IO thread, right? Other than t
Shishir
2012/06/02 01:19:29
Done.
|
+ void RecordURLRequest(const URLRequestSummary& request); |
+ void RecordUrlResponse(const URLRequestSummary& response); |
+ void RecordUrlRedirect(const URLRequestSummary& response); |
+ |
+ private: |
+ friend class ::PredictorsHandler; |
willchan no longer on Chromium
2012/05/31 02:08:58
Hm...do you really need this? AFAICT, this is only
Shishir
2012/06/02 01:19:29
In the following CLs, we will be exposing much mor
|
+ |
+ // TODO(shishir): Maybe use pointers to make the sort cheaper. |
+ typedef ResourcePrefetchPredictorTables::UrlTableRow UrlTableRow; |
+ typedef std::vector<UrlTableRow> UrlTableRowVector; |
+ |
+ struct UrlTableCacheValue { |
+ UrlTableRowVector rows_; |
+ base::Time last_visit_; |
+ }; |
+ |
+ typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; |
+ typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; |
+ |
+ // content::NotificationObserver methods OVERRIDE. |
+ virtual void Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) OVERRIDE; |
+ |
+ static bool IsHandledMainPage(net::URLRequest* request); |
+ static bool IsHandledSubresource(net::URLRequest* response); |
+ static bool IsCacheable(const net::URLRequest* response); |
+ |
+ // Deal with different kinds of requests. |
+ void OnMainFrameRequest(const URLRequestSummary& request); |
+ void OnMainFrameResponse(const URLRequestSummary& response); |
+ void OnMainFrameRedirect(const URLRequestSummary& response); |
+ void OnSubresourceResponse(const URLRequestSummary& response); |
+ void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id, |
+ const GURL& resource_url); |
+ |
+ void LazilyInitialize(); |
+ void OnHistoryAndCacheLoaded(); |
+ void CreateCaches(std::vector<UrlTableRow>* url_rows); |
+ bool ShouldTrackUrl(const GURL& url); |
+ void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
+ void OnNavigationComplete(const NavigationID& navigation_id); |
+ void LearnUrlNavigation(const GURL& main_frame_url, |
+ const std::vector<URLRequestSummary>& new_value); |
+ void RemoveAnEntryFromUrlDB(); |
+ void MaybeReportAccuracyStats(const NavigationID& navigation_id); |
+ |
+ enum InitializationState { |
willchan no longer on Chromium
2012/05/31 02:08:58
types go in the beginning of the access section, p
Shishir
2012/06/02 01:19:29
Done.
|
+ NOT_INITIALIZED = 0, |
+ INITIALIZING = 1, |
+ INITIALIZED = 2 |
+ }; |
+ |
+ Profile* profile_; |
willchan no longer on Chromium
2012/05/31 02:08:58
Profile* const? Should never change, right?
Shishir
2012/06/02 01:19:29
Done.
|
+ InitializationState initialization_state_; |
+ scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
willchan no longer on Chromium
2012/05/31 02:08:58
Doesn't this class own the ResourcePrefetchPredict
Shishir
2012/06/02 01:19:29
I would like to defer this to another CL since thi
|
+ scoped_ptr<content::NotificationRegistrar> notification_registrar_; |
+ |
+ NavigationMap inflight_navigations_; |
+ UrlTableCacheMap url_table_cache_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
+}; |
+ |
+} // namespace predictors |
+ |
+#endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |