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 #ifndef WEBKIT_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
      6 #define WEBKIT_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "webkit/browser/webkit_storage_browser_export.h"
     15 #include "webkit/common/blob/blob_data.h"
     16 
     17 class GURL;
     18 
     19 namespace base {
     20 class FilePath;
     21 class Time;
     22 }
     23 
     24 namespace webkit_blob {
     25 
     26 class BlobDataHandle;
     27 
     28 // This class handles the logistics of blob Storage within the browser process,
     29 // and maintains a mapping from blob uuid to the data. The class is single
     30 // threaded and should only be used on the IO thread.
     31 // In chromium, there is one instance per profile.
     32 class WEBKIT_STORAGE_BROWSER_EXPORT BlobStorageContext
     33     : public base::SupportsWeakPtr<BlobStorageContext> {
     34  public:
     35   BlobStorageContext();
     36   ~BlobStorageContext();
     37 
     38   scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid);
     39   scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url);
     40 
     41   // Useful for coining blobs from within the browser process. If the
     42   // blob cannot be added due to memory consumption, returns NULL.
     43   scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobData* blob_data);
     44 
     45  private:
     46   friend class BlobDataHandle;
     47   friend class BlobStorageHost;
     48 
     49   enum EntryFlags {
     50     BEING_BUILT = 1 << 0,
     51     EXCEEDED_MEMORY = 1 << 1,
     52   };
     53 
     54   struct BlobMapEntry {
     55     int refcount;
     56     int flags;
     57     scoped_refptr<BlobData> data;
     58 
     59     BlobMapEntry();
     60     BlobMapEntry(int refcount, int flags, BlobData* data);
     61     ~BlobMapEntry();
     62   };
     63 
     64   typedef std::map<std::string, BlobMapEntry>
     65       BlobMap;
     66   typedef std::map<GURL, std::string> BlobURLMap;
     67 
     68   void StartBuildingBlob(const std::string& uuid);
     69   void AppendBlobDataItem(const std::string& uuid,
     70                           const BlobData::Item& data_item);
     71   void FinishBuildingBlob(const std::string& uuid, const std::string& type);
     72   void CancelBuildingBlob(const std::string& uuid);
     73   void IncrementBlobRefCount(const std::string& uuid);
     74   void DecrementBlobRefCount(const std::string& uuid);
     75   void RegisterPublicBlobURL(const GURL& url, const std::string& uuid);
     76   void RevokePublicBlobURL(const GURL& url);
     77 
     78   bool ExpandStorageItems(BlobData* target_blob_data,
     79                           BlobData* src_blob_data,
     80                           uint64 offset,
     81                           uint64 length);
     82   bool AppendBytesItem(BlobData* target_blob_data,
     83                        const char* data, int64 length);
     84   void AppendFileItem(BlobData* target_blob_data,
     85                       const base::FilePath& file_path,
     86                       uint64 offset, uint64 length,
     87                       const base::Time& expected_modification_time);
     88   void AppendFileSystemFileItem(
     89       BlobData* target_blob_data,
     90       const GURL& url, uint64 offset, uint64 length,
     91       const base::Time& expected_modification_time);
     92 
     93   bool IsInUse(const std::string& uuid);
     94   bool IsBeingBuilt(const std::string& uuid);
     95   bool IsUrlRegistered(const GURL& blob_url);
     96 
     97   BlobMap blob_map_;
     98   BlobURLMap public_blob_urls_;
     99 
    100   // Used to keep track of how much memory is being utitlized for blob data,
    101   // we count only the items of TYPE_DATA which are held in memory and not
    102   // items of TYPE_FILE.
    103   int64 memory_usage_;
    104 
    105   DISALLOW_COPY_AND_ASSIGN(BlobStorageContext);
    106 };
    107 
    108 }  // namespace webkit_blob
    109 
    110 #endif  // WEBKIT_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
    111