summaryrefslogtreecommitdiffstats
path: root/chrome/browser/in_process_webkit/dom_storage_area.cc
blob: fc9bf80d13889f6cb382ae9475328f3725ce0202 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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_area.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/task.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/in_process_webkit/dom_storage_context.h"
#include "chrome/browser/in_process_webkit/dom_storage_dispatcher_host.h"
#include "chrome/browser/in_process_webkit/dom_storage_namespace.h"
#include "chrome/browser/in_process_webkit/dom_storage_permission_request.h"
#include "chrome/browser/host_content_settings_map.h"
#include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h"
#include "third_party/WebKit/WebKit/chromium/public/WebStorageArea.h"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
#include "third_party/WebKit/WebKit/chromium/public/WebURL.h"
#include "webkit/glue/webkit_glue.h"

using WebKit::WebSecurityOrigin;
using WebKit::WebStorageArea;
using WebKit::WebString;
using WebKit::WebURL;

DOMStorageArea::DOMStorageArea(
    const string16& origin,
    int64 id,
    DOMStorageNamespace* owner,
    HostContentSettingsMap* host_content_settings_map)
    : origin_(origin),
      origin_url_(origin),
      id_(id),
      owner_(owner),
      host_content_settings_map_(host_content_settings_map) {
  DCHECK(owner_);
  DCHECK(host_content_settings_map_);
}

DOMStorageArea::~DOMStorageArea() {
}

unsigned DOMStorageArea::Length() {
  if (!CheckContentSetting())
    return 0;  // Pretend we're an empty store.

  CreateWebStorageAreaIfNecessary();
  return storage_area_->length();
}

NullableString16 DOMStorageArea::Key(unsigned index) {
  if (!CheckContentSetting())
    return NullableString16(true);  // Null string.

  CreateWebStorageAreaIfNecessary();
  return storage_area_->key(index);
}

NullableString16 DOMStorageArea::GetItem(const string16& key) {
  if (!CheckContentSetting())
    return NullableString16(true);  // Null string.

  CreateWebStorageAreaIfNecessary();
  return storage_area_->getItem(key);
}

NullableString16 DOMStorageArea::SetItem(
    const string16& key, const string16& value, bool* quota_exception) {
  if (!CheckContentSetting()) {
    *quota_exception = true;
    return NullableString16(true);  // Ignored if exception is true.
  }

  CreateWebStorageAreaIfNecessary();
  WebString old_value;
  storage_area_->setItem(key, value, WebURL(), *quota_exception, old_value);
  return old_value;
}

NullableString16 DOMStorageArea::RemoveItem(const string16& key) {
  if (!CheckContentSetting())
    return NullableString16(true);  // Indicates nothing removed.

  CreateWebStorageAreaIfNecessary();
  WebString old_value;
  storage_area_->removeItem(key, WebURL(), old_value);
  return old_value;
}

bool DOMStorageArea::Clear() {
  if (!CheckContentSetting())
    return false;  // Nothing cleared.

  CreateWebStorageAreaIfNecessary();
  bool somethingCleared;
  storage_area_->clear(WebURL(), somethingCleared);
  return somethingCleared;
}

void DOMStorageArea::PurgeMemory() {
  storage_area_.reset();
}

void DOMStorageArea::CreateWebStorageAreaIfNecessary() {
  if (!storage_area_.get())
    storage_area_.reset(owner_->CreateWebStorageArea(origin_));
}

bool DOMStorageArea::CheckContentSetting() {
  ContentSetting content_setting =
      host_content_settings_map_->GetContentSetting(
          origin_url_, CONTENT_SETTINGS_TYPE_COOKIES);

  if (content_setting == CONTENT_SETTING_ASK) {
    WebSecurityOrigin security_origin(
        WebSecurityOrigin::createFromString(origin_));
    FilePath::StringType file_name = webkit_glue::WebStringToFilePath(
        security_origin.databaseIdentifier()).value();
    file_name.append(DOMStorageContext::kLocalStorageExtension);
    FilePath file_path = webkit_glue::WebStringToFilePath(
        owner_->data_dir_path()).Append(file_name);

    bool file_exists = false;
    int64 size = 0;
    base::Time last_modified;
    file_util::FileInfo file_info;
    if (file_util::GetFileInfo(file_path, &file_info)) {
      file_exists = true;
      size = file_info.size;
      last_modified = file_info.last_modified;
    }
    DOMStoragePermissionRequest request(origin_url_, file_exists, size,
                                        last_modified,
                                        host_content_settings_map_);
    ChromeThread::PostTask(
        ChromeThread::UI, FROM_HERE,
        NewRunnableFunction(&DOMStoragePermissionRequest::PromptUser,
                            &request));
    content_setting = request.WaitOnResponse();
  }

  if (content_setting == CONTENT_SETTING_ALLOW)
    return true;
  DCHECK(content_setting == CONTENT_SETTING_BLOCK);
  return false;
}