Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 5 | #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_ |
| 6 | #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_ |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/files/file_path.h" |
| 12 | #include "base/memory/ref_counted.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 13 | #include "content/common/content_export.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 14 | |
| 15 | class GURL; |
| 16 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 17 | namespace content { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 18 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 19 | class DOMStorageArea; |
| 20 | class DOMStorageTaskRunner; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 21 | class SessionStorageDatabase; |
| 22 | |
| 23 | // Container for the set of per-origin Areas. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 24 | // See class comments for DOMStorageContextImpl for a larger overview. |
| 25 | class CONTENT_EXPORT DOMStorageNamespace |
| 26 | : public base::RefCountedThreadSafe<DOMStorageNamespace> { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 27 | public: |
| 28 | // Option for PurgeMemory. |
| 29 | enum PurgeOption { |
| 30 | // Purge unopened areas only. |
| 31 | PURGE_UNOPENED, |
| 32 | |
| 33 | // Purge aggressively, i.e. discard cache even for areas that have |
| 34 | // non-zero open count. |
| 35 | PURGE_AGGRESSIVE, |
| 36 | }; |
| 37 | |
| 38 | // Constructor for a LocalStorage namespace with id of 0 |
| 39 | // and an optional backing directory on disk. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 40 | DOMStorageNamespace(const base::FilePath& directory, // may be empty |
| 41 | DOMStorageTaskRunner* task_runner); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 42 | |
| 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). |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 45 | DOMStorageNamespace(int64 namespace_id, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 46 | const std::string& persistent_namespace_id, |
| 47 | SessionStorageDatabase* session_storage_database, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 48 | DOMStorageTaskRunner* task_runner); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 49 | |
| 50 | int64 namespace_id() const { return namespace_id_; } |
| 51 | const std::string& persistent_namespace_id() const { |
| 52 | return persistent_namespace_id_; |
| 53 | } |
| 54 | |
| 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. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 58 | DOMStorageArea* OpenStorageArea(const GURL& origin); |
| 59 | void CloseStorageArea(DOMStorageArea* area); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 60 | |
| 61 | // Returns the area for |origin| if it's open, otherwise NULL. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 62 | DOMStorageArea* GetOpenStorageArea(const GURL& origin); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 63 | |
| 64 | // Creates a clone of |this| namespace including |
| 65 | // shallow copies of all contained areas. |
| 66 | // Should only be called for session storage namespaces. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 67 | DOMStorageNamespace* Clone(int64 clone_namespace_id, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 68 | const std::string& clone_persistent_namespace_id); |
| 69 | |
| 70 | void DeleteLocalStorageOrigin(const GURL& origin); |
| 71 | void DeleteSessionStorageOrigin(const GURL& origin); |
| 72 | void PurgeMemory(PurgeOption purge); |
| 73 | void Shutdown(); |
| 74 | |
| 75 | unsigned int CountInMemoryAreas() const; |
| 76 | |
| 77 | private: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 78 | friend class base::RefCountedThreadSafe<DOMStorageNamespace>; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 79 | |
| 80 | // Struct to hold references to our contained areas and |
| 81 | // to keep track of how many tabs have a given area open. |
| 82 | struct AreaHolder { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 83 | scoped_refptr<DOMStorageArea> area_; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 84 | int open_count_; |
| 85 | AreaHolder(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 86 | AreaHolder(DOMStorageArea* area, int count); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 87 | ~AreaHolder(); |
| 88 | }; |
| 89 | typedef std::map<GURL, AreaHolder> AreaMap; |
| 90 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 91 | ~DOMStorageNamespace(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 92 | |
| 93 | // Returns a pointer to the area holder in our map or NULL. |
| 94 | AreaHolder* GetAreaHolder(const GURL& origin); |
| 95 | |
| 96 | int64 namespace_id_; |
| 97 | std::string persistent_namespace_id_; |
| 98 | base::FilePath directory_; |
| 99 | AreaMap areas_; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 100 | scoped_refptr<DOMStorageTaskRunner> task_runner_; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 101 | scoped_refptr<SessionStorageDatabase> session_storage_database_; |
| 102 | }; |
| 103 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 104 | } // namespace content |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 105 | |
| 106 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 107 | #endif // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_ |