1 // Copyright 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.
5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_
6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/strings/nullable_string16.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/session_storage_namespace.h"
22 class DOMStorageTaskRunner;
23 class SessionStorageDatabase;
25 // Container for the set of per-origin Areas.
26 // See class comments for DOMStorageContextImpl for a larger overview.
27 class CONTENT_EXPORT DOMStorageNamespace
28 : public base::RefCountedThreadSafe<DOMStorageNamespace> {
30 // Option for PurgeMemory.
32 // Purge unopened areas only.
35 // Purge aggressively, i.e. discard cache even for areas that have
36 // non-zero open count.
40 // Constructor for a LocalStorage namespace with id of 0
41 // and an optional backing directory on disk.
42 DOMStorageNamespace(const base::FilePath& directory, // may be empty
43 DOMStorageTaskRunner* task_runner);
45 // Constructor for a SessionStorage namespace with a non-zero id and an
46 // optional backing on disk via |session_storage_database| (may be NULL).
47 DOMStorageNamespace(int64 namespace_id,
48 const std::string& persistent_namespace_id,
49 SessionStorageDatabase* session_storage_database,
50 DOMStorageTaskRunner* task_runner);
52 int64 namespace_id() const { return namespace_id_; }
53 const std::string& persistent_namespace_id() const {
54 return persistent_namespace_id_;
57 // Returns the storage area for the given origin,
58 // creating instance if needed. Each call to open
59 // must be balanced with a call to CloseStorageArea.
60 DOMStorageArea* OpenStorageArea(const GURL& origin);
61 void CloseStorageArea(DOMStorageArea* area);
63 // Returns the area for |origin| if it's open, otherwise NULL.
64 DOMStorageArea* GetOpenStorageArea(const GURL& origin);
66 // Creates a clone of |this| namespace including
67 // shallow copies of all contained areas.
68 // Should only be called for session storage namespaces.
69 DOMStorageNamespace* Clone(int64 clone_namespace_id,
70 const std::string& clone_persistent_namespace_id);
72 void DeleteLocalStorageOrigin(const GURL& origin);
73 void DeleteSessionStorageOrigin(const GURL& origin);
74 void PurgeMemory(PurgeOption purge);
77 unsigned int CountInMemoryAreas() const;
79 void AddTransactionLogProcessId(int process_id);
80 void RemoveTransactionLogProcessId(int process_id);
81 SessionStorageNamespace::MergeResult CanMerge(int process_id,
82 DOMStorageNamespace* other);
91 struct TransactionRecord {
92 LogType transaction_type;
95 base::NullableString16 value;
100 void AddTransaction(int process_id, const TransactionRecord& transaction);
101 bool IsLoggingRenderer(int process_id);
104 friend class base::RefCountedThreadSafe<DOMStorageNamespace>;
106 // Struct to hold references to our contained areas and
107 // to keep track of how many tabs have a given area open.
109 scoped_refptr<DOMStorageArea> area_;
112 AreaHolder(DOMStorageArea* area, int count);
115 typedef std::map<GURL, AreaHolder> AreaMap;
117 struct TransactionData {
118 bool max_log_size_exceeded;
119 std::vector<TransactionRecord> log;
124 ~DOMStorageNamespace();
126 // Returns a pointer to the area holder in our map or NULL.
127 AreaHolder* GetAreaHolder(const GURL& origin);
130 std::string persistent_namespace_id_;
131 base::FilePath directory_;
133 scoped_refptr<DOMStorageTaskRunner> task_runner_;
134 scoped_refptr<SessionStorageDatabase> session_storage_database_;
135 std::map<int, TransactionData*> transactions_;
138 } // namespace content
141 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_