[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "extensions/browser/blob_holder.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "base/logging.h" |
avi | a0935db6 | 2017-04-27 19:38:04 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 12 | #include "content/public/browser/blob_handle.h" |
| 13 | #include "content/public/browser/browser_thread.h" |
| 14 | #include "content/public/browser/render_process_host.h" |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 15 | #include "extensions/browser/bad_message.h" |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 16 | |
| 17 | namespace extensions { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Address to this variable used as the user data key. |
| 22 | const int kBlobHolderUserDataKey = 0; |
| 23 | } |
| 24 | |
| 25 | // static |
| 26 | BlobHolder* BlobHolder::FromRenderProcessHost( |
| 27 | content::RenderProcessHost* render_process_host) { |
| 28 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 29 | DCHECK(render_process_host); |
| 30 | BlobHolder* existing = static_cast<BlobHolder*>( |
| 31 | render_process_host->GetUserData(&kBlobHolderUserDataKey)); |
| 32 | |
| 33 | if (existing) |
| 34 | return existing; |
| 35 | |
| 36 | BlobHolder* new_instance = new BlobHolder(render_process_host); |
avi | a0935db6 | 2017-04-27 19:38:04 | [diff] [blame] | 37 | render_process_host->SetUserData(&kBlobHolderUserDataKey, |
| 38 | base::WrapUnique(new_instance)); |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 39 | return new_instance; |
| 40 | } |
| 41 | |
| 42 | BlobHolder::~BlobHolder() { |
| 43 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 44 | } |
| 45 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 46 | void BlobHolder::HoldBlobReference(std::unique_ptr<content::BlobHandle> blob) { |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 47 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 48 | DCHECK(!ContainsBlobHandle(blob.get())); |
| 49 | |
[email protected] | 2153dfcc | 2014-06-04 16:52:53 | [diff] [blame] | 50 | std::string uuid = blob->GetUUID(); |
lazyboy | f33109d | 2016-08-31 00:37:08 | [diff] [blame] | 51 | held_blobs_.insert(make_pair(uuid, std::move(blob))); |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | BlobHolder::BlobHolder(content::RenderProcessHost* render_process_host) |
| 55 | : render_process_host_(render_process_host) { |
| 56 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 57 | } |
| 58 | |
| 59 | bool BlobHolder::ContainsBlobHandle(content::BlobHandle* handle) const { |
| 60 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 61 | for (auto it = held_blobs_.cbegin(); it != held_blobs_.cend(); ++it) { |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 62 | if (it->second.get() == handle) |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | void BlobHolder::DropBlobs(const std::vector<std::string>& blob_uuids) { |
| 70 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 71 | for (auto uuid_it = blob_uuids.cbegin(); uuid_it != blob_uuids.cend(); |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 72 | ++uuid_it) { |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 73 | auto it = held_blobs_.find(*uuid_it); |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 74 | |
| 75 | if (it != held_blobs_.end()) { |
| 76 | held_blobs_.erase(it); |
| 77 | } else { |
| 78 | DLOG(ERROR) << "Tried to release a Blob we don't have ownership to." |
| 79 | << "UUID: " << *uuid_it; |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 80 | bad_message::ReceivedBadMessage(render_process_host_, |
| 81 | bad_message::BH_BLOB_NOT_OWNED); |
[email protected] | c0b5eb0 | 2014-06-02 17:28:10 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } // namespace extensions |