Chromium Code Reviews| Index: chrome/browser/predictors/resource_prefetcher.h |
| diff --git a/chrome/browser/predictors/resource_prefetcher.h b/chrome/browser/predictors/resource_prefetcher.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e01ab600fda6759d67b2fb8f1b1dc621ca0dfa71 |
| --- /dev/null |
| +++ b/chrome/browser/predictors/resource_prefetcher.h |
| @@ -0,0 +1,141 @@ |
| +// 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_PREFETCHER_H_ |
| +#define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| + |
| +#include <map> |
| +#include <list> |
| +#include <vector> |
| + |
| +#include "base/memory/linked_ptr.h" |
| +#include "base/threading/non_thread_safe.h" |
| +#include "chrome/browser/predictors/resource_prefetch_common.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "net/url_request/url_request.h" |
| + |
| +namespace net { |
| +class URLRequestContext; |
| +} |
| + |
| +namespace predictors { |
| + |
| +// Responsible for prefetching resources for a single navigation based on the |
| +// input list of resources. |
| +// - Limits the max number of resources in flight for any host and also across |
| +// hosts. |
| +// - When stopped, will wait for the pending requests to finish. |
| +// - Lives entirely on the IO thread. |
| +class ResourcePrefetcher : public base::NonThreadSafe, |
| + public net::URLRequest::Delegate { |
| + public: |
| + // Denotes the prefetch request for a single subresource. |
| + struct Request { |
| + explicit Request(const GURL& i_resource_url); |
| + Request(const Request& other); |
| + |
| + enum PrefetchStatus { |
| + PREFETCH_STATUS_NOT_STARTED, |
| + PREFETCH_STATUS_STARTED, |
| + PREFETCH_STATUS_CANCELLED, |
| + PREFETCH_STATUS_FAILED, |
| + PREFETCH_STATUS_FROM_CACHE, |
| + PREFETCH_STATUS_FROM_NETWORK |
| + }; |
| + |
| + enum UsageStatus { |
| + USAGE_STATUS_NOT_REQUESTED, |
| + USAGE_STATUS_FROM_CACHE, |
| + USAGE_STATUS_FROM_NETWORK, |
| + USAGE_STATUS_NAVIGATION_ABANDONED |
| + }; |
| + |
| + GURL resource_url; |
| + PrefetchStatus prefetch_status; |
| + UsageStatus usage_status; |
| + }; |
| + typedef std::vector<linked_ptr<Request> > RequestVector; |
|
dominich
2012/07/23 16:04:41
This being public makes it more difficult, but I t
Shishir
2012/08/01 22:35:24
Changed to ScopedVector.
|
| + |
| + // Used to communicate when the prefetching is done. All methods are invoked |
| + // on the IO thread. |
| + class Delegate { |
| + public: |
| + virtual ~Delegate() { } |
| + |
| + // Called when the ResourcePrefetcher is finished, i.e. there is nothing |
| + // pending in flight. |
| + virtual void ResourcePrefetcherFinished( |
| + ResourcePrefetcher* prefetcher, |
| + scoped_ptr<RequestVector> requests) = 0; |
| + |
| + virtual net::URLRequestContext* GetURLRequestContext() = 0; |
| + }; |
| + |
| + // 'delegate' has to outlive the ResourcePrefetcher. |
| + ResourcePrefetcher(Delegate* delegate, |
|
dominich
2012/07/23 16:04:41
I think this should be an explicit dependency rath
dominich
2012/07/23 16:04:41
comment that this takes ownership of requests.
Shishir
2012/08/01 22:35:24
Done.
Shishir
2012/08/01 22:35:24
Done.
|
| + const ResourcePrefetchPredictorConfig& config, |
| + const NavigationID& navigation_id, |
| + scoped_ptr<RequestVector> requests); |
| + virtual ~ResourcePrefetcher(); |
| + |
| + void Start(); // Kicks off the prefetching. Can only be called once. |
| + void Stop(); // No additional prefetches will be queued after this. |
| + |
| + const NavigationID& navigation_id() const { |
| + return navigation_id_; |
| + } |
| + |
| + private: |
| + // Launches new prefetch requests if possible. |
| + void TryToLaunchPrefetchRequests(); |
| + |
| + // Starts a net::URLRequest for the input 'request'. |
| + void SendRequest(Request* request); |
| + |
| + // Marks the request as finished, with the given status. |
| + void FinishRequest(net::URLRequest* request, Request::PrefetchStatus status); |
| + |
| + // Reads the response data from the response - required for the resource to |
| + // be cached correctly. |
| + void ReadFullResponse(net::URLRequest* request); |
| + |
| + // net::URLRequest::Delegate methods. |
| + virtual void OnReceivedRedirect(net::URLRequest* request, |
| + const GURL& new_url, |
| + bool* defer_redirect) OVERRIDE; |
| + virtual void OnAuthRequired(net::URLRequest* request, |
| + net::AuthChallengeInfo* auth_info) OVERRIDE; |
| + virtual void OnCertificateRequested( |
| + net::URLRequest* request, |
| + net::SSLCertRequestInfo* cert_request_info) OVERRIDE; |
| + virtual void OnSSLCertificateError(net::URLRequest* request, |
| + const net::SSLInfo& ssl_info, |
| + bool fatal) OVERRIDE; |
| + virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; |
| + virtual void OnReadCompleted(net::URLRequest* request, |
| + int bytes_read) OVERRIDE; |
| + |
| + enum PrefetcherState { |
| + INITIALIZED = 0, // Prefetching hasn't started. |
| + RUNNING = 1, // Prefetching started, allowed to add more requests. |
| + STOPPED = 2, // Prefetching started, not allowed to add more requests. |
| + FINISHED = 3 // No more inflight request, new requests not possible. |
| + }; |
| + |
| + PrefetcherState state_; |
| + Delegate* const delegate_; |
| + ResourcePrefetchPredictorConfig config_; |
| + NavigationID navigation_id_; |
| + scoped_ptr<RequestVector> request_vector_; |
| + |
| + std::map<net::URLRequest*, Request*> inflight_requests_; |
| + std::list<Request*> request_queue_; |
| + std::map<std::string, int> host_inflight_counts_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); |
| +}; |
| + |
| +} // namespace predictors |
| + |
| +#endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |