| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/tools/test_shell/test_shell_webblobregistry_impl.h" | 5 #include "webkit/tools/test_shell/test_shell_webblobregistry_impl.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/task.h" | 8 #include "base/task.h" |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlobData.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlobData.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" | |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 14 #include "webkit/blob/blob_data.h" | 12 #include "webkit/blob/blob_data.h" |
| 15 #include "webkit/blob/blob_storage_controller.h" | 13 #include "webkit/blob/blob_storage_controller.h" |
| 16 | 14 |
| 17 using WebKit::WebBlobData; | 15 using WebKit::WebBlobData; |
| 18 using WebKit::WebString; | |
| 19 using WebKit::WebURL; | 16 using WebKit::WebURL; |
| 17 using webkit_blob::BlobData; |
| 20 | 18 |
| 21 namespace { | 19 namespace { |
| 22 | 20 |
| 23 MessageLoop* g_io_thread; | 21 MessageLoop* g_io_thread; |
| 24 webkit_blob::BlobStorageController* g_blob_storage_controller; | 22 webkit_blob::BlobStorageController* g_blob_storage_controller; |
| 25 | 23 |
| 26 // WebURL contains a WebCString object that is ref-counted, | |
| 27 // but not thread-safe ref-counted. | |
| 28 // "Normal" copying of WebURL results in a copy that is not thread-safe. | |
| 29 // This method creates a deep copy of WebURL. | |
| 30 WebURL GetWebURLThreadsafeCopy(const WebURL& source) { | |
| 31 const WebKit::WebCString spec(source.spec().data(), source.spec().length()); | |
| 32 const url_parse::Parsed& parsed(source.parsed()); | |
| 33 const bool is_valid = source.isValid(); | |
| 34 return WebURL(spec, parsed, is_valid); | |
| 35 } | |
| 36 | |
| 37 } // namespace | 24 } // namespace |
| 38 | 25 |
| 39 /* static */ | 26 /* static */ |
| 40 void TestShellWebBlobRegistryImpl::InitializeOnIOThread( | 27 void TestShellWebBlobRegistryImpl::InitializeOnIOThread( |
| 41 webkit_blob::BlobStorageController* blob_storage_controller) { | 28 webkit_blob::BlobStorageController* blob_storage_controller) { |
| 42 g_io_thread = MessageLoop::current(); | 29 g_io_thread = MessageLoop::current(); |
| 43 g_blob_storage_controller = blob_storage_controller; | 30 g_blob_storage_controller = blob_storage_controller; |
| 44 } | 31 } |
| 45 | 32 |
| 46 /* static */ | 33 /* static */ |
| 47 void TestShellWebBlobRegistryImpl::Cleanup() { | 34 void TestShellWebBlobRegistryImpl::Cleanup() { |
| 48 g_io_thread = NULL; | 35 g_io_thread = NULL; |
| 49 g_blob_storage_controller = NULL; | 36 g_blob_storage_controller = NULL; |
| 50 } | 37 } |
| 51 | 38 |
| 52 TestShellWebBlobRegistryImpl::TestShellWebBlobRegistryImpl() { | 39 TestShellWebBlobRegistryImpl::TestShellWebBlobRegistryImpl() { |
| 53 } | 40 } |
| 54 | 41 |
| 55 void TestShellWebBlobRegistryImpl::registerBlobURL( | 42 void TestShellWebBlobRegistryImpl::registerBlobURL( |
| 56 const WebURL& url, WebBlobData& data) { | 43 const WebURL& url, WebBlobData& data) { |
| 57 DCHECK(g_io_thread); | 44 DCHECK(g_io_thread); |
| 58 CancelableTask* task; | 45 GURL thread_safe_url = url; // WebURL uses refcounted strings. |
| 59 { | 46 scoped_refptr<BlobData> blob_data(new BlobData(data)); |
| 60 scoped_refptr<webkit_blob::BlobData> blob_data( | 47 g_io_thread->PostTask(FROM_HERE, NewRunnableMethod( |
| 61 new webkit_blob::BlobData(data)); | 48 this, &TestShellWebBlobRegistryImpl::DoRegisterBlobUrl, |
| 62 WebURL url_copy = GetWebURLThreadsafeCopy(url); | 49 thread_safe_url, blob_data)); |
| 63 task = | |
| 64 NewRunnableMethod( | |
| 65 this, &TestShellWebBlobRegistryImpl::DoRegisterBlobUrl, url_copy, | |
| 66 blob_data); | |
| 67 // After this block exits, url_copy is disposed, and | |
| 68 // the underlying WebCString will have a refcount=1 and will | |
| 69 // only be accessible from the task object. | |
| 70 } | |
| 71 g_io_thread->PostTask(FROM_HERE, task); | |
| 72 } | 50 } |
| 73 | 51 |
| 74 void TestShellWebBlobRegistryImpl::registerBlobURL( | 52 void TestShellWebBlobRegistryImpl::registerBlobURL( |
| 75 const WebURL& url, const WebURL& src_url) { | 53 const WebURL& url, const WebURL& src_url) { |
| 76 DCHECK(g_io_thread); | 54 DCHECK(g_io_thread); |
| 77 CancelableTask* task; | 55 GURL thread_safe_url = url; |
| 78 { | 56 GURL thread_safe_src_url = src_url; |
| 79 WebURL url_copy = GetWebURLThreadsafeCopy(url); | 57 g_io_thread->PostTask(FROM_HERE, NewRunnableMethod( |
| 80 WebURL src_url_copy = GetWebURLThreadsafeCopy(src_url); | 58 this, &TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom, |
| 81 task = | 59 thread_safe_url, thread_safe_src_url)); |
| 82 NewRunnableMethod(this, | |
| 83 &TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom, | |
| 84 url_copy, | |
| 85 src_url_copy); | |
| 86 // After this block exits, url_copy and src_url_copy are disposed, and | |
| 87 // the underlying WebCStrings will have a refcount=1 and will | |
| 88 // only be accessible from the task object. | |
| 89 } | |
| 90 g_io_thread->PostTask(FROM_HERE, task); | |
| 91 } | 60 } |
| 92 | 61 |
| 93 void TestShellWebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 62 void TestShellWebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
| 94 DCHECK(g_io_thread); | 63 DCHECK(g_io_thread); |
| 95 CancelableTask* task; | 64 GURL thread_safe_url = url; |
| 96 { | 65 g_io_thread->PostTask(FROM_HERE, NewRunnableMethod( |
| 97 WebURL url_copy = GetWebURLThreadsafeCopy(url); | 66 this, &TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl, |
| 98 task = | 67 thread_safe_url)); |
| 99 NewRunnableMethod(this, | |
| 100 &TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl, | |
| 101 url_copy); | |
| 102 // After this block exits, url_copy is disposed, and | |
| 103 // the underlying WebCString will have a refcount=1 and will | |
| 104 // only be accessible from the task object. | |
| 105 } | |
| 106 g_io_thread->PostTask(FROM_HERE, task); | |
| 107 } | 68 } |
| 108 | 69 |
| 109 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrl( | 70 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrl( |
| 110 const GURL& url, webkit_blob::BlobData* blob_data) { | 71 const GURL& url, BlobData* blob_data) { |
| 111 DCHECK(g_blob_storage_controller); | 72 DCHECK(g_blob_storage_controller); |
| 112 g_blob_storage_controller->RegisterBlobUrl(url, blob_data); | 73 g_blob_storage_controller->RegisterUnfinalizedBlobUrl(url); |
| 74 for (std::vector<BlobData::Item>::const_iterator iter = |
| 75 blob_data->items().begin(); |
| 76 iter != blob_data->items().end(); ++iter) { |
| 77 g_blob_storage_controller->AppendBlobDataItem(url, *iter); |
| 78 } |
| 79 g_blob_storage_controller->FinalizeBlob(url, blob_data->content_type()); |
| 113 } | 80 } |
| 114 | 81 |
| 115 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom( | 82 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom( |
| 116 const GURL& url, const GURL& src_url) { | 83 const GURL& url, const GURL& src_url) { |
| 117 DCHECK(g_blob_storage_controller); | 84 DCHECK(g_blob_storage_controller); |
| 118 g_blob_storage_controller->RegisterBlobUrlFrom(url, src_url); | 85 g_blob_storage_controller->RegisterBlobUrlFrom(url, src_url); |
| 119 } | 86 } |
| 120 | 87 |
| 121 void TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl(const GURL& url) { | 88 void TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl(const GURL& url) { |
| 122 DCHECK(g_blob_storage_controller); | 89 DCHECK(g_blob_storage_controller); |
| 123 g_blob_storage_controller->UnregisterBlobUrl(url); | 90 g_blob_storage_controller->UnregisterBlobUrl(url); |
| 124 } | 91 } |
| OLD | NEW |