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