- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / dom_storage / dom_storage_namespace.h
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.
4
5 #ifndef CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_
6 #define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_
7
8 #include <map>
9 #include <vector>
10
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"
17 #include "url/gurl.h"
18
19 namespace content {
20
21 class DOMStorageArea;
22 class DOMStorageTaskRunner;
23 class SessionStorageDatabase;
24
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> {
29  public:
30   // Option for PurgeMemory.
31   enum PurgeOption {
32     // Purge unopened areas only.
33     PURGE_UNOPENED,
34
35     // Purge aggressively, i.e. discard cache even for areas that have
36     // non-zero open count.
37     PURGE_AGGRESSIVE,
38   };
39
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);
44
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);
51
52   int64 namespace_id() const { return namespace_id_; }
53   const std::string& persistent_namespace_id() const {
54     return persistent_namespace_id_;
55   }
56
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);
62
63   // Returns the area for |origin| if it's open, otherwise NULL.
64   DOMStorageArea* GetOpenStorageArea(const GURL& origin);
65
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);
71
72   void DeleteLocalStorageOrigin(const GURL& origin);
73   void DeleteSessionStorageOrigin(const GURL& origin);
74   void PurgeMemory(PurgeOption purge);
75   void Shutdown();
76
77   unsigned int CountInMemoryAreas() const;
78
79   void AddTransactionLogProcessId(int process_id);
80   void RemoveTransactionLogProcessId(int process_id);
81   SessionStorageNamespace::MergeResult CanMerge(int process_id,
82                                                 DOMStorageNamespace* other);
83
84   enum LogType {
85     TRANSACTION_READ,
86     TRANSACTION_WRITE,
87     TRANSACTION_REMOVE,
88     TRANSACTION_CLEAR
89   };
90
91   struct TransactionRecord {
92     LogType transaction_type;
93     GURL origin;
94     base::string16 key;
95     base::NullableString16 value;
96     TransactionRecord();
97     ~TransactionRecord();
98   };
99
100   void AddTransaction(int process_id, const TransactionRecord& transaction);
101   bool IsLoggingRenderer(int process_id);
102
103  private:
104   friend class base::RefCountedThreadSafe<DOMStorageNamespace>;
105
106   // Struct to hold references to our contained areas and
107   // to keep track of how many tabs have a given area open.
108   struct AreaHolder {
109     scoped_refptr<DOMStorageArea> area_;
110     int open_count_;
111     AreaHolder();
112     AreaHolder(DOMStorageArea* area, int count);
113     ~AreaHolder();
114   };
115   typedef std::map<GURL, AreaHolder> AreaMap;
116
117   struct TransactionData {
118     bool max_log_size_exceeded;
119     std::vector<TransactionRecord> log;
120     TransactionData();
121     ~TransactionData();
122   };
123
124   ~DOMStorageNamespace();
125
126   // Returns a pointer to the area holder in our map or NULL.
127   AreaHolder* GetAreaHolder(const GURL& origin);
128
129   int64 namespace_id_;
130   std::string persistent_namespace_id_;
131   base::FilePath directory_;
132   AreaMap areas_;
133   scoped_refptr<DOMStorageTaskRunner> task_runner_;
134   scoped_refptr<SessionStorageDatabase> session_storage_database_;
135   std::map<int, TransactionData*> transactions_;
136 };
137
138 }  // namespace content
139
140
141 #endif  // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_NAMESPACE_H_