| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/blob/blob_storage_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "googleurl/src/gurl.h" |
| 9 #include "webkit/blob/blob_data.h" |
| 10 |
| 11 namespace webkit_blob { |
| 12 |
| 13 BlobStorageController::BlobStorageController() { |
| 14 } |
| 15 |
| 16 BlobStorageController::~BlobStorageController() { |
| 17 } |
| 18 |
| 19 void BlobStorageController::AppendStorageItems( |
| 20 BlobData* target_blob_data, BlobData* src_blob_data, |
| 21 uint64 offset, uint64 length) { |
| 22 DCHECK(target_blob_data && src_blob_data && |
| 23 length != static_cast<uint64>(-1)); |
| 24 |
| 25 std::vector<BlobData::Item>::const_iterator iter = |
| 26 src_blob_data->items().begin(); |
| 27 if (offset) { |
| 28 for (; iter != src_blob_data->items().end(); ++iter) { |
| 29 if (offset >= iter->length()) |
| 30 offset -= iter->length(); |
| 31 else |
| 32 break; |
| 33 } |
| 34 } |
| 35 |
| 36 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { |
| 37 uint64 current_length = iter->length() - offset; |
| 38 uint64 new_length = current_length > length ? length : current_length; |
| 39 if (iter->type() == BlobData::TYPE_DATA) { |
| 40 target_blob_data->AppendData(iter->data(), |
| 41 static_cast<uint32>(iter->offset() + offset), |
| 42 static_cast<uint32>(new_length)); |
| 43 } else { |
| 44 DCHECK(iter->type() == BlobData::TYPE_FILE); |
| 45 target_blob_data->AppendFile(iter->file_path(), |
| 46 iter->offset() + offset, |
| 47 new_length, |
| 48 iter->expected_modification_time()); |
| 49 } |
| 50 length -= new_length; |
| 51 offset = 0; |
| 52 } |
| 53 } |
| 54 |
| 55 void BlobStorageController::RegisterBlobUrl( |
| 56 const GURL& url, const BlobData* blob_data) { |
| 57 scoped_refptr<BlobData> target_blob_data = new BlobData(); |
| 58 target_blob_data->set_content_type(blob_data->content_type()); |
| 59 target_blob_data->set_content_disposition(blob_data->content_disposition()); |
| 60 |
| 61 // The blob data is stored in the "canonical" way. That is, it only contains a |
| 62 // list of Data and File items. |
| 63 // 1) The Data item is denoted by the raw data and the range. |
| 64 // 2) The File item is denoted by the file path, the range and the expected |
| 65 // modification time. |
| 66 // All the Blob items in the passing blob data are resolved and expanded into |
| 67 // a set of Data and File items. |
| 68 |
| 69 for (std::vector<BlobData::Item>::const_iterator iter = |
| 70 blob_data->items().begin(); |
| 71 iter != blob_data->items().end(); ++iter) { |
| 72 switch (iter->type()) { |
| 73 case BlobData::TYPE_DATA: |
| 74 target_blob_data->AppendData(iter->data()); |
| 75 break; |
| 76 case BlobData::TYPE_FILE: |
| 77 target_blob_data->AppendFile(iter->file_path(), |
| 78 iter->offset(), |
| 79 iter->length(), |
| 80 iter->expected_modification_time()); |
| 81 break; |
| 82 case BlobData::TYPE_BLOB: { |
| 83 scoped_refptr<BlobData> src_blob_data = |
| 84 blob_map_[iter->blob_url().spec()]; |
| 85 DCHECK(src_blob_data.get()); |
| 86 if (src_blob_data.get()) |
| 87 AppendStorageItems(target_blob_data.get(), |
| 88 src_blob_data.get(), |
| 89 iter->offset(), |
| 90 iter->length()); |
| 91 break; |
| 92 } |
| 93 } |
| 94 } |
| 95 |
| 96 blob_map_[url.spec()] = target_blob_data; |
| 97 } |
| 98 |
| 99 void BlobStorageController::RegisterBlobUrlFrom( |
| 100 const GURL& url, const GURL& src_url) { |
| 101 BlobData* blob_data = GetBlobDataFromUrl(src_url); |
| 102 if (!blob_data) |
| 103 return; |
| 104 |
| 105 blob_map_[url.spec()] = blob_data; |
| 106 } |
| 107 |
| 108 void BlobStorageController::UnregisterBlobUrl(const GURL& url) { |
| 109 blob_map_.erase(url.spec()); |
| 110 } |
| 111 |
| 112 BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { |
| 113 return (blob_map_.find(url.spec()) == blob_map_.end()) ? |
| 114 NULL : blob_map_[url.spec()].get(); |
| 115 } |
| 116 |
| 117 } // namespace webkit_blob |
| OLD | NEW |