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