OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "content/browser/loader/upload_data_stream_builder.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_data_handle.h" |
| 12 #include "webkit/browser/blob/blob_storage_context.h" |
12 #include "webkit/browser/fileapi/upload_file_system_file_element_reader.h" | 13 #include "webkit/browser/fileapi/upload_file_system_file_element_reader.h" |
13 #include "webkit/common/resource_request_body.h" | 14 #include "webkit/common/resource_request_body.h" |
14 | 15 |
15 using webkit_blob::BlobData; | 16 using webkit_blob::BlobData; |
16 using webkit_blob::BlobStorageController; | 17 using webkit_blob::BlobDataHandle; |
| 18 using webkit_blob::BlobStorageContext; |
17 using webkit_glue::ResourceRequestBody; | 19 using webkit_glue::ResourceRequestBody; |
18 | 20 |
19 namespace content { | 21 namespace content { |
20 namespace { | 22 namespace { |
21 | 23 |
22 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. | 24 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. |
23 class BytesElementReader : public net::UploadBytesElementReader { | 25 class BytesElementReader : public net::UploadBytesElementReader { |
24 public: | 26 public: |
25 BytesElementReader(ResourceRequestBody* resource_request_body, | 27 BytesElementReader(ResourceRequestBody* resource_request_body, |
26 const ResourceRequestBody::Element& element) | 28 const ResourceRequestBody::Element& element) |
(...skipping 30 matching lines...) Expand all Loading... |
57 virtual ~FileElementReader() {} | 59 virtual ~FileElementReader() {} |
58 | 60 |
59 private: | 61 private: |
60 scoped_refptr<ResourceRequestBody> resource_request_body_; | 62 scoped_refptr<ResourceRequestBody> resource_request_body_; |
61 | 63 |
62 DISALLOW_COPY_AND_ASSIGN(FileElementReader); | 64 DISALLOW_COPY_AND_ASSIGN(FileElementReader); |
63 }; | 65 }; |
64 | 66 |
65 void ResolveBlobReference( | 67 void ResolveBlobReference( |
66 ResourceRequestBody* body, | 68 ResourceRequestBody* body, |
67 webkit_blob::BlobStorageController* blob_controller, | 69 webkit_blob::BlobStorageContext* blob_context, |
68 const GURL& blob_url, | 70 const ResourceRequestBody::Element& element, |
69 std::vector<const ResourceRequestBody::Element*>* resolved_elements) { | 71 std::vector<const ResourceRequestBody::Element*>* resolved_elements) { |
70 DCHECK(blob_controller); | 72 DCHECK(blob_context); |
71 BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url); | 73 std::string uuid = element.blob_uuid(); |
72 DCHECK(blob_data); | 74 if (uuid.empty()) |
73 if (!blob_data) | 75 uuid = blob_context->LookupUuidFromDeprecatedURL(element.blob_url()); |
| 76 scoped_ptr<webkit_blob::BlobDataHandle> handle = |
| 77 blob_context->GetBlobDataFromUUID(uuid); |
| 78 DCHECK(handle); |
| 79 if (!handle) |
74 return; | 80 return; |
75 | 81 |
76 // If there is no element in the referred blob data, just return. | 82 // If there is no element in the referred blob data, just return. |
77 if (blob_data->items().empty()) | 83 if (handle->data()->items().empty()) |
78 return; | 84 return; |
79 | 85 |
80 // Ensure the blob and any attached shareable files survive until | 86 // Append the elements in the referenced blob data. |
81 // upload completion. | 87 for (size_t i = 0; i < handle->data()->items().size(); ++i) { |
82 body->SetUserData(blob_data, new base::UserDataAdapter<BlobData>(blob_data)); | 88 const BlobData::Item& item = handle->data()->items().at(i); |
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()); | 89 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type()); |
88 resolved_elements->push_back(&item); | 90 resolved_elements->push_back(&item); |
89 } | 91 } |
| 92 |
| 93 // Ensure the blob and any attached shareable files survive until |
| 94 // upload completion. The |body| takes ownership of |handle|. |
| 95 const void* key = handle.get(); |
| 96 body->SetUserData(key, handle.release()); |
90 } | 97 } |
91 | 98 |
92 } // namespace | 99 } // namespace |
93 | 100 |
94 scoped_ptr<net::UploadDataStream> UploadDataStreamBuilder::Build( | 101 scoped_ptr<net::UploadDataStream> UploadDataStreamBuilder::Build( |
95 ResourceRequestBody* body, | 102 ResourceRequestBody* body, |
96 BlobStorageController* blob_controller, | 103 BlobStorageContext* blob_context, |
97 fileapi::FileSystemContext* file_system_context, | 104 fileapi::FileSystemContext* file_system_context, |
98 base::TaskRunner* file_task_runner) { | 105 base::TaskRunner* file_task_runner) { |
99 // Resolve all blob elements. | 106 // Resolve all blob elements. |
100 std::vector<const ResourceRequestBody::Element*> resolved_elements; | 107 std::vector<const ResourceRequestBody::Element*> resolved_elements; |
101 for (size_t i = 0; i < body->elements()->size(); ++i) { | 108 for (size_t i = 0; i < body->elements()->size(); ++i) { |
102 const ResourceRequestBody::Element& element = (*body->elements())[i]; | 109 const ResourceRequestBody::Element& element = (*body->elements())[i]; |
103 if (element.type() == ResourceRequestBody::Element::TYPE_BLOB) { | 110 if (element.type() == ResourceRequestBody::Element::TYPE_BLOB) |
104 ResolveBlobReference(body, blob_controller, element.url(), | 111 ResolveBlobReference(body, blob_context, element, &resolved_elements); |
105 &resolved_elements); | 112 else |
106 } else { | |
107 // No need to resolve, just append the element. | |
108 resolved_elements.push_back(&element); | 113 resolved_elements.push_back(&element); |
109 } | |
110 } | 114 } |
111 | 115 |
112 ScopedVector<net::UploadElementReader> element_readers; | 116 ScopedVector<net::UploadElementReader> element_readers; |
113 for (size_t i = 0; i < resolved_elements.size(); ++i) { | 117 for (size_t i = 0; i < resolved_elements.size(); ++i) { |
114 const ResourceRequestBody::Element& element = *resolved_elements[i]; | 118 const ResourceRequestBody::Element& element = *resolved_elements[i]; |
115 switch (element.type()) { | 119 switch (element.type()) { |
116 case ResourceRequestBody::Element::TYPE_BYTES: | 120 case ResourceRequestBody::Element::TYPE_BYTES: |
117 element_readers.push_back(new BytesElementReader(body, element)); | 121 element_readers.push_back(new BytesElementReader(body, element)); |
118 break; | 122 break; |
119 case ResourceRequestBody::Element::TYPE_FILE: | 123 case ResourceRequestBody::Element::TYPE_FILE: |
120 element_readers.push_back( | 124 element_readers.push_back( |
121 new FileElementReader(body, file_task_runner, element)); | 125 new FileElementReader(body, file_task_runner, element)); |
122 break; | 126 break; |
123 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: | 127 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: |
124 element_readers.push_back( | 128 element_readers.push_back( |
125 new fileapi::UploadFileSystemFileElementReader( | 129 new fileapi::UploadFileSystemFileElementReader( |
126 file_system_context, | 130 file_system_context, |
127 element.url(), | 131 element.filesystem_url(), |
128 element.offset(), | 132 element.offset(), |
129 element.length(), | 133 element.length(), |
130 element.expected_modification_time())); | 134 element.expected_modification_time())); |
131 break; | 135 break; |
132 case ResourceRequestBody::Element::TYPE_BLOB: | 136 case ResourceRequestBody::Element::TYPE_BLOB: |
133 // Blob elements should be resolved beforehand. | 137 // Blob elements should be resolved beforehand. |
134 NOTREACHED(); | 138 NOTREACHED(); |
135 break; | 139 break; |
136 case ResourceRequestBody::Element::TYPE_UNKNOWN: | 140 case ResourceRequestBody::Element::TYPE_UNKNOWN: |
137 NOTREACHED(); | 141 NOTREACHED(); |
138 break; | 142 break; |
139 } | 143 } |
140 } | 144 } |
141 | 145 |
142 return make_scoped_ptr( | 146 return make_scoped_ptr( |
143 new net::UploadDataStream(&element_readers, body->identifier())); | 147 new net::UploadDataStream(&element_readers, body->identifier())); |
144 } | 148 } |
145 | 149 |
146 } // namespace content | 150 } // namespace content |
OLD | NEW |