OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/glue/resource_request_body.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "net/base/upload_data.h" | 8 #include "net/base/upload_bytes_element_reader.h" |
9 #include "net/base/upload_data_stream.h" | |
10 #include "net/base/upload_file_element_reader.h" | |
9 #include "webkit/blob/blob_storage_controller.h" | 11 #include "webkit/blob/blob_storage_controller.h" |
10 | 12 |
11 using webkit_blob::BlobData; | 13 using webkit_blob::BlobData; |
12 using webkit_blob::BlobStorageController; | 14 using webkit_blob::BlobStorageController; |
13 | 15 |
14 namespace webkit_glue { | 16 namespace webkit_glue { |
15 | 17 |
18 namespace { | |
19 | |
20 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. | |
21 class BytesElementReader : public net::UploadBytesElementReader { | |
22 public: | |
23 BytesElementReader(ResourceRequestBody* resource_request_body, | |
24 const ResourceRequestBody::Element& element) | |
25 : net::UploadBytesElementReader(element.bytes(), element.length()), | |
26 resource_request_body_(resource_request_body) { | |
27 DCHECK_EQ(ResourceRequestBody::Element::TYPE_BYTES, element.type()); | |
28 } | |
29 | |
30 virtual ~BytesElementReader() {} | |
31 | |
32 private: | |
33 scoped_refptr<ResourceRequestBody> resource_request_body_; | |
34 | |
35 DISALLOW_COPY_AND_ASSIGN(BytesElementReader); | |
36 }; | |
37 | |
38 // A subclass of net::UploadFileElementReader which owns ResourceRequestBody. | |
39 class FileElementReader : public net::UploadFileElementReader { | |
mmenke
2012/12/12 19:26:41
This class isn't needed, is it? UploadFileElement
hashimoto
2012/12/13 03:58:47
This class is needed to keep BlobData attached to
kinuko
2012/12/13 06:05:47
Can we have a comment about that?
hashimoto
2012/12/13 06:17:51
Done.
| |
40 public: | |
41 FileElementReader(ResourceRequestBody* resource_request_body, | |
42 const ResourceRequestBody::Element& element) | |
43 : net::UploadFileElementReader(element.path(), | |
44 element.offset(), | |
45 element.length(), | |
46 element.expected_modification_time()), | |
47 resource_request_body_(resource_request_body) { | |
48 DCHECK_EQ(ResourceRequestBody::Element::TYPE_FILE, element.type()); | |
49 } | |
50 | |
51 virtual ~FileElementReader() {} | |
52 | |
53 private: | |
54 scoped_refptr<ResourceRequestBody> resource_request_body_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(FileElementReader); | |
57 }; | |
58 | |
59 } // namespace | |
60 | |
16 ResourceRequestBody::ResourceRequestBody() : identifier_(0) {} | 61 ResourceRequestBody::ResourceRequestBody() : identifier_(0) {} |
17 | 62 |
18 void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) { | 63 void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) { |
19 if (bytes_len > 0) { | 64 if (bytes_len > 0) { |
20 elements_.push_back(Element()); | 65 elements_.push_back(Element()); |
21 elements_.back().SetToBytes(bytes, bytes_len); | 66 elements_.back().SetToBytes(bytes, bytes_len); |
22 } | 67 } |
23 } | 68 } |
24 | 69 |
25 void ResourceRequestBody::AppendFileRange( | 70 void ResourceRequestBody::AppendFileRange( |
(...skipping 11 matching lines...) Expand all Loading... | |
37 } | 82 } |
38 | 83 |
39 void ResourceRequestBody::AppendFileSystemFileRange( | 84 void ResourceRequestBody::AppendFileSystemFileRange( |
40 const GURL& url, uint64 offset, uint64 length, | 85 const GURL& url, uint64 offset, uint64 length, |
41 const base::Time& expected_modification_time) { | 86 const base::Time& expected_modification_time) { |
42 elements_.push_back(Element()); | 87 elements_.push_back(Element()); |
43 elements_.back().SetToFileSystemUrlRange(url, offset, length, | 88 elements_.back().SetToFileSystemUrlRange(url, offset, length, |
44 expected_modification_time); | 89 expected_modification_time); |
45 } | 90 } |
46 | 91 |
47 net::UploadData* ResourceRequestBody::ResolveElementsAndCreateUploadData( | 92 net::UploadDataStream* |
93 ResourceRequestBody::ResolveElementsAndCreateUploadDataStream( | |
48 BlobStorageController* blob_controller) { | 94 BlobStorageController* blob_controller) { |
49 // Resolve all blob elements. | 95 // Resolve all blob elements. |
50 std::vector<const Element*> resolved_elements; | 96 std::vector<const Element*> resolved_elements; |
51 for (size_t i = 0; i < elements_.size(); ++i) { | 97 for (size_t i = 0; i < elements_.size(); ++i) { |
52 const Element& element = elements_[i]; | 98 const Element& element = elements_[i]; |
53 if (element.type() == Element::TYPE_BLOB) { | 99 if (element.type() == Element::TYPE_BLOB) { |
54 ResolveBlobReference(blob_controller, element.url(), &resolved_elements); | 100 ResolveBlobReference(blob_controller, element.url(), &resolved_elements); |
55 } else { | 101 } else { |
56 // No need to resolve, just append the element. | 102 // No need to resolve, just append the element. |
57 resolved_elements.push_back(&element); | 103 resolved_elements.push_back(&element); |
58 } | 104 } |
59 } | 105 } |
60 | 106 |
61 net::UploadData* upload_data = new net::UploadData; | 107 ScopedVector<net::UploadElementReader> element_readers; |
62 // We attach 'this' to UploadData so that we do not need to copy | |
63 // bytes for TYPE_BYTES. | |
64 upload_data->SetUserData( | |
65 this, new base::UserDataAdapter<ResourceRequestBody>(this)); | |
66 ScopedVector<net::UploadElement>* elements = | |
67 upload_data->elements_mutable(); | |
68 for (size_t i = 0; i < resolved_elements.size(); ++i) { | 108 for (size_t i = 0; i < resolved_elements.size(); ++i) { |
69 const Element& element = *resolved_elements[i]; | 109 const Element& element = *resolved_elements[i]; |
70 switch (element.type()) { | 110 switch (element.type()) { |
71 case Element::TYPE_BYTES: | 111 case Element::TYPE_BYTES: |
72 elements->push_back(new net::UploadElement()); | 112 element_readers.push_back(new BytesElementReader(this, element)); |
73 elements->back()->SetToSharedBytes(element.bytes(), element.length()); | |
74 break; | 113 break; |
75 case Element::TYPE_FILE: | 114 case Element::TYPE_FILE: |
76 elements->push_back(new net::UploadElement()); | 115 element_readers.push_back(new FileElementReader(this, element)); |
77 elements->back()->SetToFilePathRange( | |
78 element.path(), | |
79 element.offset(), | |
80 element.length(), | |
81 element.expected_modification_time()); | |
82 break; | 116 break; |
83 case Element::TYPE_FILE_FILESYSTEM: | 117 case Element::TYPE_FILE_FILESYSTEM: |
84 // TODO(kinuko): Resolve FileSystemURL before creating UploadData. | 118 // TODO(kinuko): Resolve FileSystemURL before creating UploadData. |
85 NOTREACHED(); | 119 NOTREACHED(); |
86 break; | 120 break; |
87 case Element::TYPE_BLOB: | 121 case Element::TYPE_BLOB: |
88 // Blob elements should be resolved beforehand. | 122 // Blob elements should be resolved beforehand. |
89 NOTREACHED(); | 123 NOTREACHED(); |
90 break; | 124 break; |
91 case Element::TYPE_UNKNOWN: | 125 case Element::TYPE_UNKNOWN: |
92 NOTREACHED(); | 126 NOTREACHED(); |
93 break; | 127 break; |
94 } | 128 } |
95 } | 129 } |
96 upload_data->set_identifier(identifier_); | 130 return new net::UploadDataStream(&element_readers, identifier_); |
97 return upload_data; | |
98 } | 131 } |
99 | 132 |
100 ResourceRequestBody::~ResourceRequestBody() {} | 133 ResourceRequestBody::~ResourceRequestBody() {} |
101 | 134 |
102 void ResourceRequestBody::ResolveBlobReference( | 135 void ResourceRequestBody::ResolveBlobReference( |
103 webkit_blob::BlobStorageController* blob_controller, | 136 webkit_blob::BlobStorageController* blob_controller, |
104 const GURL& blob_url, | 137 const GURL& blob_url, |
105 std::vector<const Element*>* resolved_elements) { | 138 std::vector<const Element*>* resolved_elements) { |
106 DCHECK(blob_controller); | 139 DCHECK(blob_controller); |
107 BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url); | 140 BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url); |
(...skipping 11 matching lines...) Expand all Loading... | |
119 | 152 |
120 // Append the elements in the referred blob data. | 153 // Append the elements in the referred blob data. |
121 for (size_t i = 0; i < blob_data->items().size(); ++i) { | 154 for (size_t i = 0; i < blob_data->items().size(); ++i) { |
122 const BlobData::Item& item = blob_data->items().at(i); | 155 const BlobData::Item& item = blob_data->items().at(i); |
123 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type()); | 156 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type()); |
124 resolved_elements->push_back(&item); | 157 resolved_elements->push_back(&item); |
125 } | 158 } |
126 } | 159 } |
127 | 160 |
128 } // namespace webkit_glue | 161 } // namespace webkit_glue |
OLD | NEW |