Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_namespace.h
blob48f449a615d78a632e0f74e0dec68d2b37bcb7d7
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_
8 #include <map>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "content/common/content_export.h"
15 #include "url/gurl.h"
17 namespace content {
19 class DOMStorageArea;
20 class DOMStorageTaskRunner;
21 class SessionStorageDatabase;
23 // Container for the set of per-origin Areas.
24 // See class comments for DOMStorageContextImpl for a larger overview.
25 class CONTENT_EXPORT DOMStorageNamespace
26 : public base::RefCountedThreadSafe<DOMStorageNamespace> {
27 public:
28 // Option for PurgeMemory.
29 enum PurgeOption {
30 // Purge unopened areas only.
31 PURGE_UNOPENED,
33 // Purge aggressively, i.e. discard cache even for areas that have
34 // non-zero open count.
35 PURGE_AGGRESSIVE,
38 // Constructor for a LocalStorage namespace with id of 0
39 // and an optional backing directory on disk.
40 DOMStorageNamespace(const base::FilePath& directory, // may be empty
41 DOMStorageTaskRunner* task_runner);
43 // Constructor for a SessionStorage namespace with a non-zero id and an
44 // optional backing on disk via |session_storage_database| (may be NULL).
45 DOMStorageNamespace(int64 namespace_id,
46 const std::string& persistent_namespace_id,
47 SessionStorageDatabase* session_storage_database,
48 DOMStorageTaskRunner* task_runner);
50 int64 namespace_id() const { return namespace_id_; }
51 const std::string& persistent_namespace_id() const {
52 return persistent_namespace_id_;
55 // Returns the storage area for the given origin,
56 // creating instance if needed. Each call to open
57 // must be balanced with a call to CloseStorageArea.
58 DOMStorageArea* OpenStorageArea(const GURL& origin);
59 void CloseStorageArea(DOMStorageArea* area);
61 // Returns the area for |origin| if it's open, otherwise NULL.
62 DOMStorageArea* GetOpenStorageArea(const GURL& origin);
64 // Creates a clone of |this| namespace including
65 // shallow copies of all contained areas.
66 // Should only be called for session storage namespaces.
67 DOMStorageNamespace* Clone(int64 clone_namespace_id,
68 const std::string& clone_persistent_namespace_id);
70 void DeleteLocalStorageOrigin(const GURL& origin);
71 void DeleteSessionStorageOrigin(const GURL& origin);
72 void PurgeMemory(PurgeOption purge);
73 void Shutdown();
74 void Flush();
76 unsigned int CountInMemoryAreas() const;
78 private:
79 friend class base::RefCountedThreadSafe<DOMStorageNamespace>;
81 // Struct to hold references to our contained areas and
82 // to keep track of how many tabs have a given area open.
83 struct AreaHolder {
84 scoped_refptr<DOMStorageArea> area_;
85 int open_count_;
86 AreaHolder();
87 AreaHolder(DOMStorageArea* area, int count);
88 ~AreaHolder();
90 typedef std::map<GURL, AreaHolder> AreaMap;
92 ~DOMStorageNamespace();
94 // Returns a pointer to the area holder in our map or NULL.
95 AreaHolder* GetAreaHolder(const GURL& origin);
97 int64 namespace_id_;
98 std::string persistent_namespace_id_;
99 base::FilePath directory_;
100 AreaMap areas_;
101 scoped_refptr<DOMStorageTaskRunner> task_runner_;
102 scoped_refptr<SessionStorageDatabase> session_storage_database_;
105 } // namespace content
108 #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_