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. |
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 |*consumer| and |*context| outlive |
33 // this object. The current implementation guarantees this by | 41 // this object. |
34 // the SdchManager owning this object. | 42 SdchDictionaryFetcher(SdchFetcher::Delegate* consumer, |
35 SdchDictionaryFetcher(SdchManager* manager, | 43 URLRequestContext* context); |
36 scoped_refptr<URLRequestContextGetter> context); | |
37 virtual ~SdchDictionaryFetcher(); | 44 virtual ~SdchDictionaryFetcher(); |
38 | 45 |
39 // Implementation of SdchFetcher class. | 46 // Implementation of SdchFetcher methods. |
40 virtual void Schedule(const GURL& dictionary_url) OVERRIDE; | 47 virtual void Schedule(const GURL& dictionary_url) OVERRIDE; |
41 virtual void Cancel() OVERRIDE; | 48 virtual void Cancel() OVERRIDE; |
42 | 49 |
| 50 // Implementation of URLRequest::Delegate methods. |
| 51 virtual void OnResponseStarted(URLRequest* request) OVERRIDE; |
| 52 virtual void OnReadCompleted(URLRequest* request, int bytes_read) OVERRIDE; |
| 53 |
43 private: | 54 private: |
44 // Delay in ms between Schedule and actual download. | 55 enum State { |
45 // This leaves the URL in a queue, which is de-duped, so that there is less | 56 STATE_NONE, |
46 // chance we'll try to load the same URL multiple times when a pile of | 57 STATE_IDLE, |
47 // page subresources (or tabs opened in parallel) all suggest the dictionary. | 58 STATE_REQUEST_STARTED, |
48 static const int kMsDelayFromRequestTillDownload = 100; | 59 STATE_REQUEST_READING, |
| 60 STATE_REQUEST_COMPLETE, |
| 61 }; |
49 | 62 |
50 // Ensure the download after the above delay. | 63 // State machine implementation. |
51 void ScheduleDelayedRun(); | 64 int DoLoop(int rv); |
| 65 int DoDispatchRequest(int rv); |
| 66 int DoRequestStarted(int rv); |
| 67 int DoRead(int rv); |
| 68 int DoCompleteRequest(int rv); |
52 | 69 |
53 // Make sure we're processing (or waiting for) the the arrival of the next URL | 70 State next_state_; |
54 // in the |fetch_queue_|. | 71 bool in_loop_; |
55 void StartFetching(); | |
56 | 72 |
57 // Implementation of URLFetcherDelegate. Called after transmission | 73 SdchFetcher::Delegate* const consumer_; |
58 // completes (either successfully or with failure). | |
59 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | |
60 | |
61 SdchManager* const manager_; | |
62 | 74 |
63 // A queue of URLs that are being used to download dictionaries. | 75 // A queue of URLs that are being used to download dictionaries. |
64 std::queue<GURL> fetch_queue_; | 76 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 | 77 |
69 bool task_is_pending_; | 78 // The request and buffer used for getting the current dictionary |
| 79 // Both are null when a fetch is not in progress. |
| 80 scoped_ptr<URLRequest> current_request_; |
| 81 scoped_refptr<IOBuffer> buffer_; |
| 82 |
| 83 // The currently accumulating dictionary. |
| 84 std::string dictionary_; |
70 | 85 |
71 // Althought the SDCH spec does not preclude a server from using a single URL | 86 // 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 | 87 // to load several distinct dictionaries (by telling a client to load a |
73 // dictionary from an URL several times), current implementations seem to have | 88 // 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 | 89 // 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 | 90 // 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 | 91 // trying to load more than once). In addition, some dictionaries prove |
77 // unloadable only after downloading them (because they are too large? ...or | 92 // unloadable only after downloading them (because they are too large? ...or |
78 // malformed?). As a protective element, Chromium will *only* load a | 93 // 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 | 94 // dictionary at most once from a given URL (so that it doesn't waste |
80 // bandwidth trying repeatedly). | 95 // bandwidth trying repeatedly). |
81 // The following set lists all the dictionary URLs that we've tried to load, | 96 // 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. | 97 // 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. | 98 // TODO(jar): Try to augment the SDCH proposal to include this restiction. |
84 std::set<GURL> attempted_load_; | 99 std::set<GURL> attempted_load_; |
85 | 100 |
86 // Store the system_url_request_context_getter to use it when we start | 101 // Store the URLRequestContext associated with the owning SdchManager for |
87 // fetching. | 102 // use while fetching. |
88 scoped_refptr<URLRequestContextGetter> context_; | 103 URLRequestContext* context_; |
89 | 104 |
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_; | 105 base::WeakPtrFactory<SdchDictionaryFetcher> weak_factory_; |
94 | 106 |
95 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); | 107 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcher); |
96 }; | 108 }; |
97 | 109 |
98 } // namespace net | 110 } // namespace net |
99 | 111 |
100 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_ | 112 #endif // NET_BASE_SDCH_DICTIONARY_FETCHER_H_ |
OLD | NEW |