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

Side by Side Diff: webkit/glue/resource_request_body.cc

Issue 11439008: net: Change argument of URLRequest::set_upload from UploadData to UploadDataStream (Closed) Base URL: http://git.chromium.org/chromium/src.git@chunk
Patch Set: Fix android Created 8 years 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
« no previous file with comments | « webkit/glue/resource_request_body.h ('k') | webkit/glue/resource_request_body_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This class is necessary to ensure the BlobData and any attached shareable
40 // files survive until upload completion.
41 class FileElementReader : public net::UploadFileElementReader {
42 public:
43 FileElementReader(ResourceRequestBody* resource_request_body,
44 const ResourceRequestBody::Element& element)
45 : net::UploadFileElementReader(element.path(),
46 element.offset(),
47 element.length(),
48 element.expected_modification_time()),
49 resource_request_body_(resource_request_body) {
50 DCHECK_EQ(ResourceRequestBody::Element::TYPE_FILE, element.type());
51 }
52
53 virtual ~FileElementReader() {}
54
55 private:
56 scoped_refptr<ResourceRequestBody> resource_request_body_;
57
58 DISALLOW_COPY_AND_ASSIGN(FileElementReader);
59 };
60
61 } // namespace
62
16 ResourceRequestBody::ResourceRequestBody() : identifier_(0) {} 63 ResourceRequestBody::ResourceRequestBody() : identifier_(0) {}
17 64
18 void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) { 65 void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) {
19 if (bytes_len > 0) { 66 if (bytes_len > 0) {
20 elements_.push_back(Element()); 67 elements_.push_back(Element());
21 elements_.back().SetToBytes(bytes, bytes_len); 68 elements_.back().SetToBytes(bytes, bytes_len);
22 } 69 }
23 } 70 }
24 71
25 void ResourceRequestBody::AppendFileRange( 72 void ResourceRequestBody::AppendFileRange(
(...skipping 11 matching lines...) Expand all
37 } 84 }
38 85
39 void ResourceRequestBody::AppendFileSystemFileRange( 86 void ResourceRequestBody::AppendFileSystemFileRange(
40 const GURL& url, uint64 offset, uint64 length, 87 const GURL& url, uint64 offset, uint64 length,
41 const base::Time& expected_modification_time) { 88 const base::Time& expected_modification_time) {
42 elements_.push_back(Element()); 89 elements_.push_back(Element());
43 elements_.back().SetToFileSystemUrlRange(url, offset, length, 90 elements_.back().SetToFileSystemUrlRange(url, offset, length,
44 expected_modification_time); 91 expected_modification_time);
45 } 92 }
46 93
47 net::UploadData* ResourceRequestBody::ResolveElementsAndCreateUploadData( 94 net::UploadDataStream*
95 ResourceRequestBody::ResolveElementsAndCreateUploadDataStream(
48 BlobStorageController* blob_controller) { 96 BlobStorageController* blob_controller) {
49 // Resolve all blob elements. 97 // Resolve all blob elements.
50 std::vector<const Element*> resolved_elements; 98 std::vector<const Element*> resolved_elements;
51 for (size_t i = 0; i < elements_.size(); ++i) { 99 for (size_t i = 0; i < elements_.size(); ++i) {
52 const Element& element = elements_[i]; 100 const Element& element = elements_[i];
53 if (element.type() == Element::TYPE_BLOB) { 101 if (element.type() == Element::TYPE_BLOB) {
54 ResolveBlobReference(blob_controller, element.url(), &resolved_elements); 102 ResolveBlobReference(blob_controller, element.url(), &resolved_elements);
55 } else { 103 } else {
56 // No need to resolve, just append the element. 104 // No need to resolve, just append the element.
57 resolved_elements.push_back(&element); 105 resolved_elements.push_back(&element);
58 } 106 }
59 } 107 }
60 108
61 net::UploadData* upload_data = new net::UploadData; 109 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) { 110 for (size_t i = 0; i < resolved_elements.size(); ++i) {
69 const Element& element = *resolved_elements[i]; 111 const Element& element = *resolved_elements[i];
70 switch (element.type()) { 112 switch (element.type()) {
71 case Element::TYPE_BYTES: 113 case Element::TYPE_BYTES:
72 elements->push_back(new net::UploadElement()); 114 element_readers.push_back(new BytesElementReader(this, element));
73 elements->back()->SetToSharedBytes(element.bytes(), element.length());
74 break; 115 break;
75 case Element::TYPE_FILE: 116 case Element::TYPE_FILE:
76 elements->push_back(new net::UploadElement()); 117 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; 118 break;
83 case Element::TYPE_FILE_FILESYSTEM: 119 case Element::TYPE_FILE_FILESYSTEM:
84 // TODO(kinuko): Resolve FileSystemURL before creating UploadData. 120 // TODO(kinuko): Resolve FileSystemURL before creating UploadData.
85 NOTREACHED(); 121 NOTREACHED();
86 break; 122 break;
87 case Element::TYPE_BLOB: 123 case Element::TYPE_BLOB:
88 // Blob elements should be resolved beforehand. 124 // Blob elements should be resolved beforehand.
89 NOTREACHED(); 125 NOTREACHED();
90 break; 126 break;
91 case Element::TYPE_UNKNOWN: 127 case Element::TYPE_UNKNOWN:
92 NOTREACHED(); 128 NOTREACHED();
93 break; 129 break;
94 } 130 }
95 } 131 }
96 upload_data->set_identifier(identifier_); 132 return new net::UploadDataStream(&element_readers, identifier_);
97 return upload_data;
98 } 133 }
99 134
100 ResourceRequestBody::~ResourceRequestBody() {} 135 ResourceRequestBody::~ResourceRequestBody() {}
101 136
102 void ResourceRequestBody::ResolveBlobReference( 137 void ResourceRequestBody::ResolveBlobReference(
103 webkit_blob::BlobStorageController* blob_controller, 138 webkit_blob::BlobStorageController* blob_controller,
104 const GURL& blob_url, 139 const GURL& blob_url,
105 std::vector<const Element*>* resolved_elements) { 140 std::vector<const Element*>* resolved_elements) {
106 DCHECK(blob_controller); 141 DCHECK(blob_controller);
107 BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url); 142 BlobData* blob_data = blob_controller->GetBlobDataFromUrl(blob_url);
(...skipping 11 matching lines...) Expand all
119 154
120 // Append the elements in the referred blob data. 155 // Append the elements in the referred blob data.
121 for (size_t i = 0; i < blob_data->items().size(); ++i) { 156 for (size_t i = 0; i < blob_data->items().size(); ++i) {
122 const BlobData::Item& item = blob_data->items().at(i); 157 const BlobData::Item& item = blob_data->items().at(i);
123 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type()); 158 DCHECK_NE(BlobData::Item::TYPE_BLOB, item.type());
124 resolved_elements->push_back(&item); 159 resolved_elements->push_back(&item);
125 } 160 }
126 } 161 }
127 162
128 } // namespace webkit_glue 163 } // namespace webkit_glue
OLDNEW
« no previous file with comments | « webkit/glue/resource_request_body.h ('k') | webkit/glue/resource_request_body_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698