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