1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/dom_storage/dom_storage_session.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/tracked_objects.h"
#include "content/browser/dom_storage/dom_storage_context_impl.h"
#include "content/browser/dom_storage/dom_storage_task_runner.h"
namespace content {
namespace {
void PostMergeTaskResult(
const SessionStorageNamespace::MergeResultCallback& callback,
SessionStorageNamespace::MergeResult result) {
callback.Run(result);
}
void RunMergeTaskAndPostResult(
const base::Callback<SessionStorageNamespace::MergeResult(void)>& task,
scoped_refptr<base::SingleThreadTaskRunner> result_loop,
const SessionStorageNamespace::MergeResultCallback& callback) {
SessionStorageNamespace::MergeResult result = task.Run();
result_loop->PostTask(
FROM_HERE, base::Bind(&PostMergeTaskResult, callback, result));
}
} // namespace
DOMStorageSession::DOMStorageSession(DOMStorageContextImpl* context)
: context_(context),
namespace_id_(context->AllocateSessionId()),
persistent_namespace_id_(context->AllocatePersistentSessionId()),
should_persist_(false) {
context->task_runner()->PostTask(
FROM_HERE,
base::Bind(&DOMStorageContextImpl::CreateSessionNamespace,
context_, namespace_id_, persistent_namespace_id_));
}
DOMStorageSession::DOMStorageSession(DOMStorageContextImpl* context,
const std::string& persistent_namespace_id)
: context_(context),
namespace_id_(context->AllocateSessionId()),
persistent_namespace_id_(persistent_namespace_id),
should_persist_(false) {
context->task_runner()->PostTask(
FROM_HERE,
base::Bind(&DOMStorageContextImpl::CreateSessionNamespace,
context_, namespace_id_, persistent_namespace_id_));
}
DOMStorageSession::DOMStorageSession(
DOMStorageSession* master_dom_storage_session)
: context_(master_dom_storage_session->context_),
namespace_id_(master_dom_storage_session->context_->AllocateSessionId()),
persistent_namespace_id_(
master_dom_storage_session->persistent_namespace_id()),
should_persist_(false) {
context_->task_runner()->PostTask(
FROM_HERE,
base::Bind(&DOMStorageContextImpl::CreateAliasSessionNamespace,
context_,
master_dom_storage_session->namespace_id(),
namespace_id_,
persistent_namespace_id_));
}
void DOMStorageSession::SetShouldPersist(bool should_persist) {
should_persist_ = should_persist;
}
bool DOMStorageSession::should_persist() const {
return should_persist_;
}
bool DOMStorageSession::IsFromContext(DOMStorageContextImpl* context) {
return context_.get() == context;
}
DOMStorageSession* DOMStorageSession::Clone() {
return CloneFrom(context_.get(), namespace_id_);
}
// static
DOMStorageSession* DOMStorageSession::CloneFrom(DOMStorageContextImpl* context,
int64 namepace_id_to_clone) {
int64 clone_id = context->AllocateSessionId();
std::string persistent_clone_id = context->AllocatePersistentSessionId();
context->task_runner()->PostTask(
FROM_HERE,
base::Bind(&DOMStorageContextImpl::CloneSessionNamespace,
context, namepace_id_to_clone, clone_id, persistent_clone_id));
return new DOMStorageSession(context, clone_id, persistent_clone_id);
}
DOMStorageSession::DOMStorageSession(DOMStorageContextImpl* context,
int64 namespace_id,
const std::string& persistent_namespace_id)
: context_(context),
namespace_id_(namespace_id),
persistent_namespace_id_(persistent_namespace_id),
should_persist_(false) {
// This ctor is intended for use by the Clone() method.
}
DOMStorageSession::~DOMStorageSession() {
context_->task_runner()->PostTask(
FROM_HERE,
base::Bind(&DOMStorageContextImpl::DeleteSessionNamespace,
context_, namespace_id_, should_persist_));
}
void DOMStorageSession::AddTransactionLogProcessId(int process_id) {
context_->task_runner()->PostTask(
FROM_HERE,
base::Bind(&DOMStorageContextImpl::AddTransactionLogProcessId,
context_, namespace_id_, process_id));
}
void DOMStorageSession::RemoveTransactionLogProcessId(int process_id) {
context_->task_runner()->PostTask(
FROM_HERE,
base::Bind(&DOMStorageContextImpl::RemoveTransactionLogProcessId,
context_, namespace_id_, process_id));
}
void DOMStorageSession::Merge(
bool actually_merge,
int process_id,
DOMStorageSession* other,
const SessionStorageNamespace::MergeResultCallback& callback) {
scoped_refptr<base::SingleThreadTaskRunner> current_loop(
base::ThreadTaskRunnerHandle::Get());
SessionStorageNamespace::MergeResultCallback cb =
base::Bind(&DOMStorageSession::ProcessMergeResult,
this,
actually_merge,
callback,
other->persistent_namespace_id());
context_->task_runner()->PostTask(
FROM_HERE,
base::Bind(&RunMergeTaskAndPostResult,
base::Bind(&DOMStorageContextImpl::MergeSessionStorage,
context_, namespace_id_, actually_merge, process_id,
other->namespace_id_),
current_loop,
cb));
}
void DOMStorageSession::ProcessMergeResult(
bool actually_merge,
const SessionStorageNamespace::MergeResultCallback& callback,
const std::string& new_persistent_namespace_id,
SessionStorageNamespace::MergeResult result) {
if (actually_merge &&
(result == SessionStorageNamespace::MERGE_RESULT_MERGEABLE ||
result == SessionStorageNamespace::MERGE_RESULT_NO_TRANSACTIONS)) {
persistent_namespace_id_ = new_persistent_namespace_id;
}
callback.Run(result);
}
} // namespace content
|