Commit 8da7297a authored by michaeln's avatar michaeln Committed by Commit bot

[DOMStorage] (recommitting) Offset the session storage namespace ids generated...

[DOMStorage] (recommitting) Offset the session storage namespace ids generated by different contexts to help identify when an id from one is mistakenly used in another.

>> Committed: https://crrev.com/36dcef3da5317365ab95e50a5f6b7fa98ada1d5e
>> Cr-Commit-Position: refs/heads/master@{#314256}
>
> Reverted: https://crrev.com/7bdc4a49e605261b60ee1339f93e835d853bf645
> Cr-Commit-Position: refs/heads/master@{#314274}

Review URL: https://codereview.chromium.org/881253004

Cr-Commit-Position: refs/heads/master@{#314713}
parent 2079b57f
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "content/browser/dom_storage/dom_storage_context_impl.h" #include "content/browser/dom_storage/dom_storage_context_impl.h"
#include <stdlib.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/files/file_enumerator.h" #include "base/files/file_enumerator.h"
...@@ -26,6 +28,10 @@ namespace content { ...@@ -26,6 +28,10 @@ namespace content {
static const int kSessionStoraceScavengingSeconds = 60; static const int kSessionStoraceScavengingSeconds = 60;
// Offset the session storage namespace ids generated by different contexts
// to help identify when an id from one is mistakenly used in another.
static int g_session_id_offset_sequence = 1;
DOMStorageContextImpl::DOMStorageContextImpl( DOMStorageContextImpl::DOMStorageContextImpl(
const base::FilePath& localstorage_directory, const base::FilePath& localstorage_directory,
const base::FilePath& sessionstorage_directory, const base::FilePath& sessionstorage_directory,
...@@ -34,6 +40,7 @@ DOMStorageContextImpl::DOMStorageContextImpl( ...@@ -34,6 +40,7 @@ DOMStorageContextImpl::DOMStorageContextImpl(
: localstorage_directory_(localstorage_directory), : localstorage_directory_(localstorage_directory),
sessionstorage_directory_(sessionstorage_directory), sessionstorage_directory_(sessionstorage_directory),
task_runner_(task_runner), task_runner_(task_runner),
session_id_offset_(abs((g_session_id_offset_sequence++ % 10)) * 1000),
is_shutdown_(false), is_shutdown_(false),
force_keep_session_state_(false), force_keep_session_state_(false),
special_storage_policy_(special_storage_policy), special_storage_policy_(special_storage_policy),
...@@ -227,6 +234,10 @@ void DOMStorageContextImpl::NotifyAreaCleared( ...@@ -227,6 +234,10 @@ void DOMStorageContextImpl::NotifyAreaCleared(
OnDOMStorageAreaCleared(area, page_url)); OnDOMStorageAreaCleared(area, page_url));
} }
int64 DOMStorageContextImpl::AllocateSessionId() {
return session_id_sequence_.GetNext() + session_id_offset_;
}
std::string DOMStorageContextImpl::AllocatePersistentSessionId() { std::string DOMStorageContextImpl::AllocatePersistentSessionId() {
std::string guid = base::GenerateGUID(); std::string guid = base::GenerateGUID();
std::replace(guid.begin(), guid.end(), '-', '_'); std::replace(guid.begin(), guid.end(), '-', '_');
......
...@@ -147,10 +147,7 @@ class CONTENT_EXPORT DOMStorageContextImpl ...@@ -147,10 +147,7 @@ class CONTENT_EXPORT DOMStorageContextImpl
const GURL& page_url); const GURL& page_url);
// May be called on any thread. // May be called on any thread.
int64 AllocateSessionId() { int64 AllocateSessionId();
return session_id_sequence_.GetNext();
}
std::string AllocatePersistentSessionId(); std::string AllocatePersistentSessionId();
// Must be called on the background thread. // Must be called on the background thread.
...@@ -207,7 +204,10 @@ class CONTENT_EXPORT DOMStorageContextImpl ...@@ -207,7 +204,10 @@ class CONTENT_EXPORT DOMStorageContextImpl
// We use a 32 bit identifier for per tab storage sessions. // We use a 32 bit identifier for per tab storage sessions.
// At a tab per second, this range is large enough for 68 years. // At a tab per second, this range is large enough for 68 years.
// The offset is to more quickly detect the error condition where
// an id related to one context is mistakenly used in another.
base::AtomicSequenceNumber session_id_sequence_; base::AtomicSequenceNumber session_id_sequence_;
const int session_id_offset_;
bool is_shutdown_; bool is_shutdown_;
bool force_keep_session_state_; bool force_keep_session_state_;
......
...@@ -63,6 +63,8 @@ class DOMStorageContextImplTest : public testing::Test { ...@@ -63,6 +63,8 @@ class DOMStorageContextImplTest : public testing::Test {
EXPECT_EQ(origin, infos[0].origin); EXPECT_EQ(origin, infos[0].origin);
} }
int session_id_offset() { return context_->session_id_offset_; }
protected: protected:
base::MessageLoop message_loop_; base::MessageLoop message_loop_;
base::ScopedTempDir temp_dir_; base::ScopedTempDir temp_dir_;
...@@ -80,7 +82,7 @@ TEST_F(DOMStorageContextImplTest, Basics) { ...@@ -80,7 +82,7 @@ TEST_F(DOMStorageContextImplTest, Basics) {
EXPECT_EQ(base::FilePath(), context_->sessionstorage_directory()); EXPECT_EQ(base::FilePath(), context_->sessionstorage_directory());
EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get()); EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get());
context_->DeleteLocalStorage(GURL("http://chromium.org/")); context_->DeleteLocalStorage(GURL("http://chromium.org/"));
const int kFirstSessionStorageNamespaceId = 1; const int kFirstSessionStorageNamespaceId = 1 + session_id_offset();
EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)); EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId));
EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId)); EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId));
EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId()); EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId());
...@@ -162,7 +164,7 @@ TEST_F(DOMStorageContextImplTest, SetForceKeepSessionState) { ...@@ -162,7 +164,7 @@ TEST_F(DOMStorageContextImplTest, SetForceKeepSessionState) {
} }
TEST_F(DOMStorageContextImplTest, PersistentIds) { TEST_F(DOMStorageContextImplTest, PersistentIds) {
const int kFirstSessionStorageNamespaceId = 1; const int kFirstSessionStorageNamespaceId = 1 + session_id_offset();
const std::string kPersistentId = "persistent"; const std::string kPersistentId = "persistent";
context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId, context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId,
kPersistentId); kPersistentId);
...@@ -175,7 +177,7 @@ TEST_F(DOMStorageContextImplTest, PersistentIds) { ...@@ -175,7 +177,7 @@ TEST_F(DOMStorageContextImplTest, PersistentIds) {
EXPECT_EQ(kPersistentId, area->persistent_namespace_id_); EXPECT_EQ(kPersistentId, area->persistent_namespace_id_);
// Verify that the persistent IDs are handled correctly when cloning. // Verify that the persistent IDs are handled correctly when cloning.
const int kClonedSessionStorageNamespaceId = 2; const int kClonedSessionStorageNamespaceId = 2 + session_id_offset();
const std::string kClonedPersistentId = "cloned"; const std::string kClonedPersistentId = "cloned";
context_->CloneSessionNamespace(kFirstSessionStorageNamespaceId, context_->CloneSessionNamespace(kFirstSessionStorageNamespaceId,
kClonedSessionStorageNamespaceId, kClonedSessionStorageNamespaceId,
...@@ -200,7 +202,7 @@ TEST_F(DOMStorageContextImplTest, DeleteSessionStorage) { ...@@ -200,7 +202,7 @@ TEST_F(DOMStorageContextImplTest, DeleteSessionStorage) {
ASSERT_EQ(temp_dir_.path(), context_->sessionstorage_directory()); ASSERT_EQ(temp_dir_.path(), context_->sessionstorage_directory());
// Write data. // Write data.
const int kSessionStorageNamespaceId = 1; const int kSessionStorageNamespaceId = 1 + session_id_offset();
const std::string kPersistentId = "persistent"; const std::string kPersistentId = "persistent";
context_->CreateSessionNamespace(kSessionStorageNamespaceId, context_->CreateSessionNamespace(kSessionStorageNamespaceId,
kPersistentId); kPersistentId);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment