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 | |
34 // once. | |
Ryan Sleevi
2014/09/04 20:06:20
nit: single spaces
Randy Smith (Not in Mondays)
2014/09/07 14:09:39
Done.
| |
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 methods. |
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 |
49 // Implementation of URLRequest::Delegate methods. | |
50 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; | |
51 virtual void OnReadCompleted(URLRequest* request, int bytes_read) OVERRIDE; | |
52 | |
43 private: | 53 private: |
44 // Delay in ms between Schedule and actual download. | 54 enum State { |
45 // This leaves the URL in a queue, which is de-duped, so that there is less | 55 STATE_NONE, |
46 // chance we'll try to load the same URL multiple times when a pile of | 56 STATE_IDLE, |
47 // page subresources (or tabs opened in parallel) all suggest the dictionary. | 57 STATE_REQUEST_STARTED, |
48 static const int kMsDelayFromRequestTillDownload = 100; | 58 STATE_REQUEST_READING, |
59 STATE_REQUEST_COMPLETE, | |
60 }; | |
49 | 61 |
50 // Ensure the download after the above delay. | 62 // State machine implementation. |
51 void ScheduleDelayedRun(); | 63 int DoLoop(int rv); |
64 int DoDispatchRequest(int rv); | |
65 int DoRequestStarted(int rv); | |
66 int DoRead(int rv); | |
67 int DoCompleteRequest(int rv); | |
52 | 68 |
53 // Make sure we're processing (or waiting for) the the arrival of the next URL | 69 State next_state_; |
54 // in the |fetch_queue_|. | 70 bool in_loop_; |
55 void StartFetching(); | |
56 | |
57 // Implementation of URLFetcherDelegate. Called after transmission | |
58 // completes (either successfully or with failure). | |
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 bool task_is_pending_; | 77 // The request and buffer used for getting the current dictionary |
78 // Both are null when a fetch is not in progress. | |
79 scoped_ptr<URLRequest> current_request_; | |
80 scoped_refptr<IOBuffer> buffer_; | |
81 | |
82 // The currently accumulating dictionary. | |
83 std::string dictionary_; | |
70 | 84 |
71 // 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 |
72 // 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 |
73 // dictionary from an URL several times), current implementations seem to have | 87 // dictionary from an URL several times), current implementations seem to have |
74 // 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 |
75 // 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 |
76 // trying to load more than once). In addition, some dictionaries prove | 90 // trying to load more than once). In addition, some dictionaries prove |
Ryan Sleevi
2014/09/04 20:06:20
Weird double space here that doesn't occur on 92.
Randy Smith (Not in Mondays)
2014/09/07 14:09:39
Done :-} (double -> single; you got me with the an
| |
77 // unloadable only after downloading them (because they are too large? ...or | 91 // unloadable only after downloading them (because they are too large? ...or |
78 // malformed?). As a protective element, Chromium will *only* load a | 92 // malformed?). As a protective element, Chromium will *only* load a |
79 // 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 |
80 // bandwidth trying repeatedly). | 94 // bandwidth trying repeatedly). |
81 // 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, |
82 // 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. |
83 // 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. |
84 std::set<GURL> attempted_load_; | 98 std::set<GURL> attempted_load_; |
85 | 99 |
86 // Store the system_url_request_context_getter to use it when we start | 100 // Store the URLRequestContext associated with the owning SdchManager for |
87 // fetching. | 101 // use while fetching. |
88 scoped_refptr<URLRequestContextGetter> context_; | 102 URLRequestContext* context_; |
89 | 103 |
90 // Always spread out the dictionary fetches, so that they don't steal | |
91 // bandwidth from the actual page load. Create delayed tasks to spread out | |
92 // the download. | |
93 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; | 104 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; |
94 | 105 |
95 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); | 106 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); |
96 }; | 107 }; |
97 | 108 |
98 } // namespace net | 109 } // namespace net |
99 | 110 |
100 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_ | 111 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_ |
OLD | NEW |