Enables compositing support for webview.
[chromium-blink-merge.git] / net / url_request / url_request_simple_job.cc
blobfda10127edd4908d1a69f3b6c017b29040017b08
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/url_request/url_request_simple_job.h"
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/message_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/url_request/url_request_status.h"
14 namespace net {
16 URLRequestSimpleJob::URLRequestSimpleJob(
17 URLRequest* request, NetworkDelegate* network_delegate)
18 : URLRequestJob(request, network_delegate),
19 data_offset_(0),
20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
22 void URLRequestSimpleJob::Start() {
23 // Start reading asynchronously so that all error reporting and data
24 // callbacks happen as they would for network requests.
25 MessageLoop::current()->PostTask(
26 FROM_HERE,
27 base::Bind(&URLRequestSimpleJob::StartAsync,
28 weak_factory_.GetWeakPtr()));
31 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
32 *mime_type = mime_type_;
33 return true;
36 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
37 *charset = charset_;
38 return true;
41 URLRequestSimpleJob::~URLRequestSimpleJob() {}
43 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
44 int* bytes_read) {
45 DCHECK(bytes_read);
46 int remaining = static_cast<int>(data_.size()) - data_offset_;
47 if (buf_size > remaining)
48 buf_size = remaining;
49 memcpy(buf->data(), data_.data() + data_offset_, buf_size);
50 data_offset_ += buf_size;
51 *bytes_read = buf_size;
52 return true;
55 void URLRequestSimpleJob::StartAsync() {
56 if (!request_)
57 return;
59 int result = GetData(&mime_type_, &charset_, &data_,
60 base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
61 weak_factory_.GetWeakPtr()));
62 if (result != ERR_IO_PENDING)
63 OnGetDataCompleted(result);
66 void URLRequestSimpleJob::OnGetDataCompleted(int result) {
67 if (result == OK) {
68 // Notify that the headers are complete
69 NotifyHeadersComplete();
70 } else {
71 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
75 } // namespace net