Home | History | Annotate | Download | only in blob
      1 // Copyright (c) 2013 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/browser/blob/blob_storage_context.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/location.h"
      9 #include "base/logging.h"
     10 #include "base/message_loop/message_loop_proxy.h"
     11 #include "url/gurl.h"
     12 #include "webkit/browser/blob/blob_data_handle.h"
     13 #include "webkit/common/blob/blob_data.h"
     14 
     15 namespace webkit_blob {
     16 
     17 namespace {
     18 
     19 // We can't use GURL directly for these hash fragment manipulations
     20 // since it doesn't have specific knowlege of the BlobURL format. GURL
     21 // treats BlobURLs as if they were PathURLs which don't support hash
     22 // fragments.
     23 
     24 bool BlobUrlHasRef(const GURL& url) {
     25   return url.spec().find('#') != std::string::npos;
     26 }
     27 
     28 GURL ClearBlobUrlRef(const GURL& url) {
     29   size_t hash_pos = url.spec().find('#');
     30   if (hash_pos == std::string::npos)
     31     return url;
     32   return GURL(url.spec().substr(0, hash_pos));
     33 }
     34 
     35 // TODO(michaeln): use base::SysInfo::AmountOfPhysicalMemoryMB() in some
     36 // way to come up with a better limit.
     37 static const int64 kMaxMemoryUsage = 500 * 1024 * 1024;  // Half a gig.
     38 
     39 }  // namespace
     40 
     41 BlobStorageContext::BlobMapEntry::BlobMapEntry()
     42     : refcount(0), flags(0) {
     43 }
     44 
     45 BlobStorageContext::BlobMapEntry::BlobMapEntry(
     46     int refcount, int flags, BlobData* data)
     47     : refcount(refcount), flags(flags), data(data) {
     48 }
     49 
     50 BlobStorageContext::BlobMapEntry::~BlobMapEntry() {
     51 }
     52 
     53 BlobStorageContext::BlobStorageContext()
     54     : memory_usage_(0) {
     55 }
     56 
     57 BlobStorageContext::~BlobStorageContext() {
     58 }
     59 
     60 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID(
     61     const std::string& uuid) {
     62   scoped_ptr<BlobDataHandle> result;
     63   BlobMap::iterator found = blob_map_.find(uuid);
     64   if (found == blob_map_.end())
     65     return result.Pass();
     66   if (found->second.flags & EXCEEDED_MEMORY)
     67     return result.Pass();
     68   DCHECK(!(found->second.flags & BEING_BUILT));
     69   result.reset(new BlobDataHandle(
     70       found->second.data.get(), this, base::MessageLoopProxy::current().get()));
     71   return result.Pass();
     72 }
     73 
     74 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL(
     75     const GURL& url) {
     76   BlobURLMap::iterator found = public_blob_urls_.find(
     77       BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url);
     78   if (found == public_blob_urls_.end())
     79     return scoped_ptr<BlobDataHandle>();
     80   return GetBlobDataFromUUID(found->second);
     81 }
     82 
     83 scoped_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob(
     84     const BlobData* data) {
     85   StartBuildingBlob(data->uuid());
     86   for (std::vector<BlobData::Item>::const_iterator iter =
     87            data->items().begin();
     88        iter != data->items().end(); ++iter) {
     89     AppendBlobDataItem(data->uuid(), *iter);
     90   }
     91   FinishBuildingBlob(data->uuid(), data->content_type());
     92   scoped_ptr<BlobDataHandle> handle = GetBlobDataFromUUID(data->uuid());
     93   DecrementBlobRefCount(data->uuid());
     94   return handle.Pass();
     95 }
     96 
     97 void BlobStorageContext::StartBuildingBlob(const std::string& uuid) {
     98   DCHECK(!IsInUse(uuid) && !uuid.empty());
     99   blob_map_[uuid] = BlobMapEntry(1, BEING_BUILT, new BlobData(uuid));
    100 }
    101 
    102 void BlobStorageContext::AppendBlobDataItem(
    103     const std::string& uuid, const BlobData::Item& item) {
    104   DCHECK(IsBeingBuilt(uuid));
    105   BlobMap::iterator found = blob_map_.find(uuid);
    106   if (found == blob_map_.end())
    107     return;
    108   if (found->second.flags & EXCEEDED_MEMORY)
    109     return;
    110   BlobData* target_blob_data = found->second.data.get();
    111   DCHECK(target_blob_data);
    112 
    113   bool exceeded_memory = false;
    114 
    115   // The blob data is stored in the canonical way which only contains a
    116   // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items
    117   // are expanded into the primitive constituent types.
    118   // 1) The Data item is denoted by the raw data and length.
    119   // 2) The File item is denoted by the file path, the range and the expected
    120   //    modification time.
    121   // 3) The FileSystem File item is denoted by the FileSystem URL, the range
    122   //    and the expected modification time.
    123   // 4) The Blob items are expanded.
    124   // TODO(michaeln): Would be nice to avoid copying Data items when expanding.
    125 
    126   DCHECK(item.length() > 0);
    127   switch (item.type()) {
    128     case BlobData::Item::TYPE_BYTES:
    129       DCHECK(!item.offset());
    130       exceeded_memory = !AppendBytesItem(target_blob_data,
    131                                          item.bytes(),
    132                                          static_cast<int64>(item.length()));
    133       break;
    134     case BlobData::Item::TYPE_FILE:
    135       AppendFileItem(target_blob_data,
    136                      item.path(),
    137                      item.offset(),
    138                      item.length(),
    139                      item.expected_modification_time());
    140       break;
    141     case BlobData::Item::TYPE_FILE_FILESYSTEM:
    142       AppendFileSystemFileItem(target_blob_data,
    143                                item.filesystem_url(),
    144                                item.offset(),
    145                                item.length(),
    146                                item.expected_modification_time());
    147       break;
    148     case BlobData::Item::TYPE_BLOB: {
    149       scoped_ptr<BlobDataHandle> src = GetBlobDataFromUUID(item.blob_uuid());
    150       if (src)
    151         exceeded_memory = !ExpandStorageItems(target_blob_data,
    152                                               src->data(),
    153                                               item.offset(),
    154                                               item.length());
    155       break;
    156     }
    157     default:
    158       NOTREACHED();
    159       break;
    160   }
    161 
    162   // If we're using too much memory, drop this blob's data.
    163   // TODO(michaeln): Blob memory storage does not yet spill over to disk,
    164   // as a stop gap, we'll prevent memory usage over a max amount.
    165   if (exceeded_memory) {
    166     memory_usage_ -= target_blob_data->GetMemoryUsage();
    167     found->second.flags |= EXCEEDED_MEMORY;
    168     found->second.data = new BlobData(uuid);
    169     return;
    170   }
    171 }
    172 
    173 void BlobStorageContext::FinishBuildingBlob(
    174     const std::string& uuid, const std::string& content_type) {
    175   DCHECK(IsBeingBuilt(uuid));
    176   BlobMap::iterator found = blob_map_.find(uuid);
    177   if (found == blob_map_.end())
    178     return;
    179   found->second.data->set_content_type(content_type);
    180   found->second.flags &= ~BEING_BUILT;
    181 }
    182 
    183 void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) {
    184   DCHECK(IsBeingBuilt(uuid));
    185   DecrementBlobRefCount(uuid);
    186 }
    187 
    188 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) {
    189   BlobMap::iterator found = blob_map_.find(uuid);
    190   if (found == blob_map_.end()) {
    191     DCHECK(false);
    192     return;
    193   }
    194   ++(found->second.refcount);
    195 }
    196 
    197 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) {
    198   BlobMap::iterator found = blob_map_.find(uuid);
    199   if (found == blob_map_.end())
    200     return;
    201   DCHECK_EQ(found->second.data->uuid(), uuid);
    202   if (--(found->second.refcount) == 0) {
    203     memory_usage_ -= found->second.data->GetMemoryUsage();
    204     blob_map_.erase(found);
    205   }
    206 }
    207 
    208 void BlobStorageContext::RegisterPublicBlobURL(
    209     const GURL& blob_url, const std::string& uuid) {
    210   DCHECK(!BlobUrlHasRef(blob_url));
    211   DCHECK(IsInUse(uuid));
    212   DCHECK(!IsUrlRegistered(blob_url));
    213   IncrementBlobRefCount(uuid);
    214   public_blob_urls_[blob_url] = uuid;
    215 }
    216 
    217 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) {
    218   DCHECK(!BlobUrlHasRef(blob_url));
    219   if (!IsUrlRegistered(blob_url))
    220     return;
    221   DecrementBlobRefCount(public_blob_urls_[blob_url]);
    222   public_blob_urls_.erase(blob_url);
    223 }
    224 
    225 bool BlobStorageContext::ExpandStorageItems(
    226     BlobData* target_blob_data, BlobData* src_blob_data,
    227     uint64 offset, uint64 length) {
    228   DCHECK(target_blob_data && src_blob_data &&
    229          length != static_cast<uint64>(-1));
    230 
    231   std::vector<BlobData::Item>::const_iterator iter =
    232       src_blob_data->items().begin();
    233   if (offset) {
    234     for (; iter != src_blob_data->items().end(); ++iter) {
    235       if (offset >= iter->length())
    236         offset -= iter->length();
    237       else
    238         break;
    239     }
    240   }
    241 
    242   for (; iter != src_blob_data->items().end() && length > 0; ++iter) {
    243     uint64 current_length = iter->length() - offset;
    244     uint64 new_length = current_length > length ? length : current_length;
    245     if (iter->type() == BlobData::Item::TYPE_BYTES) {
    246       if (!AppendBytesItem(
    247               target_blob_data,
    248               iter->bytes() + static_cast<size_t>(iter->offset() + offset),
    249               static_cast<int64>(new_length))) {
    250         return false;  // exceeded memory
    251       }
    252     } else if (iter->type() == BlobData::Item::TYPE_FILE) {
    253       AppendFileItem(target_blob_data,
    254                      iter->path(),
    255                      iter->offset() + offset,
    256                      new_length,
    257                      iter->expected_modification_time());
    258     } else {
    259       DCHECK(iter->type() == BlobData::Item::TYPE_FILE_FILESYSTEM);
    260       AppendFileSystemFileItem(target_blob_data,
    261                                iter->filesystem_url(),
    262                                iter->offset() + offset,
    263                                new_length,
    264                                iter->expected_modification_time());
    265     }
    266     length -= new_length;
    267     offset = 0;
    268   }
    269   return true;
    270 }
    271 
    272 bool BlobStorageContext::AppendBytesItem(
    273     BlobData* target_blob_data, const char* bytes, int64 length) {
    274   if (length < 0) {
    275     DCHECK(false);
    276     return false;
    277   }
    278   if (memory_usage_ + length > kMaxMemoryUsage)
    279     return false;
    280   target_blob_data->AppendData(bytes, static_cast<size_t>(length));
    281   memory_usage_ += length;
    282   return true;
    283 }
    284 
    285 void BlobStorageContext::AppendFileItem(
    286     BlobData* target_blob_data,
    287     const base::FilePath& file_path, uint64 offset, uint64 length,
    288     const base::Time& expected_modification_time) {
    289   target_blob_data->AppendFile(file_path, offset, length,
    290                                expected_modification_time);
    291 
    292   // It may be a temporary file that should be deleted when no longer needed.
    293   scoped_refptr<ShareableFileReference> shareable_file =
    294       ShareableFileReference::Get(file_path);
    295   if (shareable_file.get())
    296     target_blob_data->AttachShareableFileReference(shareable_file.get());
    297 }
    298 
    299 void BlobStorageContext::AppendFileSystemFileItem(
    300     BlobData* target_blob_data,
    301     const GURL& filesystem_url, uint64 offset, uint64 length,
    302     const base::Time& expected_modification_time) {
    303   target_blob_data->AppendFileSystemFile(filesystem_url, offset, length,
    304                                          expected_modification_time);
    305 }
    306 
    307 bool BlobStorageContext::IsInUse(const std::string& uuid) {
    308   return blob_map_.find(uuid) != blob_map_.end();
    309 }
    310 
    311 bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) {
    312   BlobMap::iterator found = blob_map_.find(uuid);
    313   if (found == blob_map_.end())
    314     return false;
    315   return found->second.flags & BEING_BUILT;
    316 }
    317 
    318 bool BlobStorageContext::IsUrlRegistered(const GURL& blob_url) {
    319   return public_blob_urls_.find(blob_url) != public_blob_urls_.end();
    320 }
    321 
    322 }  // namespace webkit_blob
    323