// Copyright (c) 2012 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 "webkit/dom_storage/dom_storage_context.h" #include "base/bind.h" #include "base/bind_helpers.h" #include "base/time.h" #include "webkit/dom_storage/dom_storage_namespace.h" #include "webkit/dom_storage/dom_storage_task_runner.h" #include "webkit/dom_storage/dom_storage_types.h" #include "webkit/quota/special_storage_policy.h" namespace dom_storage { DomStorageContext::DomStorageContext( const FilePath& directory, quota::SpecialStoragePolicy* special_storage_policy, DomStorageTaskRunner* task_runner) : directory_(directory), task_runner_(task_runner) { // AtomicSequenceNum starts at 0 but we want to start session // namespace ids at one since zero is reserved for the // kLocalStorageNamespaceId. session_id_sequence_.GetNext(); } DomStorageContext::~DomStorageContext() { } DomStorageNamespace* DomStorageContext::GetStorageNamespace( int64 namespace_id) { StorageNamespaceMap::iterator found = namespaces_.find(namespace_id); if (found == namespaces_.end()) { if (namespace_id == kLocalStorageNamespaceId) { DomStorageNamespace* local = new DomStorageNamespace(directory_, task_runner_); namespaces_[kLocalStorageNamespaceId] = local; return local; } return NULL; } return found->second; } void DomStorageContext::AddEventObserver(EventObserver* observer) { event_observers_.AddObserver(observer); } void DomStorageContext::RemoveEventObserver(EventObserver* observer) { event_observers_.RemoveObserver(observer); } void DomStorageContext::NotifyItemSet( const DomStorageArea* area, const string16& key, const string16& new_value, const NullableString16& old_value, const GURL& page_url) { FOR_EACH_OBSERVER( EventObserver, event_observers_, OnDomStorageItemSet(area, key, new_value, old_value, page_url)); } void DomStorageContext::NotifyItemRemoved( const DomStorageArea* area, const string16& key, const string16& old_value, const GURL& page_url) { FOR_EACH_OBSERVER( EventObserver, event_observers_, OnDomStorageItemRemoved(area, key, old_value, page_url)); } void DomStorageContext::NotifyAreaCleared( const DomStorageArea* area, const GURL& page_url) { FOR_EACH_OBSERVER( EventObserver, event_observers_, OnDomStorageAreaCleared(area, page_url)); } void DomStorageContext::CreateSessionNamespace( int64 namespace_id) { DCHECK(namespace_id != kLocalStorageNamespaceId); DCHECK(namespaces_.find(namespace_id) == namespaces_.end()); namespaces_[namespace_id] = new DomStorageNamespace( namespace_id, task_runner_); } void DomStorageContext::DeleteSessionNamespace( int64 namespace_id) { DCHECK_NE(kLocalStorageNamespaceId, namespace_id); namespaces_.erase(namespace_id); } void DomStorageContext::CloneSessionNamespace( int64 existing_id, int64 new_id) { DCHECK_NE(kLocalStorageNamespaceId, existing_id); DCHECK_NE(kLocalStorageNamespaceId, new_id); StorageNamespaceMap::iterator found = namespaces_.find(existing_id); if (found != namespaces_.end()) namespaces_[new_id] = found->second->Clone(new_id); else CreateSessionNamespace(new_id); } } // namespace dom_storage