Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: content/browser/loader/upload_data_stream_builder.cc

Issue 810403004: [Storage] Blob Storage Refactoring pt 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: BUILD.gn file changes Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/fileapi/upload_file_system_file_element_reader.h" 8 #include "content/browser/fileapi/upload_file_system_file_element_reader.h"
9 #include "content/common/resource_request_body.h" 9 #include "content/common/resource_request_body.h"
10 #include "net/base/elements_upload_data_stream.h" 10 #include "net/base/elements_upload_data_stream.h"
11 #include "net/base/upload_bytes_element_reader.h" 11 #include "net/base/upload_bytes_element_reader.h"
12 #include "net/base/upload_file_element_reader.h" 12 #include "net/base/upload_file_element_reader.h"
13 #include "storage/browser/blob/blob_data_handle.h" 13 #include "storage/browser/blob/blob_data_handle.h"
14 #include "storage/browser/blob/blob_data_snapshot.h"
14 #include "storage/browser/blob/blob_storage_context.h" 15 #include "storage/browser/blob/blob_storage_context.h"
15 16
16 using storage::BlobData;
17 using storage::BlobDataHandle;
18 using storage::BlobStorageContext;
19
20 namespace content { 17 namespace content {
21 namespace { 18 namespace {
22 19
23 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody. 20 // A subclass of net::UploadBytesElementReader which owns ResourceRequestBody.
24 class BytesElementReader : public net::UploadBytesElementReader { 21 class BytesElementReader : public net::UploadBytesElementReader {
25 public: 22 public:
26 BytesElementReader(ResourceRequestBody* resource_request_body, 23 BytesElementReader(ResourceRequestBody* resource_request_body,
27 const ResourceRequestBody::Element& element) 24 const ResourceRequestBody::Element& element)
28 : net::UploadBytesElementReader(element.bytes(), element.length()), 25 : net::UploadBytesElementReader(element.bytes(), element.length()),
29 resource_request_body_(resource_request_body) { 26 resource_request_body_(resource_request_body) {
(...skipping 27 matching lines...) Expand all
57 54
58 ~FileElementReader() override {} 55 ~FileElementReader() override {}
59 56
60 private: 57 private:
61 scoped_refptr<ResourceRequestBody> resource_request_body_; 58 scoped_refptr<ResourceRequestBody> resource_request_body_;
62 59
63 DISALLOW_COPY_AND_ASSIGN(FileElementReader); 60 DISALLOW_COPY_AND_ASSIGN(FileElementReader);
64 }; 61 };
65 62
66 void ResolveBlobReference( 63 void ResolveBlobReference(
64 ResourceRequestBody* body,
67 storage::BlobStorageContext* blob_context, 65 storage::BlobStorageContext* blob_context,
68 const ResourceRequestBody::Element& element, 66 const ResourceRequestBody::Element& element,
69 std::vector<const ResourceRequestBody::Element*>* resolved_elements) { 67 std::vector<const ResourceRequestBody::Element*>* resolved_elements) {
70 DCHECK(blob_context); 68 DCHECK(blob_context);
71 scoped_ptr<storage::BlobDataHandle> handle = 69 scoped_ptr<storage::BlobDataHandle> handle =
72 blob_context->GetBlobDataFromUUID(element.blob_uuid()); 70 blob_context->GetBlobDataFromUUID(element.blob_uuid());
73 DCHECK(handle); 71 DCHECK(handle);
74 if (!handle) 72 if (!handle)
75 return; 73 return;
76 74
75 // TODO(dmurph): Create a reader for blobs instead of decomposing the blob
76 // and storing the snapshot on the request to keep the resources around.
77 // Currently a handle is attached to the request in the resource dispatcher
78 // host, so we know the blob won't go away, but it's not very clear or useful.
79 scoped_ptr<storage::BlobDataSnapshot> snapshot = handle->CreateSnapshot();
77 // If there is no element in the referred blob data, just return. 80 // If there is no element in the referred blob data, just return.
78 if (handle->data()->items().empty()) 81 if (snapshot->items().empty())
79 return; 82 return;
80 83
81 // Append the elements in the referenced blob data. 84 // Append the elements in the referenced blob data.
82 for (size_t i = 0; i < handle->data()->items().size(); ++i) { 85 for (const auto& item : snapshot->items()) {
83 const BlobData::Item& item = handle->data()->items().at(i); 86 DCHECK_NE(storage::DataElement::TYPE_BLOB, item->type());
84 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type()); 87 resolved_elements->push_back(item->data_element_ptr());
85 resolved_elements->push_back(&item);
86 } 88 }
89 body->SetUserData(snapshot.get(), snapshot.release());
michaeln 2015/01/22 02:18:09 i'm don't think this is safe, release() can get ca
dmurph 2015/01/23 00:10:18 Done.
87 } 90 }
88 91
89 } // namespace 92 } // namespace
90 93
91 scoped_ptr<net::UploadDataStream> UploadDataStreamBuilder::Build( 94 scoped_ptr<net::UploadDataStream> UploadDataStreamBuilder::Build(
92 ResourceRequestBody* body, 95 ResourceRequestBody* body,
93 BlobStorageContext* blob_context, 96 storage::BlobStorageContext* blob_context,
94 storage::FileSystemContext* file_system_context, 97 storage::FileSystemContext* file_system_context,
95 base::TaskRunner* file_task_runner) { 98 base::TaskRunner* file_task_runner) {
96 // Resolve all blob elements. 99 // Resolve all blob elements.
97 std::vector<const ResourceRequestBody::Element*> resolved_elements; 100 std::vector<const ResourceRequestBody::Element*> resolved_elements;
98 for (size_t i = 0; i < body->elements()->size(); ++i) { 101 for (size_t i = 0; i < body->elements()->size(); ++i) {
99 const ResourceRequestBody::Element& element = (*body->elements())[i]; 102 const ResourceRequestBody::Element& element = (*body->elements())[i];
100 if (element.type() == ResourceRequestBody::Element::TYPE_BLOB) 103 if (element.type() == ResourceRequestBody::Element::TYPE_BLOB)
101 ResolveBlobReference(blob_context, element, &resolved_elements); 104 ResolveBlobReference(body, blob_context, element, &resolved_elements);
102 else 105 else
103 resolved_elements.push_back(&element); 106 resolved_elements.push_back(&element);
104 } 107 }
105 108
106 ScopedVector<net::UploadElementReader> element_readers; 109 ScopedVector<net::UploadElementReader> element_readers;
107 for (size_t i = 0; i < resolved_elements.size(); ++i) { 110 for (size_t i = 0; i < resolved_elements.size(); ++i) {
108 const ResourceRequestBody::Element& element = *resolved_elements[i]; 111 const ResourceRequestBody::Element& element = *resolved_elements[i];
109 switch (element.type()) { 112 switch (element.type()) {
110 case ResourceRequestBody::Element::TYPE_BYTES: 113 case ResourceRequestBody::Element::TYPE_BYTES:
111 element_readers.push_back(new BytesElementReader(body, element)); 114 element_readers.push_back(new BytesElementReader(body, element));
112 break; 115 break;
113 case ResourceRequestBody::Element::TYPE_FILE: 116 case ResourceRequestBody::Element::TYPE_FILE:
114 element_readers.push_back( 117 element_readers.push_back(
115 new FileElementReader(body, file_task_runner, element)); 118 new FileElementReader(body, file_task_runner, element));
116 break; 119 break;
117 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM: 120 case ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM:
118 // If |body| contains any filesystem URLs, the caller should have 121 // If |body| contains any filesystem URLs, the caller should have
119 // supplied a FileSystemContext. 122 // supplied a FileSystemContext.
120 DCHECK(file_system_context); 123 DCHECK(file_system_context);
121 element_readers.push_back( 124 element_readers.push_back(
122 new content::UploadFileSystemFileElementReader( 125 new content::UploadFileSystemFileElementReader(
123 file_system_context, 126 file_system_context,
124 element.filesystem_url(), 127 element.filesystem_url(),
125 element.offset(), 128 element.offset(),
126 element.length(), 129 element.length(),
127 element.expected_modification_time())); 130 element.expected_modification_time()));
128 break; 131 break;
129 case ResourceRequestBody::Element::TYPE_BLOB: 132 case ResourceRequestBody::Element::TYPE_BLOB:
130 // Blob elements should be resolved beforehand. 133 // Blob elements should be resolved beforehand.
134 // TODO(dmurph): Create blob reader and store the snapshot in there.
131 NOTREACHED(); 135 NOTREACHED();
132 break; 136 break;
133 case ResourceRequestBody::Element::TYPE_UNKNOWN: 137 case ResourceRequestBody::Element::TYPE_UNKNOWN:
134 NOTREACHED(); 138 NOTREACHED();
135 break; 139 break;
136 } 140 }
137 } 141 }
138 142
139 return make_scoped_ptr( 143 return make_scoped_ptr(
140 new net::ElementsUploadDataStream(element_readers.Pass(), 144 new net::ElementsUploadDataStream(element_readers.Pass(),
141 body->identifier())); 145 body->identifier()));
142 } 146 }
143 147
144 } // namespace content 148 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698