| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/in_process_webkit/dom_storage_area.h" | 5 #include "chrome/browser/in_process_webkit/dom_storage_area.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/task.h" | 9 #include "base/task.h" |
| 10 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 owner_(owner), | 35 owner_(owner), |
| 36 host_content_settings_map_(host_content_settings_map) { | 36 host_content_settings_map_(host_content_settings_map) { |
| 37 DCHECK(owner_); | 37 DCHECK(owner_); |
| 38 DCHECK(host_content_settings_map_); | 38 DCHECK(host_content_settings_map_); |
| 39 } | 39 } |
| 40 | 40 |
| 41 DOMStorageArea::~DOMStorageArea() { | 41 DOMStorageArea::~DOMStorageArea() { |
| 42 } | 42 } |
| 43 | 43 |
| 44 unsigned DOMStorageArea::Length() { | 44 unsigned DOMStorageArea::Length() { |
| 45 if (!CheckContentSetting()) | |
| 46 return 0; // Pretend we're an empty store. | |
| 47 | |
| 48 CreateWebStorageAreaIfNecessary(); | 45 CreateWebStorageAreaIfNecessary(); |
| 49 return storage_area_->length(); | 46 return storage_area_->length(); |
| 50 } | 47 } |
| 51 | 48 |
| 52 NullableString16 DOMStorageArea::Key(unsigned index) { | 49 NullableString16 DOMStorageArea::Key(unsigned index) { |
| 53 if (!CheckContentSetting()) | |
| 54 return NullableString16(true); // Null string. | |
| 55 | |
| 56 CreateWebStorageAreaIfNecessary(); | 50 CreateWebStorageAreaIfNecessary(); |
| 57 return storage_area_->key(index); | 51 return storage_area_->key(index); |
| 58 } | 52 } |
| 59 | 53 |
| 60 NullableString16 DOMStorageArea::GetItem(const string16& key) { | 54 NullableString16 DOMStorageArea::GetItem(const string16& key) { |
| 61 if (!CheckContentSetting()) | |
| 62 return NullableString16(true); // Null string. | |
| 63 | |
| 64 CreateWebStorageAreaIfNecessary(); | 55 CreateWebStorageAreaIfNecessary(); |
| 65 return storage_area_->getItem(key); | 56 return storage_area_->getItem(key); |
| 66 } | 57 } |
| 67 | 58 |
| 68 NullableString16 DOMStorageArea::SetItem( | 59 NullableString16 DOMStorageArea::SetItem( |
| 69 const string16& key, const string16& value, bool* quota_exception) { | 60 const string16& key, const string16& value, bool* quota_exception) { |
| 70 if (!CheckContentSetting()) { | 61 if (!CheckContentSetting(key, value)) { |
| 71 *quota_exception = true; | 62 *quota_exception = true; |
| 72 return NullableString16(true); // Ignored if exception is true. | 63 return NullableString16(true); // Ignored if exception is true. |
| 73 } | 64 } |
| 74 | 65 |
| 75 CreateWebStorageAreaIfNecessary(); | 66 CreateWebStorageAreaIfNecessary(); |
| 76 WebString old_value; | 67 WebString old_value; |
| 77 storage_area_->setItem(key, value, WebURL(), *quota_exception, old_value); | 68 storage_area_->setItem(key, value, WebURL(), *quota_exception, old_value); |
| 78 return old_value; | 69 return old_value; |
| 79 } | 70 } |
| 80 | 71 |
| 81 NullableString16 DOMStorageArea::RemoveItem(const string16& key) { | 72 NullableString16 DOMStorageArea::RemoveItem(const string16& key) { |
| 82 if (!CheckContentSetting()) | |
| 83 return NullableString16(true); // Indicates nothing removed. | |
| 84 | |
| 85 CreateWebStorageAreaIfNecessary(); | 73 CreateWebStorageAreaIfNecessary(); |
| 86 WebString old_value; | 74 WebString old_value; |
| 87 storage_area_->removeItem(key, WebURL(), old_value); | 75 storage_area_->removeItem(key, WebURL(), old_value); |
| 88 return old_value; | 76 return old_value; |
| 89 } | 77 } |
| 90 | 78 |
| 91 bool DOMStorageArea::Clear() { | 79 bool DOMStorageArea::Clear() { |
| 92 if (!CheckContentSetting()) | |
| 93 return false; // Nothing cleared. | |
| 94 | |
| 95 CreateWebStorageAreaIfNecessary(); | 80 CreateWebStorageAreaIfNecessary(); |
| 96 bool somethingCleared; | 81 bool somethingCleared; |
| 97 storage_area_->clear(WebURL(), somethingCleared); | 82 storage_area_->clear(WebURL(), somethingCleared); |
| 98 return somethingCleared; | 83 return somethingCleared; |
| 99 } | 84 } |
| 100 | 85 |
| 101 void DOMStorageArea::PurgeMemory() { | 86 void DOMStorageArea::PurgeMemory() { |
| 102 storage_area_.reset(); | 87 storage_area_.reset(); |
| 103 } | 88 } |
| 104 | 89 |
| 105 void DOMStorageArea::CreateWebStorageAreaIfNecessary() { | 90 void DOMStorageArea::CreateWebStorageAreaIfNecessary() { |
| 106 if (!storage_area_.get()) | 91 if (!storage_area_.get()) |
| 107 storage_area_.reset(owner_->CreateWebStorageArea(origin_)); | 92 storage_area_.reset(owner_->CreateWebStorageArea(origin_)); |
| 108 } | 93 } |
| 109 | 94 |
| 110 bool DOMStorageArea::CheckContentSetting() { | 95 bool DOMStorageArea::CheckContentSetting(const string16& key, |
| 96 const string16& value) { |
| 111 ContentSetting content_setting = | 97 ContentSetting content_setting = |
| 112 host_content_settings_map_->GetContentSetting( | 98 host_content_settings_map_->GetContentSetting( |
| 113 origin_url_, CONTENT_SETTINGS_TYPE_COOKIES); | 99 origin_url_, CONTENT_SETTINGS_TYPE_COOKIES); |
| 114 | 100 |
| 115 if (content_setting == CONTENT_SETTING_ASK) { | 101 if (content_setting == CONTENT_SETTING_ASK) { |
| 116 WebSecurityOrigin security_origin( | 102 DOMStoragePermissionRequest request(origin_url_, key, value, |
| 117 WebSecurityOrigin::createFromString(origin_)); | |
| 118 FilePath::StringType file_name = webkit_glue::WebStringToFilePath( | |
| 119 security_origin.databaseIdentifier()).value(); | |
| 120 file_name.append(DOMStorageContext::kLocalStorageExtension); | |
| 121 FilePath file_path = webkit_glue::WebStringToFilePath( | |
| 122 owner_->data_dir_path()).Append(file_name); | |
| 123 | |
| 124 bool file_exists = false; | |
| 125 int64 size = 0; | |
| 126 base::Time last_modified; | |
| 127 file_util::FileInfo file_info; | |
| 128 if (file_util::GetFileInfo(file_path, &file_info)) { | |
| 129 file_exists = true; | |
| 130 size = file_info.size; | |
| 131 last_modified = file_info.last_modified; | |
| 132 } | |
| 133 DOMStoragePermissionRequest request(origin_url_, file_exists, size, | |
| 134 last_modified, | |
| 135 host_content_settings_map_); | 103 host_content_settings_map_); |
| 136 ChromeThread::PostTask( | 104 ChromeThread::PostTask( |
| 137 ChromeThread::UI, FROM_HERE, | 105 ChromeThread::UI, FROM_HERE, |
| 138 NewRunnableFunction(&DOMStoragePermissionRequest::PromptUser, | 106 NewRunnableFunction(&DOMStoragePermissionRequest::PromptUser, |
| 139 &request)); | 107 &request)); |
| 140 content_setting = request.WaitOnResponse(); | 108 content_setting = request.WaitOnResponse(); |
| 141 } | 109 } |
| 142 | 110 |
| 143 if (content_setting == CONTENT_SETTING_ALLOW) | 111 if (content_setting == CONTENT_SETTING_ALLOW) |
| 144 return true; | 112 return true; |
| 145 DCHECK(content_setting == CONTENT_SETTING_BLOCK); | 113 DCHECK(content_setting == CONTENT_SETTING_BLOCK); |
| 146 return false; | 114 return false; |
| 147 } | 115 } |
| OLD | NEW |