summaryrefslogtreecommitdiffstats
path: root/webkit/dom_storage/dom_storage_namespace.cc
blob: 343da59045c50a5c757ce178465719ddbc3c9b0e (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
// 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_namespace.h"

#include "base/basictypes.h"
#include "base/logging.h"
#include "webkit/dom_storage/dom_storage_area.h"
#include "webkit/dom_storage/dom_storage_task_runner.h"
#include "webkit/dom_storage/dom_storage_types.h"

namespace dom_storage {

DomStorageNamespace::DomStorageNamespace(
    const FilePath& directory,
    DomStorageTaskRunner* task_runner)
    : namespace_id_(kLocalStorageNamespaceId),
      directory_(directory),
      task_runner_(task_runner) {
}

DomStorageNamespace::DomStorageNamespace(
    int64 namespace_id,
    DomStorageTaskRunner* task_runner)
    : namespace_id_(namespace_id),
      task_runner_(task_runner) {
  DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
}

DomStorageNamespace::~DomStorageNamespace() {
}

DomStorageArea* DomStorageNamespace::OpenStorageArea(const GURL& origin) {
  if (AreaHolder* holder = GetAreaHolder(origin)) {
    ++(holder->open_count_);
    return holder->area_;
  }
  DomStorageArea* area = new DomStorageArea(namespace_id_, origin,
                                            directory_, task_runner_);
  areas_[origin] = AreaHolder(area, 1);
  return area;
}

void DomStorageNamespace::CloseStorageArea(DomStorageArea* area) {
  AreaHolder* holder = GetAreaHolder(area->origin());
  DCHECK(holder);
  DCHECK_EQ(holder->area_.get(), area);
  --(holder->open_count_);
  // TODO(michaeln): Clean up areas that aren't needed in memory anymore.
  // The in-process-webkit based impl didn't do this either, but would be nice.
}

DomStorageNamespace* DomStorageNamespace::Clone(int64 clone_namespace_id) {
  DCHECK_NE(kLocalStorageNamespaceId, namespace_id_);
  DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id);
  DomStorageNamespace* clone =
      new DomStorageNamespace(clone_namespace_id, task_runner_);
  AreaMap::const_iterator it = areas_.begin();
  for (; it != areas_.end(); ++it) {
    DomStorageArea* area = it->second.area_->ShallowCopy(clone_namespace_id);
    clone->areas_[it->first] = AreaHolder(area, 0);
  }
  return clone;
}

DomStorageNamespace::AreaHolder*
DomStorageNamespace::GetAreaHolder(const GURL& origin) {
  AreaMap::iterator found = areas_.find(origin);
  if (found == areas_.end())
    return NULL;
  return &(found->second);
}

// AreaHolder

DomStorageNamespace::AreaHolder::AreaHolder()
    : open_count_(0) {
}

DomStorageNamespace::AreaHolder::AreaHolder(
    DomStorageArea* area, int count)
    : area_(area), open_count_(count) {
}

DomStorageNamespace::AreaHolder::~AreaHolder() {
}

}  // namespace dom_storage