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