OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "webkit/glue/resource_request_body.h" | 5 #include "content/browser/loader/upload_data_stream_builder.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "net/base/upload_bytes_element_reader.h" | 8 #include "net/base/upload_bytes_element_reader.h" |
9 #include "net/base/upload_data_stream.h" | 9 #include "net/base/upload_data_stream.h" |
10 #include "net/base/upload_file_element_reader.h" | 10 #include "net/base/upload_file_element_reader.h" |
11 #include "webkit/browser/blob/blob_storage_controller.h" | 11 #include "webkit/browser/blob/blob_storage_controller.h" |
12 #include "webkit/browser/fileapi/upload_file_system_file_element_reader.h" | 12 #include "webkit/browser/fileapi/upload_file_system_file_element_reader.h" |
| 13 #include "webkit/common/resource_request_body.h" |
13 | 14 |
14 using webkit_blob::BlobData; | 15 using webkit_blob::BlobData; |
15 using webkit_blob::BlobStorageController; | 16 using webkit_blob::BlobStorageController; |
| 17 using webkit_glue::ResourceRequestBody; |
16 | 18 |
17 namespace webkit_glue { | 19 namespace content { |
18 | |
19 namespace { | 20 namespace { |
20 | 21 |
21 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. | 22 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. |
22 class BytesElementReader : public net::UploadBytesElementReader { | 23 class BytesElementReader : public net::UploadBytesElementReader { |
23 public: | 24 public: |
24 BytesElementReader(ResourceRequestBody* resource_request_body, | 25 BytesElementReader(ResourceRequestBody* resource_request_body, |
25 const ResourceRequestBody::Element& element) | 26 const ResourceRequestBody::Element& element) |
26 : net::UploadBytesElementReader(element.bytes(), element.length()), | 27 : net::UploadBytesElementReader(element.bytes(), element.length()), |
27 resource_request_body_(resource_request_body) { | 28 resource_request_body_(resource_request_body) { |
28 DCHECK_EQ(ResourceRequestBody::Element::TYPE_BYTES, element.type()); | 29 DCHECK_EQ(ResourceRequestBody::Element::TYPE_BYTES, element.type()); |
(...skipping 25 matching lines...) Expand all Loading... |
54 } | 55 } |
55 | 56 |
56 virtual ~FileElementReader() {} | 57 virtual ~FileElementReader() {} |
57 | 58 |
58 private: | 59 private: |
59 scoped_refptr<ResourceRequestBody> resource_request_body_; | 60 scoped_refptr<ResourceRequestBody> resource_request_body_; |
60 | 61 |
61 DISALLOW_COPY_AND_ASSIGN(FileElementReader); | 62 DISALLOW_COPY_AND_ASSIGN(FileElementReader); |
62 }; | 63 }; |
63 | 64 |
64 } // namespace | 65 void ResolveBlobReference( |
| 66 ResourceRequestBody* body, |
| 67 webkit_blob::BlobStorageController* blob_controller, |
| 68 const GURL& blob_url, |
| 69 std::vector<const ResourceRequestBody::Element*>* resolved_elements) { |
| 70 DCHECK(blob_controller); |
| 71 BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url); |
| 72 DCHECK(blob_data); |
| 73 if (!blob_data) |
| 74 return; |
65 | 75 |
66 ResourceRequestBody::ResourceRequestBody() : identifier_(0) {} | 76 // If there is no element in the referred blob data, just return. |
| 77 if (blob_data->items().empty()) |
| 78 return; |
67 | 79 |
68 void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) { | 80 // Ensure the blob and any attached shareable files survive until |
69 if (bytes_len > 0) { | 81 // upload completion. |
70 elements_.push_back(Element()); | 82 body->SetUserData(blob_data, new base::UserDataAdapter<BlobData>(blob_data)); |
71 elements_.back().SetToBytes(bytes, bytes_len); | 83 |
| 84 // Append the elements in the referred blob data. |
| 85 for (size_t i = 0; i < blob_data->items().size(); ++i) { |
| 86 const BlobData::Item& item = blob_data->items().at(i); |
| 87 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type()); |
| 88 resolved_elements->push_back(&item); |
72 } | 89 } |
73 } | 90 } |
74 | 91 |
75 void ResourceRequestBody::AppendFileRange( | 92 } // namespace |
76 const base::FilePath& file_path, | |
77 uint64 offset, uint64 length, | |
78 const base::Time& expected_modification_time) { | |
79 elements_.push_back(Element()); | |
80 elements_.back().SetToFilePathRange(file_path, offset, length, | |
81 expected_modification_time); | |
82 } | |
83 | 93 |
84 void ResourceRequestBody::AppendBlob(const GURL& blob_url) { | 94 scoped_ptr<net::UploadDataStream> UploadDataStreamBuilder::Build( |
85 elements_.push_back(Element()); | 95 ResourceRequestBody* body, |
86 elements_.back().SetToBlobUrl(blob_url); | |
87 } | |
88 | |
89 void ResourceRequestBody::AppendFileSystemFileRange( | |
90 const GURL& url, uint64 offset, uint64 length, | |
91 const base::Time& expected_modification_time) { | |
92 elements_.push_back(Element()); | |
93 elements_.back().SetToFileSystemUrlRange(url, offset, length, | |
94 expected_modification_time); | |
95 } | |
96 | |
97 net::UploadDataStream* | |
98 ResourceRequestBody::ResolveElementsAndCreateUploadDataStream( | |
99 BlobStorageController* blob_controller, | 96 BlobStorageController* blob_controller, |
100 fileapi::FileSystemContext* file_system_context, | 97 fileapi::FileSystemContext* file_system_context, |
101 base::TaskRunner* file_task_runner) { | 98 base::TaskRunner* file_task_runner) { |
102 // Resolve all blob elements. | 99 // Resolve all blob elements. |
103 std::vector<const Element*> resolved_elements; | 100 std::vector<const ResourceRequestBody::Element*> resolved_elements; |
104 for (size_t i = 0; i < elements_.size(); ++i) { | 101 for (size_t i = 0; i < body->elements()->size(); ++i) { |
105 const Element& element = elements_[i]; | 102 const ResourceRequestBody::Element& element = (*body->elements())[i]; |
106 if (element.type() == Element::TYPE_BLOB) { | 103 if (element.type() == ResourceRequestBody::Element::TYPE_BLOB) { |
107 ResolveBlobReference(blob_controller, element.url(), &resolved_elements); | 104 ResolveBlobReference(body, blob_controller, element.url(), |
| 105 &resolved_elements); |
108 } else { | 106 } else { |
109 // No need to resolve, just append the element. | 107 // No need to resolve, just append the element. |
110 resolved_elements.push_back(&element); | 108 resolved_elements.push_back(&element); |
111 } | 109 } |
112 } | 110 } |
113 | 111 |
114 ScopedVector<net::UploadElementReader> element_readers; | 112 ScopedVector<net::UploadElementReader> element_readers; |
115 for (size_t i = 0; i < resolved_elements.size(); ++i) { | 113 for (size_t i = 0; i < resolved_elements.size(); ++i) { |
116 const Element& element = *resolved_elements[i]; | 114 const ResourceRequestBody::Element& element = *resolved_elements[i]; |
117 switch (element.type()) { | 115 switch (element.type()) { |
118 case Element::TYPE_BYTES: | 116 case ResourceRequestBody::Element::TYPE_BYTES: |
119 element_readers.push_back(new BytesElementReader(this, element)); | 117 element_readers.push_back(new BytesElementReader(body, element)); |
120 break; | 118 break; |
121 case Element::TYPE_FILE: | 119 case ResourceRequestBody::Element::TYPE_FILE: |
122 element_readers.push_back( | 120 element_readers.push_back( |
123 new FileElementReader(this, file_task_runner, element)); | 121 new FileElementReader(body, file_task_runner, element)); |
124 break; | 122 break; |
125 case Element::TYPE_FILE_FILESYSTEM: | 123 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: |
126 element_readers.push_back( | 124 element_readers.push_back( |
127 new fileapi::UploadFileSystemFileElementReader( | 125 new fileapi::UploadFileSystemFileElementReader( |
128 file_system_context, | 126 file_system_context, |
129 element.url(), | 127 element.url(), |
130 element.offset(), | 128 element.offset(), |
131 element.length(), | 129 element.length(), |
132 element.expected_modification_time())); | 130 element.expected_modification_time())); |
133 break; | 131 break; |
134 case Element::TYPE_BLOB: | 132 case ResourceRequestBody::Element::TYPE_BLOB: |
135 // Blob elements should be resolved beforehand. | 133 // Blob elements should be resolved beforehand. |
136 NOTREACHED(); | 134 NOTREACHED(); |
137 break; | 135 break; |
138 case Element::TYPE_UNKNOWN: | 136 case ResourceRequestBody::Element::TYPE_UNKNOWN: |
139 NOTREACHED(); | 137 NOTREACHED(); |
140 break; | 138 break; |
141 } | 139 } |
142 } | 140 } |
143 return new net::UploadDataStream(&element_readers, identifier_); | 141 |
| 142 return make_scoped_ptr( |
| 143 new net::UploadDataStream(&element_readers, body->identifier())); |
144 } | 144 } |
145 | 145 |
146 ResourceRequestBody::~ResourceRequestBody() {} | 146 } // namespace content |
147 | |
148 void ResourceRequestBody::ResolveBlobReference( | |
149 webkit_blob::BlobStorageController* blob_controller, | |
150 const GURL& blob_url, | |
151 std::vector<const Element*>* resolved_elements) { | |
152 DCHECK(blob_controller); | |
153 BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url); | |
154 DCHECK(blob_data); | |
155 if (!blob_data) | |
156 return; | |
157 | |
158 // If there is no element in the referred blob data, just return. | |
159 if (blob_data->items().empty()) | |
160 return; | |
161 | |
162 // Ensure the blob and any attached shareable files survive until | |
163 // upload completion. | |
164 SetUserData(blob_data, new base::UserDataAdapter<BlobData>(blob_data)); | |
165 | |
166 // Append the elements in the referred blob data. | |
167 for (size_t i = 0; i < blob_data->items().size(); ++i) { | |
168 const BlobData::Item& item = blob_data->items().at(i); | |
169 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type()); | |
170 resolved_elements->push_back(&item); | |
171 } | |
172 } | |
173 | |
174 } // namespace webkit_glue | |
OLD | NEW |