Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6104)

Unified Diff: chrome/browser/predictors/resource_prefetch_predictor.h

Issue 10416002: Seculative resource prefetching for URLs CL. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Moving to WeakPtrs and RVHD. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..499ed0ec877e9ab06081c685652f4f90e2632574
--- /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 pofile.
dominich 2012/05/30 15:35:01 nit: profile
Shishir 2012/05/30 18:07:15 Done.
+// - 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 NetworkDelegate) and post tasks to the
+// ResourcePrefetchPredictor on the UI thread. This is owned by the
+// ProfileIOData for the profile.
dominich 2012/05/30 15:35:01 note for willchan: Do you think we need a ProfileI
+// * 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.
+//
+// NOTE: The acutal prefetching is not yet implemented.
dominich 2012/05/30 15:35:01 Can you make this a TODO(shishir)?
Shishir 2012/05/30 18:07:15 Done.
+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);
+
+ NavigationID navigation_id_;
+ 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.
+ void RecordURLRequest(const URLRequestSummary& request);
+ void RecordUrlResponse(const URLRequestSummary& response);
+ void RecordUrlRedirect(const URLRequestSummary& response);
+
+ private:
+ friend class ::PredictorsHandler;
+
+ // 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 {
+ NOT_INITIALIZED = 0,
+ INITIALIZING = 1,
+ INITIALIZED = 2
+ };
+
+ Profile* profile_;
+ InitializationState initialization_state_;
+ scoped_refptr<ResourcePrefetchPredictorTables> tables_;
+ 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_

Powered by Google App Engine
This is Rietveld 408576698