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