OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/blob/blob_storage_controller.h" | 5 #include "webkit/blob/blob_storage_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
9 #include "net/base/upload_data.h" | 9 #include "net/base/upload_data.h" |
10 #include "webkit/blob/blob_data.h" | 10 #include "webkit/blob/blob_data.h" |
11 | 11 |
12 namespace webkit_blob { | 12 namespace webkit_blob { |
13 | 13 |
14 BlobStorageController::BlobStorageController() { | 14 BlobStorageController::BlobStorageController() { |
15 } | 15 } |
16 | 16 |
17 BlobStorageController::~BlobStorageController() { | 17 BlobStorageController::~BlobStorageController() { |
18 } | 18 } |
19 | 19 |
20 void BlobStorageController::AppendStorageItems( | |
21 BlobData* target_blob_data, BlobData* src_blob_data, | |
22 uint64 offset, uint64 length) { | |
23 DCHECK(target_blob_data && src_blob_data && | |
24 length != static_cast<uint64>(-1)); | |
25 | |
26 std::vector<BlobData::Item>::const_iterator iter = | |
27 src_blob_data->items().begin(); | |
28 if (offset) { | |
29 for (; iter != src_blob_data->items().end(); ++iter) { | |
30 if (offset >= iter->length()) | |
31 offset -= iter->length(); | |
32 else | |
33 break; | |
34 } | |
35 } | |
36 | |
37 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { | |
38 uint64 current_length = iter->length() - offset; | |
39 uint64 new_length = current_length > length ? length : current_length; | |
40 if (iter->type() == BlobData::TYPE_DATA) { | |
41 target_blob_data->AppendData(iter->data(), | |
42 static_cast<uint32>(iter->offset() + offset), | |
43 static_cast<uint32>(new_length)); | |
44 } else { | |
45 DCHECK(iter->type() == BlobData::TYPE_FILE); | |
46 target_blob_data->AppendFile(iter->file_path(), | |
47 iter->offset() + offset, | |
48 new_length, | |
49 iter->expected_modification_time()); | |
50 } | |
51 length -= new_length; | |
52 offset = 0; | |
53 } | |
54 } | |
55 | |
56 void BlobStorageController::RegisterBlobUrl( | 20 void BlobStorageController::RegisterBlobUrl( |
57 const GURL& url, const BlobData* blob_data) { | 21 const GURL& url, const BlobData* blob_data) { |
58 scoped_refptr<BlobData> target_blob_data = new BlobData(); | 22 scoped_refptr<BlobData> target_blob_data = new BlobData(); |
59 target_blob_data->set_content_type(blob_data->content_type()); | 23 target_blob_data->set_content_type(blob_data->content_type()); |
60 target_blob_data->set_content_disposition(blob_data->content_disposition()); | 24 target_blob_data->set_content_disposition(blob_data->content_disposition()); |
61 | 25 |
62 // The blob data is stored in the "canonical" way. That is, it only contains a | 26 // The blob data is stored in the "canonical" way. That is, it only contains a |
63 // list of Data and File items. | 27 // list of Data and File items. |
64 // 1) The Data item is denoted by the raw data and the range. | 28 // 1) The Data item is denoted by the raw data and the range. |
65 // 2) The File item is denoted by the file path, the range and the expected | 29 // 2) The File item is denoted by the file path, the range and the expected |
66 // modification time. | 30 // modification time. |
67 // All the Blob items in the passing blob data are resolved and expanded into | 31 // All the Blob items in the passing blob data are resolved and expanded into |
68 // a set of Data and File items. | 32 // a set of Data and File items. |
69 | 33 |
70 for (std::vector<BlobData::Item>::const_iterator iter = | 34 for (std::vector<BlobData::Item>::const_iterator iter = |
71 blob_data->items().begin(); | 35 blob_data->items().begin(); |
72 iter != blob_data->items().end(); ++iter) { | 36 iter != blob_data->items().end(); ++iter) { |
73 switch (iter->type()) { | 37 switch (iter->type()) { |
74 case BlobData::TYPE_DATA: { | 38 case BlobData::TYPE_DATA: { |
75 // WebBlobData does not allow partial data. | 39 // WebBlobData does not allow partial data. |
76 DCHECK(!(iter->offset()) && iter->length() == iter->data().size()); | 40 DCHECK(!(iter->offset()) && iter->length() == iter->data().size()); |
77 target_blob_data->AppendData(iter->data()); | 41 target_blob_data->AppendData(iter->data()); |
78 break; | 42 break; |
79 } | 43 } |
80 case BlobData::TYPE_FILE: | 44 case BlobData::TYPE_FILE: |
81 target_blob_data->AppendFile(iter->file_path(), | 45 AppendFileItem(target_blob_data, |
82 iter->offset(), | 46 iter->file_path(), |
83 iter->length(), | 47 iter->offset(), |
84 iter->expected_modification_time()); | 48 iter->length(), |
| 49 iter->expected_modification_time()); |
85 break; | 50 break; |
86 case BlobData::TYPE_BLOB: { | 51 case BlobData::TYPE_BLOB: { |
87 BlobData* src_blob_data = GetBlobDataFromUrl(iter->blob_url()); | 52 BlobData* src_blob_data = GetBlobDataFromUrl(iter->blob_url()); |
88 DCHECK(src_blob_data); | 53 DCHECK(src_blob_data); |
89 if (src_blob_data) | 54 if (src_blob_data) |
90 AppendStorageItems(target_blob_data.get(), | 55 AppendStorageItems(target_blob_data.get(), |
91 src_blob_data, | 56 src_blob_data, |
92 iter->offset(), | 57 iter->offset(), |
93 iter->length()); | 58 iter->length()); |
94 break; | 59 break; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 item.expected_modification_time()); | 137 item.expected_modification_time()); |
173 break; | 138 break; |
174 default: | 139 default: |
175 NOTREACHED(); | 140 NOTREACHED(); |
176 break; | 141 break; |
177 } | 142 } |
178 } | 143 } |
179 } | 144 } |
180 } | 145 } |
181 | 146 |
| 147 void BlobStorageController::AppendStorageItems( |
| 148 BlobData* target_blob_data, BlobData* src_blob_data, |
| 149 uint64 offset, uint64 length) { |
| 150 DCHECK(target_blob_data && src_blob_data && |
| 151 length != static_cast<uint64>(-1)); |
| 152 |
| 153 std::vector<BlobData::Item>::const_iterator iter = |
| 154 src_blob_data->items().begin(); |
| 155 if (offset) { |
| 156 for (; iter != src_blob_data->items().end(); ++iter) { |
| 157 if (offset >= iter->length()) |
| 158 offset -= iter->length(); |
| 159 else |
| 160 break; |
| 161 } |
| 162 } |
| 163 |
| 164 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { |
| 165 uint64 current_length = iter->length() - offset; |
| 166 uint64 new_length = current_length > length ? length : current_length; |
| 167 if (iter->type() == BlobData::TYPE_DATA) { |
| 168 target_blob_data->AppendData(iter->data(), |
| 169 static_cast<uint32>(iter->offset() + offset), |
| 170 static_cast<uint32>(new_length)); |
| 171 } else { |
| 172 DCHECK(iter->type() == BlobData::TYPE_FILE); |
| 173 AppendFileItem(target_blob_data, |
| 174 iter->file_path(), |
| 175 iter->offset() + offset, |
| 176 new_length, |
| 177 iter->expected_modification_time()); |
| 178 } |
| 179 length -= new_length; |
| 180 offset = 0; |
| 181 } |
| 182 } |
| 183 |
| 184 void BlobStorageController::AppendFileItem( |
| 185 BlobData* target_blob_data, |
| 186 const FilePath& file_path, uint64 offset, uint64 length, |
| 187 const base::Time& expected_modification_time) { |
| 188 target_blob_data->AppendFile(file_path, offset, length, |
| 189 expected_modification_time); |
| 190 |
| 191 // It may be a temporary file that should be deleted when no longer needed. |
| 192 scoped_refptr<DeletableFileReference> deletable_file = |
| 193 DeletableFileReference::Get(file_path); |
| 194 if (deletable_file) |
| 195 target_blob_data->AttachDeletableFileReference(deletable_file); |
| 196 } |
| 197 |
182 } // namespace webkit_blob | 198 } // namespace webkit_blob |
OLD | NEW |