Chromium Code Reviews| 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..484b29995e83e72ca226cc577fd0455a4cd0d29a |
| --- /dev/null |
| +++ b/chrome/browser/predictors/resource_prefetch_predictor.h |
| @@ -0,0 +1,152 @@ |
| +// 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/time.h" |
| +#include "chrome/browser/predictors/resource_prefetch_common.h" |
| +#include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| +#include "chrome/browser/profiles/refcounted_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 needs to be refcounted as the ownership is shared between the |
| +// Profile and the ResourcePrefetchPredictorObserver class on the UI and the |
| +// IO threads respectively. During initialization, the DB thread also needs to |
| +// call this class as a part of the PostTaskAndReply call. |
| +// - Most of the methods in the class (including the constructor) have to be |
| +// called on the UI thread except the static functions that are thread safe. |
| +// |
| +// The overall flow of the resource prefetching algorithm is as follows: |
| +// |
| +// * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and |
|
dominich
2012/05/24 16:07:53
who owns this?
Shishir
2012/05/30 01:07:51
Done.
|
| +// redirects on the IO thread(via NetworkDelegate) and post tasks to the |
| +// ResourcePrefetchPredictor on the UI thread. |
| +// * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data |
| +// to a sql database. Runs entirely on the DB thread. |
| +// * ResourcePrefetchPredictor - Learns about resource requirements per URL in |
|
dominich
2012/05/24 16:07:53
document who owns this
Shishir
2012/05/30 01:07:51
Done.
|
| +// the UI thread through the ResourcePrefetchPredictorObserver and perisists |
| +// it to disk in the DB thread through the ResourcePrefetchPredictorTables |
| +// |
| +// NOTE: The acutal prefetching is not yet implemented. |
| +class ResourcePrefetchPredictor : public content::NotificationObserver, |
| + public RefcountedProfileKeyedService { |
| + public: |
| + explicit ResourcePrefetchPredictor(Profile* profile); |
| + virtual ~ResourcePrefetchPredictor(); |
|
willchan no longer on Chromium
2012/05/24 22:28:52
If this is refcounted...should this be private?
Shishir
2012/05/30 01:07:51
No longer refcounted.
|
| + |
| + // 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); |
| + 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; |
| + |
| + // RefcountedProfileKeyedService methods OVERRIDE. |
| + virtual void ShutdownOnUIThread() OVERRIDE; |
| + |
| + // 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 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); |
| + |
| + Profile* profile_; |
| + bool initialized_; |
| + 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_ |