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 |
(...skipping 17 matching lines...) Expand all Loading... |
28 class URLRequestThrottlerEntryInterface; | 28 class URLRequestThrottlerEntryInterface; |
29 | 29 |
30 // This class is used by embedder SDCH policy object to fetch | 30 // This class is used by embedder SDCH policy object to fetch |
31 // dictionaries. It queues requests for dictionaries and dispatches | 31 // dictionaries. It queues requests for dictionaries and dispatches |
32 // them serially, implementing the URLRequest::Delegate interface to | 32 // them serially, implementing the URLRequest::Delegate interface to |
33 // handle callbacks (but see above TODO). It tracks all requests, only | 33 // handle callbacks (but see above TODO). It tracks all requests, only |
34 // attempting to fetch each dictionary once. | 34 // attempting to fetch each dictionary once. |
35 class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, | 35 class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, |
36 public base::NonThreadSafe { | 36 public base::NonThreadSafe { |
37 public: | 37 public: |
| 38 class Data { |
| 39 public: |
| 40 virtual ~Data() {} |
| 41 }; |
| 42 |
38 typedef base::Callback<void(const std::string& dictionary_text, | 43 typedef base::Callback<void(const std::string& dictionary_text, |
39 const GURL& dictionary_url, | 44 const GURL& dictionary_url, |
| 45 scoped_ptr<Data> extra_data, |
40 const BoundNetLog& net_log)> | 46 const BoundNetLog& net_log)> |
41 OnDictionaryFetchedCallback; | 47 OnDictionaryFetchedCallback; |
42 | 48 |
43 // The consumer must guarantee that |*context| outlives this object. | 49 // The consumer must guarantee that |*context| outlives this object. |
44 // |callback| will be called on successful dictionary fetch | 50 // |callback| will be called on successful dictionary fetch |
45 // requested through Schedule(). |callback| will not be called | 51 // requested through Schedule(). |callback| will not be called |
46 // after object destruction. | 52 // after object destruction. |
47 SdchDictionaryFetcher(URLRequestContext* context, | 53 SdchDictionaryFetcher(URLRequestContext* context, |
48 const OnDictionaryFetchedCallback& callback); | 54 const OnDictionaryFetchedCallback& callback); |
49 ~SdchDictionaryFetcher() override; | 55 ~SdchDictionaryFetcher() override; |
50 | 56 |
51 // Request a new dictionary fetch. | 57 // Request a new dictionary fetch. |extra_data| is data that will be |
52 void Schedule(const GURL& dictionary_url); | 58 // returned in the OnDictionaryFetchedCallback if the dictionary fetch |
| 59 // succeeds; otherwise it will be destroyed. |
| 60 void Schedule(const GURL& dictionary_url, scoped_ptr<Data> extra_data); |
| 61 |
| 62 // Request a dictionary fetch from cache only. See comment above |
| 63 // for information on |extra_data|. |
| 64 void ScheduleReload(const GURL& dictionary_url, scoped_ptr<Data> extra_data); |
53 | 65 |
54 // Cancel any in-progress requests. | 66 // Cancel any in-progress requests. |
55 void Cancel(); | 67 void Cancel(); |
56 | 68 |
57 // Implementation of URLRequest::Delegate methods. | 69 // Implementation of URLRequest::Delegate methods. |
58 void OnResponseStarted(URLRequest* request) override; | 70 void OnResponseStarted(URLRequest* request) override; |
59 void OnReadCompleted(URLRequest* request, int bytes_read) override; | 71 void OnReadCompleted(URLRequest* request, int bytes_read) override; |
60 | 72 |
61 private: | 73 private: |
62 enum State { | 74 enum State { |
63 STATE_NONE, | 75 STATE_NONE, |
64 STATE_IDLE, | 76 STATE_IDLE, |
65 STATE_REQUEST_STARTED, | 77 STATE_REQUEST_STARTED, |
66 STATE_REQUEST_READING, | 78 STATE_REQUEST_READING, |
67 STATE_REQUEST_COMPLETE, | 79 STATE_REQUEST_COMPLETE, |
68 }; | 80 }; |
69 | 81 |
| 82 struct QueuedInfo { |
| 83 const GURL url; |
| 84 bool download_only_from_cache; |
| 85 |
| 86 // Owned by |fetch_queue_|; must be destroyed if queue entry is |
| 87 // dropped. Not a scoped_ptr<> as std::queue requires copyable |
| 88 // types. |
| 89 Data* extra_data; |
| 90 |
| 91 QueuedInfo() : download_only_from_cache(false), extra_data(nullptr) {} |
| 92 QueuedInfo(const GURL& url, |
| 93 bool download_only_from_cache, |
| 94 scoped_ptr<Data> extra_data); |
| 95 QueuedInfo(const QueuedInfo& rhs) = default; |
| 96 QueuedInfo& operator=(const QueuedInfo& rhs) = default; |
| 97 }; |
| 98 |
| 99 // Schedule implementation. |
| 100 void ScheduleInternal(const GURL& dictionary_url, |
| 101 bool reload, |
| 102 scoped_ptr<Data> extra_data); |
| 103 |
| 104 // Null out the current request and push the state machine to the |
| 105 // next request, if any. |
| 106 void ResetRequest(); |
| 107 |
70 // State machine implementation. | 108 // State machine implementation. |
71 int DoLoop(int rv); | 109 int DoLoop(int rv); |
72 int DoDispatchRequest(int rv); | 110 int DoDispatchRequest(int rv); |
73 int DoRequestStarted(int rv); | 111 int DoRequestStarted(int rv); |
74 int DoRead(int rv); | 112 int DoRead(int rv); |
75 int DoCompleteRequest(int rv); | 113 int DoCompleteRequest(int rv); |
76 | 114 |
77 State next_state_; | 115 State next_state_; |
78 bool in_loop_; | 116 bool in_loop_; |
79 | 117 |
80 // A queue of URLs that are being used to download dictionaries. | 118 // A queue of URLs that are being used to download dictionaries. |
81 std::queue<GURL> fetch_queue_; | 119 // |fetch_queue_[n].second| indicated whether the download should occur |
| 120 // only from cache. |
| 121 std::queue<QueuedInfo> fetch_queue_; |
82 | 122 |
83 // The request and buffer used for getting the current dictionary | 123 // The request, buffer, and consumer supplied data used for getting |
84 // Both are null when a fetch is not in progress. | 124 // the current dictionary. All are null when a fetch is not in progress. |
85 scoped_ptr<URLRequest> current_request_; | 125 scoped_ptr<URLRequest> current_request_; |
86 scoped_refptr<IOBuffer> buffer_; | 126 scoped_refptr<IOBuffer> buffer_; |
| 127 scoped_ptr<Data> current_extra_data_; |
87 | 128 |
88 // The currently accumulating dictionary. | 129 // The currently accumulating dictionary. |
89 std::string dictionary_; | 130 std::string dictionary_; |
90 | 131 |
91 // Althought the SDCH spec does not preclude a server from using a single URL | 132 // Althought the SDCH spec does not preclude a server from using a single URL |
92 // to load several distinct dictionaries (by telling a client to load a | 133 // to load several distinct dictionaries (by telling a client to load a |
93 // dictionary from an URL several times), current implementations seem to have | 134 // dictionary from an URL several times), current implementations seem to have |
94 // that 1-1 relationship (i.e., each URL points at a single dictionary, and | 135 // that 1-1 relationship (i.e., each URL points at a single dictionary, and |
95 // the dictionary content does not change over time, and hence is not worth | 136 // the dictionary content does not change over time, and hence is not worth |
96 // trying to load more than once). In addition, some dictionaries prove | 137 // trying to load more than once). In addition, some dictionaries prove |
(...skipping 13 matching lines...) Expand all Loading... |
110 const OnDictionaryFetchedCallback dictionary_fetched_callback_; | 151 const OnDictionaryFetchedCallback dictionary_fetched_callback_; |
111 | 152 |
112 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; | 153 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; |
113 | 154 |
114 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); | 155 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); |
115 }; | 156 }; |
116 | 157 |
117 } // namespace net | 158 } // namespace net |
118 | 159 |
119 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ | 160 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ |
OLD | NEW |