Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / net / url_request / url_request_simple_job.cc
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.
4
5 #include "net/url_request/url_request_simple_job.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_request_headers.h"
16 #include "net/http/http_util.h"
17 #include "net/url_request/url_request_status.h"
18
19 namespace net {
20
21 URLRequestSimpleJob::URLRequestSimpleJob(
22     URLRequest* request, NetworkDelegate* network_delegate)
23     : URLRangeRequestJob(request, network_delegate),
24       data_offset_(0),
25       weak_factory_(this) {}
26
27 void URLRequestSimpleJob::Start() {
28   // Start reading asynchronously so that all error reporting and data
29   // callbacks happen as they would for network requests.
30   base::MessageLoop::current()->PostTask(
31       FROM_HERE,
32       base::Bind(&URLRequestSimpleJob::StartAsync, weak_factory_.GetWeakPtr()));
33 }
34
35 bool URLRequestSimpleJob::GetMimeType(std::string* mime_type) const {
36   *mime_type = mime_type_;
37   return true;
38 }
39
40 bool URLRequestSimpleJob::GetCharset(std::string* charset) {
41   *charset = charset_;
42   return true;
43 }
44
45 URLRequestSimpleJob::~URLRequestSimpleJob() {}
46
47 bool URLRequestSimpleJob::ReadRawData(IOBuffer* buf, int buf_size,
48                                       int* bytes_read) {
49   DCHECK(bytes_read);
50   int remaining = byte_range_.last_byte_position() - data_offset_ + 1;
51   if (buf_size > remaining)
52     buf_size = remaining;
53   memcpy(buf->data(), data_.data() + data_offset_, buf_size);
54   data_offset_ += buf_size;
55   *bytes_read = buf_size;
56   return true;
57 }
58
59 void URLRequestSimpleJob::StartAsync() {
60   if (!request_)
61     return;
62
63   if (ranges().size() > 1) {
64     // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
65     tracked_objects::ScopedTracker tracking_profile(
66         FROM_HERE_WITH_EXPLICIT_FUNCTION(
67             "422489 URLRequestSimpleJob::StartAsync 1"));
68
69     NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
70                                 ERR_REQUEST_RANGE_NOT_SATISFIABLE));
71     return;
72   }
73
74   if (!ranges().empty() && range_parse_result() == OK)
75     byte_range_ = ranges().front();
76
77   int result;
78   {
79     // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
80     // Remove the block and assign 'result' in its declaration.
81     tracked_objects::ScopedTracker tracking_profile(
82         FROM_HERE_WITH_EXPLICIT_FUNCTION(
83             "422489 URLRequestSimpleJob::StartAsync 2"));
84
85     result = GetData(&mime_type_,
86                      &charset_,
87                      &data_,
88                      base::Bind(&URLRequestSimpleJob::OnGetDataCompleted,
89                                 weak_factory_.GetWeakPtr()));
90   }
91
92   if (result != ERR_IO_PENDING) {
93     // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
94     tracked_objects::ScopedTracker tracking_profile(
95         FROM_HERE_WITH_EXPLICIT_FUNCTION(
96             "422489 URLRequestSimpleJob::StartAsync 3"));
97
98     OnGetDataCompleted(result);
99   }
100 }
101
102 void URLRequestSimpleJob::OnGetDataCompleted(int result) {
103   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
104   tracked_objects::ScopedTracker tracking_profile(
105       FROM_HERE_WITH_EXPLICIT_FUNCTION(
106           "422489 URLRequestSimpleJob::OnGetDataCompleted"));
107
108   if (result == OK) {
109     // Notify that the headers are complete
110     if (!byte_range_.ComputeBounds(data_.size())) {
111       NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
112                  ERR_REQUEST_RANGE_NOT_SATISFIABLE));
113       return;
114     }
115
116     data_offset_ = byte_range_.first_byte_position();
117     int remaining_bytes = byte_range_.last_byte_position() -
118         byte_range_.first_byte_position() + 1;
119     set_expected_content_size(remaining_bytes);
120     NotifyHeadersComplete();
121   } else {
122     NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
123   }
124 }
125
126 }  // namespace net