Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 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 "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 9 #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" | |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 12 #include "webkit/blob/blob_data.h" | 14 #include "webkit/blob/blob_data.h" |
| 13 #include "webkit/blob/blob_storage_controller.h" | 15 #include "webkit/blob/blob_storage_controller.h" |
| 14 | 16 |
| 15 using WebKit::WebBlobData; | 17 using WebKit::WebBlobData; |
| 16 using WebKit::WebString; | 18 using WebKit::WebString; |
| 17 using WebKit::WebURL; | 19 using WebKit::WebURL; |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 MessageLoop* g_io_thread; | 23 MessageLoop* g_io_thread; |
| 22 webkit_blob::BlobStorageController* g_blob_storage_controller; | 24 webkit_blob::BlobStorageController* g_blob_storage_controller; |
| 23 | 25 |
| 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()); | |
|
John Knottenbelt
2011/07/22 11:35:50
If I am reading the code correctly, spec will stil
| |
| 32 const url_parse::Parsed& parsed(source.parsed()); | |
| 33 const bool is_valid = source.isValid(); | |
| 34 return WebURL(spec, parsed, is_valid); | |
| 35 } | |
| 36 | |
| 24 } // namespace | 37 } // namespace |
| 25 | 38 |
| 26 /* static */ | 39 /* static */ |
| 27 void TestShellWebBlobRegistryImpl::InitializeOnIOThread( | 40 void TestShellWebBlobRegistryImpl::InitializeOnIOThread( |
| 28 webkit_blob::BlobStorageController* blob_storage_controller) { | 41 webkit_blob::BlobStorageController* blob_storage_controller) { |
| 29 g_io_thread = MessageLoop::current(); | 42 g_io_thread = MessageLoop::current(); |
| 30 g_blob_storage_controller = blob_storage_controller; | 43 g_blob_storage_controller = blob_storage_controller; |
| 31 } | 44 } |
| 32 | 45 |
| 33 /* static */ | 46 /* static */ |
| 34 void TestShellWebBlobRegistryImpl::Cleanup() { | 47 void TestShellWebBlobRegistryImpl::Cleanup() { |
| 35 g_io_thread = NULL; | 48 g_io_thread = NULL; |
| 36 g_blob_storage_controller = NULL; | 49 g_blob_storage_controller = NULL; |
| 37 } | 50 } |
| 38 | 51 |
| 39 TestShellWebBlobRegistryImpl::TestShellWebBlobRegistryImpl() { | 52 TestShellWebBlobRegistryImpl::TestShellWebBlobRegistryImpl() { |
| 40 } | 53 } |
| 41 | 54 |
| 42 void TestShellWebBlobRegistryImpl::registerBlobURL( | 55 void TestShellWebBlobRegistryImpl::registerBlobURL( |
| 43 const WebURL& url, WebBlobData& data) { | 56 const WebURL& url, WebBlobData& data) { |
| 44 DCHECK(g_io_thread); | 57 DCHECK(g_io_thread); |
| 45 // Note: BlobData is not refcounted thread safe. | 58 CancelableTask* task; |
| 46 scoped_refptr<webkit_blob::BlobData> blob_data( | 59 { |
| 60 scoped_refptr<webkit_blob::BlobData> blob_data( | |
| 47 new webkit_blob::BlobData(data)); | 61 new webkit_blob::BlobData(data)); |
| 48 g_io_thread->PostTask( | 62 WebURL url_copy = GetWebURLThreadsafeCopy(url); |
| 49 FROM_HERE, | 63 task = |
| 50 NewRunnableMethod( | 64 NewRunnableMethod( |
| 51 this, &TestShellWebBlobRegistryImpl::DoRegisterBlobUrl, url, | 65 this, &TestShellWebBlobRegistryImpl::DoRegisterBlobUrl, url_copy, |
| 52 blob_data)); | 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); | |
| 53 } | 72 } |
| 54 | 73 |
| 55 void TestShellWebBlobRegistryImpl::registerBlobURL( | 74 void TestShellWebBlobRegistryImpl::registerBlobURL( |
| 56 const WebURL& url, const WebURL& src_url) { | 75 const WebURL& url, const WebURL& src_url) { |
| 57 DCHECK(g_io_thread); | 76 DCHECK(g_io_thread); |
| 58 g_io_thread->PostTask( | 77 CancelableTask* task; |
| 59 FROM_HERE, | 78 { |
| 79 WebURL url_copy = GetWebURLThreadsafeCopy(url); | |
| 80 WebURL src_url_copy = GetWebURLThreadsafeCopy(src_url); | |
| 81 task = | |
| 60 NewRunnableMethod(this, | 82 NewRunnableMethod(this, |
| 61 &TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom, | 83 &TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom, |
| 62 url, | 84 url_copy, |
| 63 src_url)); | 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); | |
| 64 } | 91 } |
| 65 | 92 |
| 66 void TestShellWebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 93 void TestShellWebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
| 67 DCHECK(g_io_thread); | 94 DCHECK(g_io_thread); |
| 68 g_io_thread->PostTask( | 95 CancelableTask* task; |
| 69 FROM_HERE, | 96 { |
| 97 WebURL url_copy = GetWebURLThreadsafeCopy(url); | |
| 98 task = | |
| 70 NewRunnableMethod(this, | 99 NewRunnableMethod(this, |
| 71 &TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl, | 100 &TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl, |
| 72 url)); | 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); | |
| 73 } | 107 } |
| 74 | 108 |
| 75 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrl( | 109 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrl( |
| 76 const GURL& url, webkit_blob::BlobData* blob_data) { | 110 const GURL& url, webkit_blob::BlobData* blob_data) { |
| 77 DCHECK(g_blob_storage_controller); | 111 DCHECK(g_blob_storage_controller); |
| 78 g_blob_storage_controller->RegisterBlobUrl(url, blob_data); | 112 g_blob_storage_controller->RegisterBlobUrl(url, blob_data); |
| 79 } | 113 } |
| 80 | 114 |
| 81 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom( | 115 void TestShellWebBlobRegistryImpl::DoRegisterBlobUrlFrom( |
| 82 const GURL& url, const GURL& src_url) { | 116 const GURL& url, const GURL& src_url) { |
| 83 DCHECK(g_blob_storage_controller); | 117 DCHECK(g_blob_storage_controller); |
| 84 g_blob_storage_controller->RegisterBlobUrlFrom(url, src_url); | 118 g_blob_storage_controller->RegisterBlobUrlFrom(url, src_url); |
| 85 } | 119 } |
| 86 | 120 |
| 87 void TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl(const GURL& url) { | 121 void TestShellWebBlobRegistryImpl::DoUnregisterBlobUrl(const GURL& url) { |
| 88 DCHECK(g_blob_storage_controller); | 122 DCHECK(g_blob_storage_controller); |
| 89 g_blob_storage_controller->UnregisterBlobUrl(url); | 123 g_blob_storage_controller->UnregisterBlobUrl(url); |
| 90 } | 124 } |
| OLD | NEW |