| 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 be moved out to the net/ embedder and | 5 // TODO(rdsmith): This class needs to delegate URLRequest::Delegate methods |
| 6 // hooked into whatever mechanisms the embedder uses for authentication. | 6 // to the net/ embedder for correct implementation of authentication. |
| 7 // Specifically, this class needs methods overriding | 7 // Specifically, this class needs the embedder to provide functionality |
| 8 // URLRequest::Delegate::{OnAuthRequired,OnCertificateRequested} and can't | 8 // corresponding to |
| 9 // implement them at the net/ layer. | 9 // URLRequest::Delegate::{OnAuthRequired,OnCertificateRequested}. |
| 10 | 10 |
| 11 #ifndef NET_BASE_SDCH_DICTIONARY_FETCHER_H_ | 11 #ifndef NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ |
| 12 #define NET_BASE_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> |
| 16 #include <string> | 16 #include <string> |
| 17 | 17 |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "base/threading/non_thread_safe.h" | 20 #include "base/threading/non_thread_safe.h" |
| 21 #include "net/base/sdch_manager.h" | 21 #include "net/base/sdch_manager.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
| 24 | 24 |
| 25 namespace net { | 25 namespace net { |
| 26 | 26 |
| 27 class URLRequest; | 27 class URLRequest; |
| 28 class URLRequestThrottlerEntryInterface; | 28 class URLRequestThrottlerEntryInterface; |
| 29 | 29 |
| 30 // This class implements the SdchFetcher interface. It queues requests | 30 // This class is used by embedder SDCH policy object to fetch |
| 31 // for dictionaries and dispatches them serially, implementing | 31 // dictionaries. It queues requests for dictionaries and dispatches |
| 32 // the URLRequest::Delegate interface to handle callbacks (but see above | 32 // them serially, implementing the URLRequest::Delegate interface to |
| 33 // TODO). It tracks all requests, only attempting to fetch each dictionary | 33 // handle callbacks (but see above TODO). It tracks all requests, only |
| 34 // once. | 34 // attempting to fetch each dictionary once. |
| 35 class NET_EXPORT SdchDictionaryFetcher | 35 class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, |
| 36 : public SdchFetcher, | 36 public base::NonThreadSafe { |
| 37 public URLRequest::Delegate, | |
| 38 public base::NonThreadSafe { | |
| 39 public: | 37 public: |
| 40 // The consumer must guarantee that |*consumer| and |*context| outlive | 38 typedef base::Callback<void(const std::string& dictionary_text, |
| 41 // this object. | 39 const GURL& dictionary_url)> |
| 42 SdchDictionaryFetcher(SdchFetcher::Delegate* consumer, | 40 OnDictionaryFetchedCallback; |
| 43 URLRequestContext* context); | 41 |
| 42 // The consumer must guarantee that |*context| outlives this object. |
| 43 // |callback| will be called on successful dictionary fetch |
| 44 // requested through Schedule(). |callback| will not be called |
| 45 // after object destruction. |
| 46 SdchDictionaryFetcher(URLRequestContext* context, |
| 47 const OnDictionaryFetchedCallback& callback); |
| 44 ~SdchDictionaryFetcher() override; | 48 ~SdchDictionaryFetcher() override; |
| 45 | 49 |
| 46 // Implementation of SdchFetcher methods. | 50 // Request a new dictionary fetch. |
| 47 void Schedule(const GURL& dictionary_url) override; | 51 void Schedule(const GURL& dictionary_url); |
| 48 void Cancel() override; | 52 |
| 53 // Cancel any in-progress requests. |
| 54 void Cancel(); |
| 49 | 55 |
| 50 // Implementation of URLRequest::Delegate methods. | 56 // Implementation of URLRequest::Delegate methods. |
| 51 void OnResponseStarted(URLRequest* request) override; | 57 void OnResponseStarted(URLRequest* request) override; |
| 52 void OnReadCompleted(URLRequest* request, int bytes_read) override; | 58 void OnReadCompleted(URLRequest* request, int bytes_read) override; |
| 53 | 59 |
| 54 private: | 60 private: |
| 55 enum State { | 61 enum State { |
| 56 STATE_NONE, | 62 STATE_NONE, |
| 57 STATE_IDLE, | 63 STATE_IDLE, |
| 58 STATE_REQUEST_STARTED, | 64 STATE_REQUEST_STARTED, |
| 59 STATE_REQUEST_READING, | 65 STATE_REQUEST_READING, |
| 60 STATE_REQUEST_COMPLETE, | 66 STATE_REQUEST_COMPLETE, |
| 61 }; | 67 }; |
| 62 | 68 |
| 63 // State machine implementation. | 69 // State machine implementation. |
| 64 int DoLoop(int rv); | 70 int DoLoop(int rv); |
| 65 int DoDispatchRequest(int rv); | 71 int DoDispatchRequest(int rv); |
| 66 int DoRequestStarted(int rv); | 72 int DoRequestStarted(int rv); |
| 67 int DoRead(int rv); | 73 int DoRead(int rv); |
| 68 int DoCompleteRequest(int rv); | 74 int DoCompleteRequest(int rv); |
| 69 | 75 |
| 70 State next_state_; | 76 State next_state_; |
| 71 bool in_loop_; | 77 bool in_loop_; |
| 72 | 78 |
| 73 SdchFetcher::Delegate* const consumer_; | |
| 74 | |
| 75 // A queue of URLs that are being used to download dictionaries. | 79 // A queue of URLs that are being used to download dictionaries. |
| 76 std::queue<GURL> fetch_queue_; | 80 std::queue<GURL> fetch_queue_; |
| 77 | 81 |
| 78 // The request and buffer used for getting the current dictionary | 82 // The request and buffer used for getting the current dictionary |
| 79 // Both are null when a fetch is not in progress. | 83 // Both are null when a fetch is not in progress. |
| 80 scoped_ptr<URLRequest> current_request_; | 84 scoped_ptr<URLRequest> current_request_; |
| 81 scoped_refptr<IOBuffer> buffer_; | 85 scoped_refptr<IOBuffer> buffer_; |
| 82 | 86 |
| 83 // The currently accumulating dictionary. | 87 // The currently accumulating dictionary. |
| 84 std::string dictionary_; | 88 std::string dictionary_; |
| 85 | 89 |
| 86 // Althought the SDCH spec does not preclude a server from using a single URL | 90 // Althought the SDCH spec does not preclude a server from using a single URL |
| 87 // to load several distinct dictionaries (by telling a client to load a | 91 // to load several distinct dictionaries (by telling a client to load a |
| 88 // dictionary from an URL several times), current implementations seem to have | 92 // dictionary from an URL several times), current implementations seem to have |
| 89 // that 1-1 relationship (i.e., each URL points at a single dictionary, and | 93 // that 1-1 relationship (i.e., each URL points at a single dictionary, and |
| 90 // the dictionary content does not change over time, and hence is not worth | 94 // the dictionary content does not change over time, and hence is not worth |
| 91 // trying to load more than once). In addition, some dictionaries prove | 95 // trying to load more than once). In addition, some dictionaries prove |
| 92 // unloadable only after downloading them (because they are too large? ...or | 96 // unloadable only after downloading them (because they are too large? ...or |
| 93 // malformed?). As a protective element, Chromium will *only* load a | 97 // malformed?). As a protective element, Chromium will *only* load a |
| 94 // dictionary at most once from a given URL (so that it doesn't waste | 98 // dictionary at most once from a given URL (so that it doesn't waste |
| 95 // bandwidth trying repeatedly). | 99 // bandwidth trying repeatedly). |
| 96 // The following set lists all the dictionary URLs that we've tried to load, | 100 // The following set lists all the dictionary URLs that we've tried to load, |
| 97 // so that we won't try to load from an URL more than once. | 101 // so that we won't try to load from an URL more than once. |
| 98 // TODO(jar): Try to augment the SDCH proposal to include this restiction. | 102 // TODO(jar): Try to augment the SDCH proposal to include this restiction. |
| 99 std::set<GURL> attempted_load_; | 103 std::set<GURL> attempted_load_; |
| 100 | 104 |
| 101 // Store the URLRequestContext associated with the owning SdchManager for | 105 // Store the URLRequestContext associated with the owning SdchManager for |
| 102 // use while fetching. | 106 // use while fetching. |
| 103 URLRequestContext* context_; | 107 URLRequestContext* const context_; |
| 108 |
| 109 const OnDictionaryFetchedCallback dictionary_fetched_callback_; |
| 104 | 110 |
| 105 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; | 111 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; |
| 106 | 112 |
| 107 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); | 113 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); |
| 108 }; | 114 }; |
| 109 | 115 |
| 110 } // namespace net | 116 } // namespace net |
| 111 | 117 |
| 112 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_ | 118 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ |
| OLD | NEW |