Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // TODO(rdsmith): This class needs to delegate URLRequest::Delegate methods | 5 // TODO(rdsmith): This class needs to delegate URLRequest::Delegate methods |
| 6 // to the net/ embedder for correct implementation of authentication. | 6 // to the net/ embedder for correct implementation of authentication. |
| 7 // Specifically, this class needs the embedder to provide functionality | 7 // Specifically, this class needs the embedder to provide functionality |
| 8 // corresponding to | 8 // corresponding to |
| 9 // URLRequest::Delegate::{OnAuthRequired,OnCertificateRequested}. | 9 // URLRequest::Delegate::{OnAuthRequired,OnCertificateRequested}. |
| 10 | 10 |
| 11 #ifndef NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ | 11 #ifndef NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ |
| 12 #define NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ | 12 #define NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ |
| 13 | 13 |
| 14 #include <queue> | 14 #include <queue> |
| 15 #include <set> | 15 #include <set> |
|
mmenke
2015/02/26 17:27:20
These both can be moved into the CC file, unless y
Elly Fong-Jones
2015/03/03 21:37:48
Done.
| |
| 16 #include <string> | 16 #include <string> |
| 17 | 17 |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 22 #include "base/threading/non_thread_safe.h" | 22 #include "base/threading/non_thread_safe.h" |
| 23 #include "net/base/sdch_manager.h" | 23 #include "net/base/sdch_manager.h" |
| 24 #include "net/url_request/url_fetcher_delegate.h" | 24 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "net/url_request/url_request.h" | 25 #include "net/url_request/url_request.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 namespace net { | 28 namespace net { |
| 29 | 29 |
| 30 class BoundNetLog; | 30 class BoundNetLog; |
| 31 class URLRequest; | 31 class URLRequest; |
| 32 class URLRequestThrottlerEntryInterface; | 32 class URLRequestThrottlerEntryInterface; |
| 33 | 33 |
| 34 namespace { | |
| 35 class UniqueFetchQueue; | |
|
mmenke
2015/02/26 17:27:21
Shouldn't put anonymous namespaces in header files
Elly Fong-Jones
2015/03/03 21:37:48
Done.
| |
| 36 } | |
| 37 | |
| 34 // This class is used by embedder SDCH policy object to fetch | 38 // This class is used by embedder SDCH policy object to fetch |
| 35 // dictionaries. It queues requests for dictionaries and dispatches | 39 // dictionaries. It queues requests for dictionaries and dispatches |
| 36 // them serially, implementing the URLRequest::Delegate interface to | 40 // them serially, implementing the URLRequest::Delegate interface to |
| 37 // handle callbacks (but see above TODO). It tracks all requests, only | 41 // handle callbacks (but see above TODO). It tracks all requests, only |
| 38 // attempting to fetch each dictionary once. | 42 // attempting to fetch each dictionary once. |
| 39 class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, | 43 class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, |
| 40 public base::NonThreadSafe { | 44 public base::NonThreadSafe { |
| 41 public: | 45 public: |
| 42 typedef base::Callback<void(const std::string& dictionary_text, | 46 typedef base::Callback<void(const std::string& dictionary_text, |
| 43 const GURL& dictionary_url, | 47 const GURL& dictionary_url, |
| 44 const BoundNetLog& net_log)> | 48 const BoundNetLog& net_log)> |
| 45 OnDictionaryFetchedCallback; | 49 OnDictionaryFetchedCallback; |
| 46 | 50 |
| 47 // The consumer must guarantee that |*context| outlives this object. | 51 // The consumer must guarantee that |*context| outlives this object. |
| 48 // |callback| will be called on successful dictionary fetch | 52 explicit SdchDictionaryFetcher(URLRequestContext* context); |
| 49 // requested through Schedule(). |callback| will not be called | |
| 50 // after object destruction. | |
| 51 SdchDictionaryFetcher(URLRequestContext* context, | |
| 52 const OnDictionaryFetchedCallback& callback); | |
| 53 ~SdchDictionaryFetcher() override; | 53 ~SdchDictionaryFetcher() override; |
| 54 | 54 |
| 55 // Request a new dictionary fetch. | 55 // Request a new dictionary fetch. The callback will be called |
| 56 void Schedule(const GURL& dictionary_url); | 56 // only if the dictionary is successfully fetched. Returns true if a |
| 57 // request for |dictionary_url| has been scheduled, and false otherwise. | |
| 58 virtual bool Schedule(const GURL& dictionary_url, | |
| 59 const OnDictionaryFetchedCallback& callback); | |
| 60 | |
| 61 // Request a dictionary fetch from cache only. The callback will be called | |
| 62 // only if the dictionary is successfully fetched. Returns true if a request | |
| 63 // for |dictionary_url| has been scheduled, and false otherwise. | |
| 64 virtual bool ScheduleReload(const GURL& dictionary_url, | |
| 65 const OnDictionaryFetchedCallback& callback); | |
| 57 | 66 |
| 58 // Cancel any in-progress requests. | 67 // Cancel any in-progress requests. |
| 59 void Cancel(); | 68 virtual void Cancel(); |
| 60 | 69 |
| 61 // Implementation of URLRequest::Delegate methods. | 70 // Implementation of URLRequest::Delegate methods. |
| 62 void OnResponseStarted(URLRequest* request) override; | 71 void OnResponseStarted(URLRequest* request) override; |
| 63 void OnReadCompleted(URLRequest* request, int bytes_read) override; | 72 void OnReadCompleted(URLRequest* request, int bytes_read) override; |
| 64 | 73 |
| 65 private: | 74 private: |
| 66 enum State { | 75 enum State { |
| 67 STATE_NONE, | 76 STATE_NONE, |
| 68 STATE_SEND_REQUEST, | 77 STATE_SEND_REQUEST, |
| 69 STATE_SEND_REQUEST_COMPLETE, | 78 STATE_SEND_REQUEST_COMPLETE, |
| 70 STATE_READ_BODY, | 79 STATE_READ_BODY, |
| 71 STATE_READ_BODY_COMPLETE, | 80 STATE_READ_BODY_COMPLETE, |
| 72 STATE_REQUEST_COMPLETE, | 81 STATE_REQUEST_COMPLETE, |
| 73 }; | 82 }; |
| 74 | 83 |
| 84 // Schedule implementation. Returns true if a request for |dictionary_url| has | |
| 85 // been added to the queue, and false otherwise. | |
| 86 bool ScheduleInternal(const GURL& dictionary_url, | |
| 87 bool reload, | |
| 88 const OnDictionaryFetchedCallback& callback); | |
| 89 | |
| 90 // Null out the current request and push the state machine to the | |
| 91 // next request, if any. | |
| 92 void ResetRequest(); | |
| 93 | |
| 75 // State machine implementation. | 94 // State machine implementation. |
| 76 int DoLoop(int rv); | 95 int DoLoop(int rv); |
| 77 int DoSendRequest(int rv); | 96 int DoSendRequest(int rv); |
| 78 int DoSendRequestComplete(int rv); | 97 int DoSendRequestComplete(int rv); |
| 79 int DoReadBody(int rv); | 98 int DoReadBody(int rv); |
| 80 int DoReadBodyComplete(int rv); | 99 int DoReadBodyComplete(int rv); |
| 81 int DoCompleteRequest(int rv); | 100 int DoCompleteRequest(int rv); |
| 82 | 101 |
| 83 State next_state_; | 102 State next_state_; |
| 84 bool in_loop_; | 103 bool in_loop_; |
| 85 | 104 |
| 86 // A queue of URLs that are being used to download dictionaries. | 105 // A queue of URLs that are being used to download dictionaries. |
| 87 std::queue<GURL> fetch_queue_; | 106 scoped_ptr<UniqueFetchQueue> fetch_queue_; |
|
Randy Smith (Not in Mondays)
2015/02/23 21:23:49
nit, suggestion: If you declare UniqueFetchQueue i
Elly Fong-Jones
2015/03/03 21:37:48
I used the scoped_ptr to avoid inlining it. I've m
| |
| 88 | 107 |
| 89 // The request and buffer used for getting the current dictionary | 108 // The request, buffer, and consumer supplied data used for getting |
| 90 // Both are null when a fetch is not in progress. | 109 // the current dictionary. All are null when a fetch is not in progress. |
| 91 scoped_ptr<URLRequest> current_request_; | 110 scoped_ptr<URLRequest> current_request_; |
| 92 scoped_refptr<IOBuffer> buffer_; | 111 scoped_refptr<IOBuffer> buffer_; |
| 112 OnDictionaryFetchedCallback current_callback_; | |
| 93 | 113 |
| 94 // The currently accumulating dictionary. | 114 // The currently accumulating dictionary. |
| 95 std::string dictionary_; | 115 std::string dictionary_; |
| 96 | 116 |
| 97 // Althought the SDCH spec does not preclude a server from using a single URL | |
| 98 // to load several distinct dictionaries (by telling a client to load a | |
| 99 // dictionary from an URL several times), current implementations seem to have | |
| 100 // that 1-1 relationship (i.e., each URL points at a single dictionary, and | |
| 101 // the dictionary content does not change over time, and hence is not worth | |
| 102 // trying to load more than once). In addition, some dictionaries prove | |
| 103 // unloadable only after downloading them (because they are too large? ...or | |
| 104 // malformed?). As a protective element, Chromium will *only* load a | |
| 105 // dictionary at most once from a given URL (so that it doesn't waste | |
| 106 // bandwidth trying repeatedly). | |
| 107 // The following set lists all the dictionary URLs that we've tried to load, | |
| 108 // so that we won't try to load from an URL more than once. | |
| 109 // TODO(jar): Try to augment the SDCH proposal to include this restiction. | |
| 110 std::set<GURL> attempted_load_; | |
| 111 | |
| 112 // Store the URLRequestContext associated with the owning SdchManager for | 117 // Store the URLRequestContext associated with the owning SdchManager for |
| 113 // use while fetching. | 118 // use while fetching. |
| 114 URLRequestContext* const context_; | 119 URLRequestContext* const context_; |
| 115 | 120 |
| 116 const OnDictionaryFetchedCallback dictionary_fetched_callback_; | |
| 117 | |
| 118 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; | 121 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; |
| 119 | 122 |
| 120 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); | 123 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); |
| 121 }; | 124 }; |
| 122 | 125 |
| 123 } // namespace net | 126 } // namespace net |
| 124 | 127 |
| 125 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ | 128 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ |
| OLD | NEW |