summaryrefslogtreecommitdiffstats
path: root/chrome/browser/in_process_webkit/dom_storage_context.cc
blob: 9f763e2755409381603236ff6ad4b08e22eff2bc (plain)
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
// Copyright (c) 2009 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 "chrome/browser/in_process_webkit/dom_storage_context.h"

#include "base/file_path.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/in_process_webkit/storage_area.h"
#include "chrome/browser/in_process_webkit/storage_namespace.h"
#include "chrome/browser/in_process_webkit/webkit_context.h"

DOMStorageContext::DOMStorageContext(WebKitContext* webkit_context)
    : last_storage_area_id_(kFirstStorageAreaId),
      last_storage_namespace_id_(kFirstStorageNamespaceId),
      webkit_context_(webkit_context) {
}

DOMStorageContext::~DOMStorageContext() {
  // This should not go away until all DOM Storage Dispatcher hosts have gone
  // away.  And they remove themselves from this list.
  DCHECK(dispatcher_host_set_.empty());

  // If we don't have any work to do on the WebKit thread, bail.
  if (storage_namespace_map_.empty())
    return;
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));

  // The storage namespace destructor unregisters the storage namespace, so
  // our iterator becomes invalid.  Thus we just keep deleting the first item
  // until there are none left.
  while (!storage_namespace_map_.empty())
    delete storage_namespace_map_.begin()->second;
}

StorageNamespace* DOMStorageContext::LocalStorage() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  StorageNamespace* storage_namespace = GetStorageNamespace(
      kLocalStorageNamespaceId);
  if (storage_namespace)
    return storage_namespace;

  FilePath data_path = webkit_context_->data_path();
  FilePath dir_path;
  if (!data_path.empty())
    dir_path = data_path.AppendASCII("localStorage");
  return StorageNamespace::CreateLocalStorageNamespace(this, dir_path);
}

StorageNamespace* DOMStorageContext::NewSessionStorage() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  return StorageNamespace::CreateSessionStorageNamespace(this);
}

void DOMStorageContext::RegisterStorageArea(StorageArea* storage_area) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  int64 id = storage_area->id();
  DCHECK(!GetStorageArea(id));
  storage_area_map_[id] = storage_area;
}

void DOMStorageContext::UnregisterStorageArea(StorageArea* storage_area) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  int64 id = storage_area->id();
  DCHECK(GetStorageArea(id));
  storage_area_map_.erase(id);
}

StorageArea* DOMStorageContext::GetStorageArea(int64 id) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  StorageAreaMap::iterator iter = storage_area_map_.find(id);
  if (iter == storage_area_map_.end())
    return NULL;
  return iter->second;
}

void DOMStorageContext::RegisterStorageNamespace(
    StorageNamespace* storage_namespace) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  int64 id = storage_namespace->id();
  DCHECK(!GetStorageNamespace(id));
  storage_namespace_map_[id] = storage_namespace;
}

void DOMStorageContext::UnregisterStorageNamespace(
    StorageNamespace* storage_namespace) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  int64 id = storage_namespace->id();
  DCHECK(GetStorageNamespace(id));
  storage_namespace_map_.erase(id);
}

StorageNamespace* DOMStorageContext::GetStorageNamespace(int64 id) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT));
  StorageNamespaceMap::iterator iter = storage_namespace_map_.find(id);
  if (iter == storage_namespace_map_.end())
    return NULL;
  return iter->second;
}

void DOMStorageContext::RegisterDispatcherHost(
    DOMStorageDispatcherHost* dispatcher_host) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
  DCHECK(dispatcher_host_set_.find(dispatcher_host) ==
         dispatcher_host_set_.end());
  dispatcher_host_set_.insert(dispatcher_host);
}

void DOMStorageContext::UnregisterDispatcherHost(
    DOMStorageDispatcherHost* dispatcher_host) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
  DCHECK(dispatcher_host_set_.find(dispatcher_host) !=
         dispatcher_host_set_.end());
  dispatcher_host_set_.erase(dispatcher_host);
}

const DOMStorageContext::DispatcherHostSet*
DOMStorageContext::GetDispatcherHostSet() const {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
  return &dispatcher_host_set_;
}