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 27 matching lines...) Expand all Loading... | |
38 // attempting to fetch each dictionary once. | 38 // attempting to fetch each dictionary once. |
39 class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, | 39 class NET_EXPORT SdchDictionaryFetcher : public URLRequest::Delegate, |
40 public base::NonThreadSafe { | 40 public base::NonThreadSafe { |
41 public: | 41 public: |
42 typedef base::Callback<void(const std::string& dictionary_text, | 42 typedef base::Callback<void(const std::string& dictionary_text, |
43 const GURL& dictionary_url, | 43 const GURL& dictionary_url, |
44 const BoundNetLog& net_log)> | 44 const BoundNetLog& net_log)> |
45 OnDictionaryFetchedCallback; | 45 OnDictionaryFetchedCallback; |
46 | 46 |
47 // The consumer must guarantee that |*context| outlives this object. | 47 // The consumer must guarantee that |*context| outlives this object. |
48 // |callback| will be called on successful dictionary fetch | 48 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; | 49 ~SdchDictionaryFetcher() override; |
54 | 50 |
55 // Request a new dictionary fetch. | 51 // Request a new dictionary fetch. The callback will be called |
56 void Schedule(const GURL& dictionary_url); | 52 // only if the dictionary is successfully fetched. |
53 void Schedule(const GURL& dictionary_url, | |
54 const OnDictionaryFetchedCallback& callback); | |
55 | |
56 // Request a dictionary fetch from cache only. The callback will be called | |
57 // only if the dictionary is successfully fetched. | |
58 void ScheduleReload(const GURL& dictionary_url, | |
59 const OnDictionaryFetchedCallback& callback); | |
57 | 60 |
58 // Cancel any in-progress requests. | 61 // Cancel any in-progress requests. |
59 void Cancel(); | 62 void Cancel(); |
60 | 63 |
61 // Implementation of URLRequest::Delegate methods. | 64 // Implementation of URLRequest::Delegate methods. |
62 void OnResponseStarted(URLRequest* request) override; | 65 void OnResponseStarted(URLRequest* request) override; |
63 void OnReadCompleted(URLRequest* request, int bytes_read) override; | 66 void OnReadCompleted(URLRequest* request, int bytes_read) override; |
64 | 67 |
65 private: | 68 private: |
66 enum State { | 69 enum State { |
67 STATE_NONE, | 70 STATE_NONE, |
68 STATE_SEND_REQUEST, | 71 STATE_SEND_REQUEST, |
69 STATE_SEND_REQUEST_COMPLETE, | 72 STATE_SEND_REQUEST_COMPLETE, |
70 STATE_READ_BODY, | 73 STATE_READ_BODY, |
71 STATE_READ_BODY_COMPLETE, | 74 STATE_READ_BODY_COMPLETE, |
72 STATE_REQUEST_COMPLETE, | 75 STATE_REQUEST_COMPLETE, |
73 }; | 76 }; |
74 | 77 |
78 struct QueuedInfo { | |
79 const GURL url; | |
80 bool download_only_from_cache; | |
81 OnDictionaryFetchedCallback callback; | |
82 | |
83 QueuedInfo(); | |
84 QueuedInfo(const GURL& url, | |
85 bool download_only_from_cache, | |
86 const OnDictionaryFetchedCallback& callback); | |
87 QueuedInfo(const QueuedInfo& rhs) = default; | |
Bernhard Bauer
2015/02/06 16:40:27
Hm... I think technically this defines the constru
Elly Fong-Jones
2015/02/06 18:44:09
Done. We can only have an operator= if the GURL me
Bernhard Bauer
2015/02/06 20:12:18
Makes sense, if we allow assignment.
(What does t
| |
88 QueuedInfo& operator=(const QueuedInfo& rhs) = default; | |
89 | |
90 ~QueuedInfo(); | |
91 }; | |
92 | |
93 // Schedule implementation. | |
94 void ScheduleInternal(const GURL& dictionary_url, | |
95 bool reload, | |
96 const OnDictionaryFetchedCallback& callback); | |
97 | |
98 // Null out the current request and push the state machine to the | |
99 // next request, if any. | |
100 void ResetRequest(); | |
101 | |
75 // State machine implementation. | 102 // State machine implementation. |
76 int DoLoop(int rv); | 103 int DoLoop(int rv); |
77 int DoSendRequest(int rv); | 104 int DoSendRequest(int rv); |
78 int DoSendRequestComplete(int rv); | 105 int DoSendRequestComplete(int rv); |
79 int DoReadBody(int rv); | 106 int DoReadBody(int rv); |
80 int DoReadBodyComplete(int rv); | 107 int DoReadBodyComplete(int rv); |
81 int DoCompleteRequest(int rv); | 108 int DoCompleteRequest(int rv); |
82 | 109 |
83 State next_state_; | 110 State next_state_; |
84 bool in_loop_; | 111 bool in_loop_; |
85 | 112 |
86 // A queue of URLs that are being used to download dictionaries. | 113 // A queue of URLs that are being used to download dictionaries. |
87 std::queue<GURL> fetch_queue_; | 114 // |fetch_queue_[n].second| indicated whether the download should occur |
115 // only from cache. | |
116 std::queue<QueuedInfo> fetch_queue_; | |
88 | 117 |
89 // The request and buffer used for getting the current dictionary | 118 // The request, buffer, and consumer supplied data used for getting |
90 // Both are null when a fetch is not in progress. | 119 // the current dictionary. All are null when a fetch is not in progress. |
91 scoped_ptr<URLRequest> current_request_; | 120 scoped_ptr<URLRequest> current_request_; |
92 scoped_refptr<IOBuffer> buffer_; | 121 scoped_refptr<IOBuffer> buffer_; |
122 OnDictionaryFetchedCallback current_callback_; | |
93 | 123 |
94 // The currently accumulating dictionary. | 124 // The currently accumulating dictionary. |
95 std::string dictionary_; | 125 std::string dictionary_; |
96 | 126 |
97 // Althought the SDCH spec does not preclude a server from using a single URL | 127 // 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 | 128 // to load several distinct dictionaries (by telling a client to load a |
99 // dictionary from an URL several times), current implementations seem to have | 129 // 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 | 130 // 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 | 131 // 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 | 132 // trying to load more than once). In addition, some dictionaries prove |
103 // unloadable only after downloading them (because they are too large? ...or | 133 // unloadable only after downloading them (because they are too large? ...or |
104 // malformed?). As a protective element, Chromium will *only* load a | 134 // 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 | 135 // dictionary at most once from a given URL (so that it doesn't waste |
106 // bandwidth trying repeatedly). | 136 // bandwidth trying repeatedly). |
107 // The following set lists all the dictionary URLs that we've tried to load, | 137 // 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. | 138 // 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. | 139 // TODO(jar): Try to augment the SDCH proposal to include this restiction. |
110 std::set<GURL> attempted_load_; | 140 std::set<GURL> attempted_load_; |
111 | 141 |
112 // Store the URLRequestContext associated with the owning SdchManager for | 142 // Store the URLRequestContext associated with the owning SdchManager for |
113 // use while fetching. | 143 // use while fetching. |
114 URLRequestContext* const context_; | 144 URLRequestContext* const context_; |
115 | 145 |
116 const OnDictionaryFetchedCallback dictionary_fetched_callback_; | |
117 | |
118 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; | 146 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; |
119 | 147 |
120 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); | 148 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); |
121 }; | 149 }; |
122 | 150 |
123 } // namespace net | 151 } // namespace net |
124 | 152 |
125 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ | 153 #endif // NET_URL_REQUEST_SDCH_DICTIONARY_FETCHER_H_ |
OLD | NEW |