summaryrefslogtreecommitdiffstats
path: root/chrome/browser/in_process_webkit/dom_storage_namespace.cc
blob: 602b24d91bc17251374e7665edebdd091843f53b (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
// Copyright (c) 2010 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_namespace.h"

#include "base/file_path.h"
#include "chrome/browser/in_process_webkit/dom_storage_area.h"
#include "chrome/browser/in_process_webkit/dom_storage_context.h"
#include "chrome/browser/in_process_webkit/dom_storage_dispatcher_host.h"
#include "third_party/WebKit/WebKit/chromium/public/WebStorageArea.h"
#include "third_party/WebKit/WebKit/chromium/public/WebStorageNamespace.h"
#include "webkit/glue/webkit_glue.h"

using WebKit::WebStorageArea;
using WebKit::WebStorageNamespace;
using WebKit::WebString;

/* static */
DOMStorageNamespace* DOMStorageNamespace::CreateLocalStorageNamespace(
    DOMStorageContext* dom_storage_context, const FilePath& data_dir_path) {
  int64 id = kLocalStorageNamespaceId;
  DCHECK(!dom_storage_context->GetStorageNamespace(id, false));
  return new DOMStorageNamespace(dom_storage_context, id,
      webkit_glue::FilePathToWebString(data_dir_path), DOM_STORAGE_LOCAL);
}

/* static */
DOMStorageNamespace* DOMStorageNamespace::CreateSessionStorageNamespace(
    DOMStorageContext* dom_storage_context, int64 id) {
  DCHECK(!dom_storage_context->GetStorageNamespace(id, false));
  return new DOMStorageNamespace(dom_storage_context, id, WebString(),
                                 DOM_STORAGE_SESSION);
}

DOMStorageNamespace::DOMStorageNamespace(DOMStorageContext* dom_storage_context,
                                         int64 id,
                                         const WebString& data_dir_path,
                                         DOMStorageType dom_storage_type)
    : dom_storage_context_(dom_storage_context),
      id_(id),
      data_dir_path_(data_dir_path),
      dom_storage_type_(dom_storage_type) {
  DCHECK(dom_storage_context_);
}

DOMStorageNamespace::~DOMStorageNamespace() {
  // TODO(jorlow): If the DOMStorageContext is being destructed, there's no need
  //               to do these calls.  Maybe we should add a fast path?
  for (OriginToStorageAreaMap::iterator iter(origin_to_storage_area_.begin());
       iter != origin_to_storage_area_.end(); ++iter) {
    dom_storage_context_->UnregisterStorageArea(iter->second);
    delete iter->second;
  }
}

DOMStorageArea* DOMStorageNamespace::GetStorageArea(
    const string16& origin,
    HostContentSettingsMap* host_content_settings_map) {
  // We may have already created it for another dispatcher host.
  OriginToStorageAreaMap::iterator iter = origin_to_storage_area_.find(origin);
  if (iter != origin_to_storage_area_.end())
    return iter->second;

  // We need to create a new one.
  int64 id = dom_storage_context_->AllocateStorageAreaId();
  DCHECK(!dom_storage_context_->GetStorageArea(id));
  DOMStorageArea* storage_area = new DOMStorageArea(origin, id, this,
                                                    host_content_settings_map);
  origin_to_storage_area_[origin] = storage_area;
  dom_storage_context_->RegisterStorageArea(storage_area);
  return storage_area;
}

DOMStorageNamespace* DOMStorageNamespace::Copy(int64 id) {
  DCHECK(dom_storage_type_ == DOM_STORAGE_SESSION);
  DCHECK(!dom_storage_context_->GetStorageNamespace(id, false));
  DOMStorageNamespace* new_storage_namespace = new DOMStorageNamespace(
      dom_storage_context_, id, data_dir_path_, dom_storage_type_);
  // If we haven't used the namespace yet, there's nothing to copy.
  if (storage_namespace_.get())
    new_storage_namespace->storage_namespace_.reset(storage_namespace_->copy());
  return new_storage_namespace;
}

void DOMStorageNamespace::PurgeMemory() {
  DCHECK(dom_storage_type_ == DOM_STORAGE_LOCAL);
  for (OriginToStorageAreaMap::iterator iter(origin_to_storage_area_.begin());
       iter != origin_to_storage_area_.end(); ++iter)
    iter->second->PurgeMemory();
  storage_namespace_.reset();
}

WebStorageArea* DOMStorageNamespace::CreateWebStorageArea(
    const string16& origin) {
  CreateWebStorageNamespaceIfNecessary();
  return storage_namespace_->createStorageArea(origin);
}

void DOMStorageNamespace::CreateWebStorageNamespaceIfNecessary() {
  if (storage_namespace_.get())
    return;

  if (dom_storage_type_ == DOM_STORAGE_LOCAL) {
    storage_namespace_.reset(
        WebStorageNamespace::createLocalStorageNamespace(data_dir_path_,
                                                         kLocalStorageQuota));
  } else {
    storage_namespace_.reset(
        WebStorageNamespace::createSessionStorageNamespace());
  }
}