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/browser/fileapi/chrome_blob_storage_context.h" | 5 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/guid.h" | |
| 8 #include "content/public/browser/browser_context.h" | 9 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "webkit/browser/blob/blob_data_handle.h" | |
| 10 #include "webkit/browser/blob/blob_storage_context.h" | 12 #include "webkit/browser/blob/blob_storage_context.h" |
| 11 | 13 |
| 12 using base::UserDataAdapter; | 14 using base::UserDataAdapter; |
| 13 using webkit_blob::BlobStorageContext; | 15 using webkit_blob::BlobStorageContext; |
| 14 | 16 |
| 15 namespace content { | 17 namespace content { |
| 16 | 18 |
| 19 class ChromeBlobHandle : public BlobHandle { | |
|
michaeln
2014/05/08 23:03:18
The ChromeXXX naming convention is really archaic
tommycli
2014/05/09 16:10:17
Done.
| |
| 20 public: | |
| 21 ChromeBlobHandle(scoped_ptr<webkit_blob::BlobDataHandle> handle) | |
| 22 : handle_(handle.Pass()) { | |
| 23 } | |
| 24 | |
| 25 virtual ~ChromeBlobHandle() {} | |
| 26 | |
| 27 virtual std::string uuid() OVERRIDE { | |
| 28 return handle_->uuid(); | |
| 29 } | |
| 30 | |
| 31 private: | |
| 32 scoped_ptr<webkit_blob::BlobDataHandle> handle_; | |
| 33 }; | |
| 34 | |
| 17 static const char* kBlobStorageContextKeyName = "content_blob_storage_context"; | 35 static const char* kBlobStorageContextKeyName = "content_blob_storage_context"; |
| 18 | 36 |
| 19 ChromeBlobStorageContext::ChromeBlobStorageContext() {} | 37 ChromeBlobStorageContext::ChromeBlobStorageContext() {} |
| 20 | 38 |
| 21 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( | 39 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( |
| 22 BrowserContext* context) { | 40 BrowserContext* context) { |
| 23 if (!context->GetUserData(kBlobStorageContextKeyName)) { | 41 if (!context->GetUserData(kBlobStorageContextKeyName)) { |
| 24 scoped_refptr<ChromeBlobStorageContext> blob = | 42 scoped_refptr<ChromeBlobStorageContext> blob = |
| 25 new ChromeBlobStorageContext(); | 43 new ChromeBlobStorageContext(); |
| 26 context->SetUserData( | 44 context->SetUserData( |
| 27 kBlobStorageContextKeyName, | 45 kBlobStorageContextKeyName, |
| 28 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); | 46 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); |
| 29 // Check first to avoid memory leak in unittests. | 47 // Check first to avoid memory leak in unittests. |
| 30 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | 48 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 31 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
| 32 BrowserThread::IO, FROM_HERE, | 50 BrowserThread::IO, FROM_HERE, |
| 33 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); | 51 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); |
| 34 } | 52 } |
| 35 } | 53 } |
| 36 | 54 |
| 37 return UserDataAdapter<ChromeBlobStorageContext>::Get( | 55 return UserDataAdapter<ChromeBlobStorageContext>::Get( |
| 38 context, kBlobStorageContextKeyName); | 56 context, kBlobStorageContextKeyName); |
| 39 } | 57 } |
| 40 | 58 |
| 41 void ChromeBlobStorageContext::InitializeOnIOThread() { | 59 void ChromeBlobStorageContext::InitializeOnIOThread() { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 43 context_.reset(new BlobStorageContext()); | 61 context_.reset(new BlobStorageContext()); |
| 44 } | 62 } |
| 45 | 63 |
| 64 void ChromeBlobStorageContext::CreateMemoryBackedBlob( | |
| 65 const char* data, size_t length, const BlobCallback& callback) { | |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 67 BrowserThread::PostTaskAndReplyWithResult( | |
| 68 BrowserThread::IO, FROM_HERE, | |
| 69 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlobOnIOThread, | |
| 70 this, data, length, callback), | |
| 71 callback); | |
| 72 } | |
| 73 | |
| 46 ChromeBlobStorageContext::~ChromeBlobStorageContext() {} | 74 ChromeBlobStorageContext::~ChromeBlobStorageContext() {} |
| 47 | 75 |
| 76 scoped_ptr<BlobHandle> | |
| 77 ChromeBlobStorageContext::CreateMemoryBackedBlobOnIOThread( | |
| 78 const char* data, size_t length, const BlobCallback& callback) { | |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 80 | |
| 81 std::string uuid(base::GenerateGUID()); | |
| 82 scoped_refptr<webkit_blob::BlobData> blob_data = | |
| 83 new webkit_blob::BlobData(uuid); | |
| 84 blob_data->AppendData(data, length); | |
| 85 scoped_ptr<BlobHandle> blob_handle( | |
| 86 new ChromeBlobHandle(context_->AddFinishedBlob(blob_data.get()).Pass())); | |
| 87 return blob_handle.Pass(); | |
| 88 } | |
| 89 | |
| 48 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { | 90 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { |
| 49 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && | 91 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && |
| 50 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 92 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 51 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); | 93 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); |
| 52 return; | 94 return; |
| 53 } | 95 } |
| 54 delete this; | 96 delete this; |
| 55 } | 97 } |
| 56 | 98 |
| 57 } // namespace content | 99 } // namespace content |
| OLD | NEW |