summaryrefslogtreecommitdiffstats
path: root/net/url_request/sdch_dictionary_fetcher.cc
blob: 25ae1f21a83a2d3c0444b50470f594a87e1387e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/url_request/sdch_dictionary_fetcher.h"

#include <stdint.h>
#include <queue>
#include <set>

#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/thread_task_runner_handle.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/sdch_net_log_params.h"
#include "net/http/http_response_headers.h"
#include "net/log/net_log.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_throttler_manager.h"

namespace net {

namespace {

const int kBufferSize = 4096;

// Map the bytes_read result from a read attempt and a URLRequest's
// status into a single net return value.
int GetReadResult(int bytes_read, const URLRequest* request) {
  int rv = request->status().error();
  if (request->status().is_success() && bytes_read < 0) {
    rv = ERR_FAILED;
    request->net_log().AddEventWithNetErrorCode(
        NetLog::TYPE_SDCH_DICTIONARY_FETCH_IMPLIED_ERROR, rv);
  }

  if (rv == OK)
    rv = bytes_read;

  return rv;
}

struct FetchInfo {
  FetchInfo(const GURL& url,
            bool cache_only,
            const SdchDictionaryFetcher::OnDictionaryFetchedCallback& callback)
      : url(url), cache_only(cache_only), callback(callback) {}
  FetchInfo() {}

  GURL url;
  bool cache_only;
  SdchDictionaryFetcher::OnDictionaryFetchedCallback callback;
};

}  // namespace

// A UniqueFetchQueue is used to queue outgoing requests, which are either cache
// requests or network requests (which *may* still be served from cache).
// The UniqueFetchQueue enforces that a URL can only be queued for network fetch
// at most once. Calling Clear() resets UniqueFetchQueue's memory of which URLs
// have been queued.
class SdchDictionaryFetcher::UniqueFetchQueue {
 public:
  UniqueFetchQueue();
  ~UniqueFetchQueue();

  bool Push(const FetchInfo& info);
  bool Pop(FetchInfo* info);
  bool IsEmpty() const;
  void Clear();

 private:
  std::queue<FetchInfo> queue_;
  std::set<GURL> ever_network_queued_;

  DISALLOW_COPY_AND_ASSIGN(UniqueFetchQueue);
};

SdchDictionaryFetcher::UniqueFetchQueue::UniqueFetchQueue() {}
SdchDictionaryFetcher::UniqueFetchQueue::~UniqueFetchQueue() {}

bool SdchDictionaryFetcher::UniqueFetchQueue::Push(const FetchInfo& info) {
  if (ever_network_queued_.count(info.url) != 0)
    return false;
  if (!info.cache_only)
    ever_network_queued_.insert(info.url);
  queue_.push(info);
  return true;
}

bool SdchDictionaryFetcher::UniqueFetchQueue::Pop(FetchInfo* info) {
  if (IsEmpty())
    return false;
  *info = queue_.front();
  queue_.pop();
  return true;
}

bool SdchDictionaryFetcher::UniqueFetchQueue::IsEmpty() const {
  return queue_.empty();
}

void SdchDictionaryFetcher::UniqueFetchQueue::Clear() {
  ever_network_queued_.clear();
  while (!queue_.empty())
    queue_.pop();
}

SdchDictionaryFetcher::SdchDictionaryFetcher(URLRequestContext* context)
    : next_state_(STATE_NONE),
      in_loop_(false),
      fetch_queue_(new UniqueFetchQueue()),
      context_(context) {
  DCHECK(CalledOnValidThread());
  DCHECK(context);
}

SdchDictionaryFetcher::~SdchDictionaryFetcher() {
}

bool SdchDictionaryFetcher::Schedule(
    const GURL& dictionary_url,
    const OnDictionaryFetchedCallback& callback) {
  return ScheduleInternal(dictionary_url, false, callback);
}

bool SdchDictionaryFetcher::ScheduleReload(
    const GURL& dictionary_url,
    const OnDictionaryFetchedCallback& callback) {
  return ScheduleInternal(dictionary_url, true, callback);
}

void SdchDictionaryFetcher::Cancel() {
  DCHECK(CalledOnValidThread());

  ResetRequest();
  next_state_ = STATE_NONE;

  fetch_queue_->Clear();
}

void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(request, current_request_.get());
  DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE);
  DCHECK(!in_loop_);

  // Confirm that the response isn't a stale read from the cache (as
  // may happen in the reload case).  If the response was not retrieved over
  // HTTP, it is presumed to be fresh.
  HttpResponseHeaders* response_headers = request->response_headers();
  int result = request->status().error();
  if (result == OK && response_headers) {
    ValidationType validation_type = response_headers->RequiresValidation(
        request->response_info().request_time,
        request->response_info().response_time, base::Time::Now());
    // TODO(rdsmith): Maybe handle VALIDATION_ASYNCHRONOUS by queueing
    // a non-reload request for the dictionary.
    if (validation_type != VALIDATION_NONE)
      result = ERR_FAILED;
  }

  DoLoop(result);
}

void SdchDictionaryFetcher::OnReadCompleted(URLRequest* request,
                                            int bytes_read) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(request, current_request_.get());
  DCHECK_EQ(next_state_, STATE_READ_BODY_COMPLETE);
  DCHECK(!in_loop_);

  DoLoop(GetReadResult(bytes_read, current_request_.get()));
}

bool SdchDictionaryFetcher::ScheduleInternal(
    const GURL& dictionary_url,
    bool reload,
    const OnDictionaryFetchedCallback& callback) {
  DCHECK(CalledOnValidThread());

  // If Push() fails, |dictionary_url| has already been fetched or scheduled to
  // be fetched.
  if (!fetch_queue_->Push(FetchInfo(dictionary_url, reload, callback))) {
    // TODO(rdsmith): Log this error to the net log.  In the case of a
    // normal fetch, this can be through the URLRequest
    // initiating this fetch (once the URLRequest is passed to the fetcher);
    // in the case of a reload, it's more complicated.
    SdchManager::SdchErrorRecovery(
        SDCH_DICTIONARY_PREVIOUSLY_SCHEDULED_TO_DOWNLOAD);
    return false;
  }

  // If the loop is already processing, it'll pick up the above in the
  // normal course of events.
  if (next_state_ != STATE_NONE)
    return true;

  next_state_ = STATE_SEND_REQUEST;

  // There are no callbacks to user code from the dictionary fetcher,
  // and Schedule() is only called from user code, so this call to DoLoop()
  // does not require an |if (in_loop_) return;| guard.
  DoLoop(OK);
  return true;
}

void SdchDictionaryFetcher::ResetRequest() {
  current_request_.reset();
  buffer_ = nullptr;
  current_callback_.Reset();
  dictionary_.clear();
  return;
}

int SdchDictionaryFetcher::DoLoop(int rv) {
  DCHECK(!in_loop_);
  base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true);

  do {
    State state = next_state_;
    next_state_ = STATE_NONE;
    switch (state) {
      case STATE_SEND_REQUEST:
        rv = DoSendRequest(rv);
        break;
      case STATE_SEND_REQUEST_COMPLETE:
        rv = DoSendRequestComplete(rv);
        break;
      case STATE_READ_BODY:
        rv = DoReadBody(rv);
        break;
      case STATE_READ_BODY_COMPLETE:
        rv = DoReadBodyComplete(rv);
        break;
      case STATE_REQUEST_COMPLETE:
        rv = DoCompleteRequest(rv);
        break;
      case STATE_NONE:
        NOTREACHED();
    }
  } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);

  return rv;
}

int SdchDictionaryFetcher::DoSendRequest(int rv) {
  DCHECK(CalledOnValidThread());

  // |rv| is ignored, as the result from the previous request doesn't
  // affect the next request.

  if (fetch_queue_->IsEmpty() || current_request_.get()) {
    next_state_ = STATE_NONE;
    return OK;
  }

  next_state_ = STATE_SEND_REQUEST_COMPLETE;

  FetchInfo info;
  bool success = fetch_queue_->Pop(&info);
  DCHECK(success);
  current_request_ = context_->CreateRequest(info.url, IDLE, this);
  int load_flags = LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES;
  if (info.cache_only)
    load_flags |= LOAD_ONLY_FROM_CACHE;
  current_request_->SetLoadFlags(load_flags);

  buffer_ = new IOBuffer(kBufferSize);
  current_callback_ = info.callback;

  current_request_->Start();
  current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH);

  return ERR_IO_PENDING;
}

int SdchDictionaryFetcher::DoSendRequestComplete(int rv) {
  DCHECK(CalledOnValidThread());

  // If there's been an error, abort the current request.
  if (rv != OK) {
    current_request_.reset();
    buffer_ = NULL;
    next_state_ = STATE_SEND_REQUEST;

    return OK;
  }

  next_state_ = STATE_READ_BODY;
  return OK;
}

int SdchDictionaryFetcher::DoReadBody(int rv) {
  DCHECK(CalledOnValidThread());

  // If there's been an error, abort the current request.
  if (rv != OK) {
    ResetRequest();
    next_state_ = STATE_SEND_REQUEST;
    return OK;
  }

  next_state_ = STATE_READ_BODY_COMPLETE;
  int bytes_read = 0;
  current_request_->Read(buffer_.get(), kBufferSize, &bytes_read);
  if (current_request_->status().is_io_pending())
    return ERR_IO_PENDING;

  return GetReadResult(bytes_read, current_request_.get());
}

int SdchDictionaryFetcher::DoReadBodyComplete(int rv) {
  DCHECK(CalledOnValidThread());

  // An error; abort the current request.
  if (rv < 0) {
    current_request_.reset();
    buffer_ = NULL;
    next_state_ = STATE_SEND_REQUEST;
    return OK;
  }

  DCHECK(current_request_->status().is_success());

  // Data; append to the dictionary and look for more data.
  if (rv > 0) {
    dictionary_.append(buffer_->data(), rv);
    next_state_ = STATE_READ_BODY;
    return OK;
  }

  // End of file; complete the request.
  next_state_ = STATE_REQUEST_COMPLETE;
  return OK;
}

int SdchDictionaryFetcher::DoCompleteRequest(int rv) {
  DCHECK(CalledOnValidThread());

  // If the dictionary was successfully fetched, add it to the manager.
  if (rv == OK) {
    current_callback_.Run(dictionary_, current_request_->url(),
                          current_request_->net_log(),
                          current_request_->was_cached());
  }

  ResetRequest();
  next_state_ = STATE_SEND_REQUEST;
  return OK;
}

}  // namespace net