Chromium Code Reviews| 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::SharedMemory shared_buf; | |
| 104 base::SharedMemoryHandle shared_mem_handle; | |
| 105 if (key.length() + value_len > dom_storage::kPerAreaQuota) { | |
| 87 result = ResultBlockedByQuota; | 106 result = ResultBlockedByQuota; |
| 88 return; | 107 return; |
| 89 } | 108 } |
| 90 NullableString16 old_value; | 109 NullableString16 old_value; |
| 91 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItem( | 110 |
| 92 connection_id_, key, value, url, &result, &old_value)); | 111 if (value_len < dom_storage::kShmEnableThreshold) { |
| 112 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItem( | |
| 113 connection_id_, key, value, url, &result, &old_value)); | |
| 114 old_value_webkit = old_value; | |
| 115 return; | |
| 116 } | |
| 117 // Try Allocate a shared memory buffer to hold the bulk of data. | |
| 118 // if fail, go through channel | |
| 119 if (!shared_buf.CreateAndMapAnonymous(value_len)) { | |
|
michaeln
2012/05/10 18:42:59
See ChildThread::AllocateSharedMemory. According t
Pan
2012/05/14 06:50:30
Thanks :)
Yes, renderer cannot create a shared mem
| |
| 120 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItem( | |
| 121 connection_id_, key, value, url, &result, &old_value)); | |
| 122 old_value_webkit = old_value; | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 // Go through shm, Copy the bits into shared memory. | |
| 127 shared_buf.Lock(); | |
| 128 memcpy(shared_buf.memory(), value.data(), value_len); | |
| 129 shared_buf.Unlock(); | |
| 130 // give to process | |
| 131 shared_mem_handle = shared_buf.handle(); | |
| 132 RenderThreadImpl::current()->Send(new DOMStorageHostMsg_SetItemOpt( | |
| 133 connection_id_, key, value.length(), shared_mem_handle, url, | |
| 134 &result, &old_value)); | |
| 135 shared_buf.Close(); | |
| 93 old_value_webkit = old_value; | 136 old_value_webkit = old_value; |
| 94 } | 137 } |
| 95 | 138 |
| 96 void RendererWebStorageAreaImpl::removeItem( | 139 void RendererWebStorageAreaImpl::removeItem( |
| 97 const WebString& key, const WebURL& url, WebString& old_value_webkit) { | 140 const WebString& key, const WebURL& url, WebString& old_value_webkit) { |
| 98 NullableString16 old_value; | 141 NullableString16 old_value; |
| 99 RenderThreadImpl::current()->Send( | 142 RenderThreadImpl::current()->Send( |
| 100 new DOMStorageHostMsg_RemoveItem(connection_id_, key, url, &old_value)); | 143 new DOMStorageHostMsg_RemoveItem(connection_id_, key, url, &old_value)); |
| 101 old_value_webkit = old_value; | 144 old_value_webkit = old_value; |
| 102 } | 145 } |
| 103 | 146 |
| 104 void RendererWebStorageAreaImpl::clear( | 147 void RendererWebStorageAreaImpl::clear( |
| 105 const WebURL& url, bool& cleared_something) { | 148 const WebURL& url, bool& cleared_something) { |
| 106 RenderThreadImpl::current()->Send( | 149 RenderThreadImpl::current()->Send( |
| 107 new DOMStorageHostMsg_Clear(connection_id_, url, &cleared_something)); | 150 new DOMStorageHostMsg_Clear(connection_id_, url, &cleared_something)); |
| 108 } | 151 } |
| OLD | NEW |