OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/renderer_webstoragearea_impl.h" | 5 #include "content/renderer/renderer_webstoragearea_impl.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "content/common/dom_storage_messages.h" | 11 #include "content/common/dom_storage_messages.h" |
12 #include "content/renderer/render_thread_impl.h" | 12 #include "content/renderer/render_thread_impl.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebCommon.h" |
14 #include "webkit/dom_storage/dom_storage_types.h" | 15 #include "webkit/dom_storage/dom_storage_types.h" |
15 | 16 |
16 using WebKit::WebString; | 17 using WebKit::WebString; |
17 using WebKit::WebURL; | 18 using WebKit::WebURL; |
18 | 19 |
19 typedef IDMap<RendererWebStorageAreaImpl> AreaImplMap; | 20 typedef IDMap<RendererWebStorageAreaImpl> AreaImplMap; |
20 | 21 |
21 static base::LazyInstance<AreaImplMap>::Leaky | 22 static base::LazyInstance<AreaImplMap>::Leaky |
22 g_all_areas_map = LAZY_INSTANCE_INITIALIZER; | 23 g_all_areas_map = LAZY_INSTANCE_INITIALIZER; |
23 | 24 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 69 |
69 WebString RendererWebStorageAreaImpl::key(unsigned index) { | 70 WebString RendererWebStorageAreaImpl::key(unsigned index) { |
70 NullableString16 key; | 71 NullableString16 key; |
71 RenderThreadImpl::current()->Send( | 72 RenderThreadImpl::current()->Send( |
72 new DOMStorageHostMsg_Key(connection_id_, index, &key)); | 73 new DOMStorageHostMsg_Key(connection_id_, index, &key)); |
73 return key; | 74 return key; |
74 } | 75 } |
75 | 76 |
76 WebString RendererWebStorageAreaImpl::getItem(const WebString& key) { | 77 WebString RendererWebStorageAreaImpl::getItem(const WebString& key) { |
77 NullableString16 value; | 78 NullableString16 value; |
78 RenderThreadImpl::current()->Send( | 79 unsigned value_len; |
79 new DOMStorageHostMsg_GetItem(connection_id_, key, &value)); | 80 base::SharedMemoryHandle browser_handle = base::SharedMemory::NULLHandle(); |
| 81 dom_storage::IPC_Flag ipc_flag; |
| 82 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_GetItem( |
| 83 connection_id_, key, |
| 84 reinterpret_cast<int*>(&ipc_flag), |
| 85 &browser_handle, &value_len, &value)); |
| 86 if (ipc_flag == dom_storage::SharedMemory) { |
| 87 base::SharedMemory shared_buf(browser_handle, true); |
| 88 shared_buf.Map(value_len*sizeof(char16)); |
| 89 string16 shm_str; |
| 90 shared_buf.Lock(); |
| 91 shm_str.append(static_cast<char16*>(shared_buf.memory()), value_len); |
| 92 shared_buf.Unlock(); |
| 93 shared_buf.Close(); |
| 94 value = NullableString16(shm_str, false); |
| 95 } |
80 return value; | 96 return value; |
81 } | 97 } |
82 | 98 |
83 void RendererWebStorageAreaImpl::setItem( | 99 void RendererWebStorageAreaImpl::setItem( |
84 const WebString& key, const WebString& value, const WebURL& url, | 100 const WebString& key, const WebString& value, const WebURL& url, |
85 WebStorageArea::Result& result, WebString& old_value_webkit) { | 101 WebStorageArea::Result& result, WebString& old_value_webkit) { |
86 if (key.length() + value.length() > dom_storage::kPerAreaQuota) { | 102 size_t value_len = value.length()*sizeof(WebKit::WebUChar); |
| 103 base::SharedMemoryHandle shared_mem_handle; |
| 104 if (key.length() + value_len > dom_storage::kPerAreaQuota) { |
87 result = ResultBlockedByQuota; | 105 result = ResultBlockedByQuota; |
88 return; | 106 return; |
89 } | 107 } |
90 NullableString16 old_value; | 108 NullableString16 old_value; |
91 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItem( | 109 |
92 connection_id_, key, value, url, &result, &old_value)); | 110 if (value_len < dom_storage::kShmEnableThreshold) { |
| 111 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItem( |
| 112 connection_id_, key, value, url, &result, &old_value)); |
| 113 old_value_webkit = old_value; |
| 114 return; |
| 115 } |
| 116 // Try Allocate a shared memory buffer to hold the bulk of data. |
| 117 // if fail, go through channel |
| 118 #if defined(OS_WIN) |
| 119 base::SharedMemory shared_buf; |
| 120 if (!shared_buf.CreateAndMapAnonymous(value_len)) { |
| 121 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItem( |
| 122 connection_id_, key, value, url, &result, &old_value)); |
| 123 old_value_webkit = old_value; |
| 124 return; |
| 125 } |
| 126 #elif defined(OS_POSIX) |
| 127 shared_mem_handle = |
| 128 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(value_len); |
| 129 if (!base::SharedMemory::IsHandleValid(shared_mem_handle)) { |
| 130 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItem( |
| 131 connection_id_, key, value, url, &result, &old_value)); |
| 132 old_value_webkit = old_value; |
| 133 return; |
| 134 } |
| 135 base::SharedMemory shared_buf(shared_mem_handle, false); |
| 136 shared_buf.Map(value_len); |
| 137 #endif |
| 138 // Go through shm, Copy the bits into shared memory. |
| 139 shared_buf.Lock(); |
| 140 memcpy(shared_buf.memory(), value.data(), value_len); |
| 141 shared_buf.Unlock(); |
| 142 // give to process |
| 143 shared_mem_handle = shared_buf.handle(); |
| 144 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItemOpt( |
| 145 connection_id_, key, value.length(), shared_mem_handle, url, |
| 146 &result, &old_value)); |
| 147 shared_buf.Close(); |
93 old_value_webkit = old_value; | 148 old_value_webkit = old_value; |
94 } | 149 } |
95 | 150 |
96 void RendererWebStorageAreaImpl::removeItem( | 151 void RendererWebStorageAreaImpl::removeItem( |
97 const WebString& key, const WebURL& url, WebString& old_value_webkit) { | 152 const WebString& key, const WebURL& url, WebString& old_value_webkit) { |
98 NullableString16 old_value; | 153 NullableString16 old_value; |
99 RenderThreadImpl::current()->Send( | 154 RenderThreadImpl::current()->Send( |
100 new DOMStorageHostMsg_RemoveItem(connection_id_, key, url, &old_value)); | 155 new DOMStorageHostMsg_RemoveItem(connection_id_, key, url, &old_value)); |
101 old_value_webkit = old_value; | 156 old_value_webkit = old_value; |
102 } | 157 } |
103 | 158 |
104 void RendererWebStorageAreaImpl::clear( | 159 void RendererWebStorageAreaImpl::clear( |
105 const WebURL& url, bool& cleared_something) { | 160 const WebURL& url, bool& cleared_something) { |
106 RenderThreadImpl::current()->Send( | 161 RenderThreadImpl::current()->Send( |
107 new DOMStorageHostMsg_Clear(connection_id_, url, &cleared_something)); | 162 new DOMStorageHostMsg_Clear(connection_id_, url, &cleared_something)); |
108 } | 163 } |
OLD | NEW |