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_SESSION_H_
6#define CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_SESSION_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "content/browser/dom_storage/session_storage_namespace_impl.h"
13#include "content/common/content_export.h"
14
15namespace content {
16
17class DOMStorageContextImpl;
18
19// This refcounted class determines the lifetime of a session
20// storage namespace and provides an interface to Clone() an
21// existing session storage namespace. It may be used on any thread.
22// See class comments for DOMStorageContextImpl for a larger overview.
23class CONTENT_EXPORT DOMStorageSession
24    : public base::RefCountedThreadSafe<DOMStorageSession> {
25 public:
26  // Constructs a |DOMStorageSession| and allocates new IDs for it.
27  explicit DOMStorageSession(DOMStorageContextImpl* context);
28
29  // Constructs a |DOMStorageSession| and assigns |persistent_namespace_id|
30  // to it. Allocates a new non-persistent ID.
31  DOMStorageSession(DOMStorageContextImpl* context,
32                    const std::string& persistent_namespace_id);
33
34  // Constructs a |DOMStorageSession| as an alias of
35  // |master_dom_storage_session|. Allocates a new non-persistent ID.
36  explicit DOMStorageSession(DOMStorageSession* master_dom_storage_session);
37
38  int64 namespace_id() const { return namespace_id_; }
39  const std::string& persistent_namespace_id() const {
40    return persistent_namespace_id_;
41  }
42  void SetShouldPersist(bool should_persist);
43  bool should_persist() const;
44  bool IsFromContext(DOMStorageContextImpl* context);
45  DOMStorageSession* Clone();
46
47  // Constructs a |DOMStorageSession| by cloning
48  // |namespace_id_to_clone|. Allocates new IDs for it.
49  static DOMStorageSession* CloneFrom(DOMStorageContextImpl* context,
50                                      int64 namepace_id_to_clone);
51
52  void AddTransactionLogProcessId(int process_id);
53  void RemoveTransactionLogProcessId(int process_id);
54  void Merge(bool actually_merge,
55             int process_id,
56             DOMStorageSession* other,
57             const SessionStorageNamespace::MergeResultCallback& callback);
58
59 private:
60  friend class base::RefCountedThreadSafe<DOMStorageSession>;
61
62  DOMStorageSession(DOMStorageContextImpl* context,
63                    int64 namespace_id,
64                    const std::string& persistent_namespace_id);
65  ~DOMStorageSession();
66
67  void ProcessMergeResult(
68      bool actually_merge,
69      const SessionStorageNamespace::MergeResultCallback& callback,
70      const std::string& new_persistent_namespace_id,
71      SessionStorageNamespace::MergeResult result);
72
73  scoped_refptr<DOMStorageContextImpl> context_;
74  int64 namespace_id_;
75  std::string persistent_namespace_id_;
76  bool should_persist_;
77
78  DISALLOW_IMPLICIT_CONSTRUCTORS(DOMStorageSession);
79};
80
81}  // namespace content
82
83#endif  // CONTENT_BROWSER_DOM_STORAGE_DOM_STORAGE_SESSION_H_
84