1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/blob_holder.h"
10 #include "base/logging.h"
11 #include "content/public/browser/blob_handle.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "extensions/browser/bad_message.h"
16 namespace extensions
{
20 // Address to this variable used as the user data key.
21 const int kBlobHolderUserDataKey
= 0;
25 BlobHolder
* BlobHolder::FromRenderProcessHost(
26 content::RenderProcessHost
* render_process_host
) {
27 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
28 DCHECK(render_process_host
);
29 BlobHolder
* existing
= static_cast<BlobHolder
*>(
30 render_process_host
->GetUserData(&kBlobHolderUserDataKey
));
35 BlobHolder
* new_instance
= new BlobHolder(render_process_host
);
36 render_process_host
->SetUserData(&kBlobHolderUserDataKey
, new_instance
);
40 BlobHolder::~BlobHolder() {
41 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
44 void BlobHolder::HoldBlobReference(scoped_ptr
<content::BlobHandle
> blob
) {
45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
46 DCHECK(!ContainsBlobHandle(blob
.get()));
48 std::string uuid
= blob
->GetUUID();
49 held_blobs_
.insert(make_pair(uuid
, make_linked_ptr(blob
.release())));
52 BlobHolder::BlobHolder(content::RenderProcessHost
* render_process_host
)
53 : render_process_host_(render_process_host
) {
54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
57 bool BlobHolder::ContainsBlobHandle(content::BlobHandle
* handle
) const {
58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
59 for (BlobHandleMultimap::const_iterator it
= held_blobs_
.begin();
60 it
!= held_blobs_
.end();
62 if (it
->second
.get() == handle
)
69 void BlobHolder::DropBlobs(const std::vector
<std::string
>& blob_uuids
) {
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
71 for (std::vector
<std::string
>::const_iterator uuid_it
= blob_uuids
.begin();
72 uuid_it
!= blob_uuids
.end();
74 BlobHandleMultimap::iterator it
= held_blobs_
.find(*uuid_it
);
76 if (it
!= held_blobs_
.end()) {
77 held_blobs_
.erase(it
);
79 DLOG(ERROR
) << "Tried to release a Blob we don't have ownership to."
80 << "UUID: " << *uuid_it
;
81 bad_message::ReceivedBadMessage(render_process_host_
,
82 bad_message::BH_BLOB_NOT_OWNED
);
87 } // namespace extensions