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 // Support modularity by calling to load a new SDCH filter dictionary. | 5 // TODO(rdsmith): This class needs to be moved out to the net/ embedder and |
6 // Note that this sort of calling can't be done in the /net directory, as it has | 6 // hooked into whatever mechanisms the embedder uses for authentication. |
7 // no concept of the HTTP cache (which is only visible at the browser level). | 7 // Specifically, this class needs methods overriding |
8 // URLRequest::Delegate::{OnAuthRequired,OnCertificateRequested} and can't | |
9 // implement them at the net/ layer. | |
8 | 10 |
9 #ifndef NET_BASE_SDCH_DICTIONARY_FETCHER_H_ | 11 #ifndef NET_BASE_SDCH_DICTIONARY_FETCHER_H_ |
10 #define NET_BASE_SDCH_DICTIONARY_FETCHER_H_ | 12 #define NET_BASE_SDCH_DICTIONARY_FETCHER_H_ |
11 | 13 |
12 #include <queue> | 14 #include <queue> |
13 #include <set> | 15 #include <set> |
14 #include <string> | 16 #include <string> |
15 | 17 |
16 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
18 #include "base/threading/non_thread_safe.h" | 20 #include "base/threading/non_thread_safe.h" |
19 #include "net/base/sdch_manager.h" | 21 #include "net/base/sdch_manager.h" |
20 #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" | |
21 | 24 |
22 namespace net { | 25 namespace net { |
23 | 26 |
24 class URLFetcher; | 27 class URLRequest; |
25 class URLRequestContextGetter; | 28 class URLRequestThrottlerEntryInterface; |
26 | 29 |
30 // This class implements the SdchFetcher interface. It queues requests | |
31 // for dictionaries and dispatches them serially, implementing | |
32 // the URLRequest::Delegate interface to handle callbacks (but see above | |
33 // TODO). It tracks all requests, only attempting to fetch each dictionary | |
Ryan Sleevi
2014/09/04 01:14:21
anal-retentive nit: once space, not two, between s
Randy Smith (Not in Mondays)
2014/09/04 18:52:57
Apologies for getting a-r back at you, but could y
| |
34 // once. | |
27 class NET_EXPORT SdchDictionaryFetcher | 35 class NET_EXPORT SdchDictionaryFetcher |
28 : public URLFetcherDelegate, | 36 : public SdchFetcher, |
29 public SdchFetcher, | 37 public URLRequest::Delegate, |
30 public base::NonThreadSafe { | 38 public base::NonThreadSafe { |
31 public: | 39 public: |
32 // The consumer must guarantee that |*manager| outlives | 40 // The consumer must guarantee that |*manager| and |*context| outlive |
33 // this object. The current implementation guarantees this by | 41 // this object. |
34 // the SdchManager owning this object. | 42 SdchDictionaryFetcher(SdchManager* manager, URLRequestContext* context); |
35 SdchDictionaryFetcher(SdchManager* manager, | |
36 scoped_refptr<URLRequestContextGetter> context); | |
37 virtual ~SdchDictionaryFetcher(); | 43 virtual ~SdchDictionaryFetcher(); |
38 | 44 |
39 // Implementation of SdchFetcher class. | 45 // Implementation of SdchFetcher class. |
40 virtual void Schedule(const GURL& dictionary_url) OVERRIDE; | 46 virtual void Schedule(const GURL& dictionary_url) OVERRIDE; |
41 virtual void Cancel() OVERRIDE; | 47 virtual void Cancel() OVERRIDE; |
42 | 48 |
43 private: | 49 private: |
44 // Delay in ms between Schedule and actual download. | 50 enum State { |
45 // This leaves the URL in a queue, which is de-duped, so that there is less | 51 STATE_NONE, |
46 // chance we'll try to load the same URL multiple times when a pile of | 52 STATE_IDLE, |
47 // page subresources (or tabs opened in parallel) all suggest the dictionary. | 53 STATE_REQUEST_STARTED, |
48 static const int kMsDelayFromRequestTillDownload = 100; | 54 STATE_REQUEST_READING, |
55 STATE_REQUEST_COMPLETE, | |
56 }; | |
49 | 57 |
50 // Ensure the download after the above delay. | 58 // State machine implementation. |
51 void ScheduleDelayedRun(); | 59 int DoLoop(int rv); |
60 int DoDispatchRequest(int rv); | |
61 int DoRequestStarted(int rv); | |
62 int DoRead(int rv); | |
63 int DoCompleteRequest(int rv); | |
52 | 64 |
53 // Make sure we're processing (or waiting for) the the arrival of the next URL | 65 // URLRequest::Delegate overrides |
54 // in the |fetch_queue_|. | 66 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; |
55 void StartFetching(); | 67 virtual void OnReadCompleted(URLRequest* request, int bytes_read) OVERRIDE; |
Ryan Sleevi
2014/09/04 01:14:21
I argue with pkasting a lot about this, whether or
Randy Smith (Not in Mondays)
2014/09/04 18:52:57
Wow. Ok, will do.
(I personally prefer the style
Randy Smith (Not in Mondays)
2014/09/04 18:54:13
Sorry, I realized this was ambiguous. I've made t
| |
56 | 68 |
57 // Implementation of URLFetcherDelegate. Called after transmission | 69 State next_state_; |
58 // completes (either successfully or with failure). | 70 bool in_loop_; |
59 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | |
60 | 71 |
61 SdchManager* const manager_; | 72 SdchManager* const manager_; |
62 | 73 |
63 // A queue of URLs that are being used to download dictionaries. | 74 // A queue of URLs that are being used to download dictionaries. |
64 std::queue<GURL> fetch_queue_; | 75 std::queue<GURL> fetch_queue_; |
65 // The currently outstanding URL fetch of a dicitonary. | |
66 // If this is null, then there is no outstanding request. | |
67 scoped_ptr<URLFetcher> current_fetch_; | |
68 | 76 |
69 // Always spread out the dictionary fetches, so that they don't steal | 77 // The request and buffer used for getting the current dictionary |
70 // bandwidth from the actual page load. Create delayed tasks to spread out | 78 // Both are null when a fetch is not in progress. |
71 // the download. | 79 scoped_ptr<URLRequest> current_request_; |
72 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; | 80 scoped_refptr<IOBuffer> buffer_; |
73 bool task_is_pending_; | 81 |
82 // The currently accumulating dictionary. | |
83 std::string dictionary_; | |
74 | 84 |
75 // Althought the SDCH spec does not preclude a server from using a single URL | 85 // Althought the SDCH spec does not preclude a server from using a single URL |
76 // to load several distinct dictionaries (by telling a client to load a | 86 // to load several distinct dictionaries (by telling a client to load a |
77 // dictionary from an URL several times), current implementations seem to have | 87 // dictionary from an URL several times), current implementations seem to have |
78 // that 1-1 relationship (i.e., each URL points at a single dictionary, and | 88 // that 1-1 relationship (i.e., each URL points at a single dictionary, and |
79 // the dictionary content does not change over time, and hence is not worth | 89 // the dictionary content does not change over time, and hence is not worth |
80 // trying to load more than once). In addition, some dictionaries prove | 90 // trying to load more than once). In addition, some dictionaries prove |
81 // unloadable only after downloading them (because they are too large? ...or | 91 // unloadable only after downloading them (because they are too large? ...or |
82 // malformed?). As a protective element, Chromium will *only* load a | 92 // malformed?). As a protective element, Chromium will *only* load a |
83 // dictionary at most once from a given URL (so that it doesn't waste | 93 // dictionary at most once from a given URL (so that it doesn't waste |
84 // bandwidth trying repeatedly). | 94 // bandwidth trying repeatedly). |
85 // The following set lists all the dictionary URLs that we've tried to load, | 95 // The following set lists all the dictionary URLs that we've tried to load, |
86 // so that we won't try to load from an URL more than once. | 96 // so that we won't try to load from an URL more than once. |
87 // TODO(jar): Try to augment the SDCH proposal to include this restiction. | 97 // TODO(jar): Try to augment the SDCH proposal to include this restiction. |
88 std::set<GURL> attempted_load_; | 98 std::set<GURL> attempted_load_; |
89 | 99 |
90 // Store the system_url_request_context_getter to use it when we start | 100 // Store the URLRequestContext associated with the owning SdchManager for |
91 // fetching. | 101 // use while fetching. |
92 scoped_refptr<URLRequestContextGetter> context_; | 102 URLRequestContext* context_; |
103 | |
104 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; | |
93 | 105 |
94 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); | 106 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); |
95 }; | 107 }; |
96 | 108 |
97 } // namespace net | 109 } // namespace net |
98 | 110 |
99 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_ | 111 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_ |
OLD | NEW |