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 #include "net/base/sdch_dictionary_fetcher.h" | 5 #include "net/base/sdch_dictionary_fetcher.h" |
6 | 6 |
7 #include <stdint.h> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
9 #include "base/message_loop/message_loop.h" | 11 #include "base/thread_task_runner_handle.h" |
10 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
11 #include "net/url_request/url_fetcher.h" | |
12 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
13 #include "net/url_request/url_request_status.h" | 14 #include "net/url_request/url_request_status.h" |
15 #include "net/url_request/url_request_throttler_manager.h" | |
16 | |
17 namespace { | |
18 | |
19 const int kBufferSize = 4096; | |
20 | |
21 } // namespace | |
14 | 22 |
15 namespace net { | 23 namespace net { |
16 | 24 |
17 SdchDictionaryFetcher::SdchDictionaryFetcher( | 25 SdchDictionaryFetcher::SdchDictionaryFetcher( |
18 SdchManager* manager, | 26 SdchManager* manager, |
19 scoped_refptr<URLRequestContextGetter> context) | 27 scoped_refptr<URLRequestContextGetter> context) |
20 : manager_(manager), | 28 : manager_(manager), |
21 weak_factory_(this), | 29 context_(context), |
22 task_is_pending_(false), | 30 weak_factory_(this) { |
23 context_(context) { | |
24 DCHECK(CalledOnValidThread()); | 31 DCHECK(CalledOnValidThread()); |
25 DCHECK(manager); | 32 DCHECK(manager); |
26 } | 33 } |
27 | 34 |
28 SdchDictionaryFetcher::~SdchDictionaryFetcher() { | 35 SdchDictionaryFetcher::~SdchDictionaryFetcher() { |
29 DCHECK(CalledOnValidThread()); | 36 DCHECK(CalledOnValidThread()); |
30 } | 37 } |
31 | 38 |
32 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { | 39 void SdchDictionaryFetcher::Schedule(const GURL& dictionary_url) { |
33 DCHECK(CalledOnValidThread()); | 40 DCHECK(CalledOnValidThread()); |
34 | 41 |
35 // Avoid pushing duplicate copy onto queue. We may fetch this url again later | 42 // Avoid pushing duplicate copy onto queue. We may fetch this url again later |
36 // and get a different dictionary, but there is no reason to have it in the | 43 // and get a different dictionary, but there is no reason to have it in the |
37 // queue twice at one time. | 44 // queue twice at one time. |
38 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { | 45 if (!fetch_queue_.empty() && fetch_queue_.back() == dictionary_url) { |
39 SdchManager::SdchErrorRecovery( | 46 SdchManager::SdchErrorRecovery( |
40 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); | 47 SdchManager::DICTIONARY_ALREADY_SCHEDULED_TO_DOWNLOAD); |
41 return; | 48 return; |
42 } | 49 } |
43 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { | 50 if (attempted_load_.find(dictionary_url) != attempted_load_.end()) { |
44 SdchManager::SdchErrorRecovery( | 51 SdchManager::SdchErrorRecovery( |
45 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); | 52 SdchManager::DICTIONARY_ALREADY_TRIED_TO_DOWNLOAD); |
46 return; | 53 return; |
47 } | 54 } |
48 attempted_load_.insert(dictionary_url); | 55 attempted_load_.insert(dictionary_url); |
49 fetch_queue_.push(dictionary_url); | 56 fetch_queue_.push(dictionary_url); |
50 ScheduleDelayedRun(); | 57 DispatchRun(); |
51 } | 58 } |
52 | 59 |
53 void SdchDictionaryFetcher::Cancel() { | 60 void SdchDictionaryFetcher::Cancel() { |
54 DCHECK(CalledOnValidThread()); | 61 DCHECK(CalledOnValidThread()); |
55 | 62 |
56 while (!fetch_queue_.empty()) | 63 while (!fetch_queue_.empty()) |
57 fetch_queue_.pop(); | 64 fetch_queue_.pop(); |
58 attempted_load_.clear(); | 65 attempted_load_.clear(); |
59 weak_factory_.InvalidateWeakPtrs(); | 66 weak_factory_.InvalidateWeakPtrs(); |
60 current_fetch_.reset(NULL); | 67 current_request_.reset(NULL); |
68 buffer_ = NULL; | |
69 dictionary_ = ""; | |
Ryan Sleevi
2014/08/25 20:00:00
s/""/std::string()
or just dictionary_.clear()
Randy Smith (Not in Mondays)
2014/08/26 15:34:17
Done.
| |
61 } | 70 } |
62 | 71 |
63 void SdchDictionaryFetcher::ScheduleDelayedRun() { | 72 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { |
64 if (fetch_queue_.empty() || current_fetch_.get() || task_is_pending_) | 73 DCHECK(CalledOnValidThread()); |
65 return; | 74 DCHECK_EQ(request, current_request_.get()); |
66 base::MessageLoop::current()->PostDelayedTask(FROM_HERE, | 75 |
67 base::Bind(&SdchDictionaryFetcher::StartFetching, | 76 int bytes_read = 0; |
68 weak_factory_.GetWeakPtr()), | 77 request->Read(buffer_.get(), kBufferSize, &bytes_read); |
Ryan Sleevi
2014/08/25 20:00:00
Read() returns a bool. Please check it before assu
Randy Smith (Not in Mondays)
2014/08/26 15:34:17
Done, but I'd like a bit more of a consult here.
| |
69 base::TimeDelta::FromMilliseconds(kMsDelayFromRequestTillDownload)); | 78 OnReadCompleted(request, bytes_read); |
70 task_is_pending_ = true; | |
71 } | 79 } |
72 | 80 |
73 void SdchDictionaryFetcher::StartFetching() { | 81 void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request, |
82 int bytes_read) { | |
74 DCHECK(CalledOnValidThread()); | 83 DCHECK(CalledOnValidThread()); |
75 DCHECK(task_is_pending_); | 84 DCHECK_EQ(request, current_request_.get()); |
76 task_is_pending_ = false; | |
77 | 85 |
78 // Handle losing the race against Cancel(). | 86 URLRequestThrottlerManager* throttler_manager = |
79 if (fetch_queue_.empty()) | 87 request->context()->throttler_manager(); |
88 if (throttler_manager) { | |
89 url_throttler_entry_ = | |
90 throttler_manager->RegisterRequestUrl(request->url()); | |
91 } | |
92 | |
93 do { | |
94 if (!current_request_->status().is_success() || bytes_read <= 0) | |
95 break; | |
96 | |
97 dictionary_ += std::string(buffer_->data(), bytes_read); | |
Ryan Sleevi
2014/08/25 20:00:00
dictionary_.append(), and avoid an unnecessary all
Randy Smith (Not in Mondays)
2014/08/26 15:34:17
Done.
| |
98 } while (current_request_->Read(buffer_.get(), kBufferSize, &bytes_read)); | |
99 | |
100 const URLRequestStatus status = current_request_->status(); | |
101 | |
102 if (!status.is_io_pending()) { | |
103 if (status.is_success()) | |
104 manager_->AddSdchDictionary(dictionary_, current_request_->url()); | |
105 | |
106 current_request_.reset(); | |
107 buffer_ = NULL; | |
108 dictionary_ = ""; | |
Ryan Sleevi
2014/08/25 20:00:00
.clear()
Randy Smith (Not in Mondays)
2014/08/26 15:34:17
Done.
| |
109 | |
110 DispatchRun(); | |
111 } | |
112 } | |
113 | |
114 void SdchDictionaryFetcher::DispatchRun() { | |
115 DCHECK(CalledOnValidThread()); | |
116 DCHECK(context_.get()); | |
117 | |
118 if (fetch_queue_.empty() || current_request_.get()) | |
80 return; | 119 return; |
81 | 120 |
82 DCHECK(context_.get()); | 121 int64 delay = INT64_C(0); |
83 current_fetch_.reset(URLFetcher::Create( | 122 if (!original_url_throttler_entry_.get()) { |
84 fetch_queue_.front(), URLFetcher::GET, this)); | 123 URLRequestThrottlerManager* manager = |
85 fetch_queue_.pop(); | 124 context_->GetURLRequestContext()->throttler_manager(); |
86 current_fetch_->SetRequestContext(context_.get()); | 125 if (manager) { |
87 current_fetch_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | 126 original_url_throttler_entry_ = |
88 LOAD_DO_NOT_SAVE_COOKIES); | 127 manager->RegisterRequestUrl(fetch_queue_.front()); |
89 current_fetch_->Start(); | 128 } |
129 } | |
130 if (original_url_throttler_entry_.get()) { | |
131 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest( | |
132 GetBackoffReleaseTime()); | |
133 } | |
134 | |
135 if (delay != INT64_C(0)) { | |
136 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
137 FROM_HERE, base::Bind(&SdchDictionaryFetcher::StartURLRequest, | |
138 weak_factory_.GetWeakPtr()), | |
139 base::TimeDelta::FromMilliseconds(delay)); | |
140 return; | |
141 } | |
142 | |
143 StartURLRequest(); | |
Ryan Sleevi
2014/08/25 20:00:01
DANGER: This potentially leads to large recursion.
Randy Smith (Not in Mondays)
2014/09/02 19:35:03
See top level comments; we need to hash a bit here
| |
90 } | 144 } |
91 | 145 |
92 void SdchDictionaryFetcher::OnURLFetchComplete( | 146 void SdchDictionaryFetcher::StartURLRequest() { |
93 const URLFetcher* source) { | |
94 DCHECK(CalledOnValidThread()); | 147 DCHECK(CalledOnValidThread()); |
95 if ((200 == source->GetResponseCode()) && | 148 DCHECK(context_.get()); |
96 (source->GetStatus().status() == URLRequestStatus::SUCCESS)) { | 149 DCHECK(!current_request_.get()); |
97 std::string data; | 150 |
98 source->GetResponseAsString(&data); | 151 current_request_ = context_->GetURLRequestContext()->CreateRequest( |
99 manager_->AddSdchDictionary(data, source->GetURL()); | 152 fetch_queue_.front(), IDLE, this, NULL); |
Ryan Sleevi
2014/08/25 20:00:01
Can this fail?
Randy Smith (Not in Mondays)
2014/08/26 15:34:17
The header is silent on the topic. As I read the
| |
153 current_request_->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES | | |
154 LOAD_DO_NOT_SAVE_COOKIES); | |
155 buffer_ = new IOBuffer(kBufferSize); | |
156 fetch_queue_.pop(); | |
157 | |
158 current_request_->Start(); | |
159 } | |
160 | |
161 base::TimeTicks SdchDictionaryFetcher::GetBackoffReleaseTime() { | |
162 DCHECK(CalledOnValidThread()); | |
163 | |
164 if (!original_url_throttler_entry_.get()) | |
165 return base::TimeTicks(); | |
166 | |
167 base::TimeTicks original_url_backoff = | |
168 original_url_throttler_entry_->GetExponentialBackoffReleaseTime(); | |
169 base::TimeTicks destination_url_backoff; | |
170 if (url_throttler_entry_.get() && | |
171 original_url_throttler_entry_.get() != url_throttler_entry_.get()) { | |
172 destination_url_backoff = | |
173 url_throttler_entry_->GetExponentialBackoffReleaseTime(); | |
100 } | 174 } |
101 current_fetch_.reset(NULL); | 175 |
102 ScheduleDelayedRun(); | 176 return original_url_backoff > destination_url_backoff ? |
177 original_url_backoff : destination_url_backoff; | |
103 } | 178 } |
104 | 179 |
105 } // namespace net | 180 } // namespace net |
OLD | NEW |