blob: 7410dbf5e3318a5623bbf62a12bcafbe21d8dcdb [file] [log] [blame]
[email protected]c0b5eb02014-06-02 17:28:101// 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"
avia0935db62017-04-27 19:38:0411#include "base/memory/ptr_util.h"
[email protected]c0b5eb02014-06-02 17:28:1012#include "content/public/browser/blob_handle.h"
13#include "content/public/browser/browser_thread.h"
14#include "content/public/browser/render_process_host.h"
jamescookda2505812015-03-20 18:01:1815#include "extensions/browser/bad_message.h"
[email protected]c0b5eb02014-06-02 17:28:1016
17namespace extensions {
18
19namespace {
20
21// Address to this variable used as the user data key.
22const int kBlobHolderUserDataKey = 0;
23}
24
25// static
26BlobHolder* 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);
avia0935db62017-04-27 19:38:0437 render_process_host->SetUserData(&kBlobHolderUserDataKey,
38 base::WrapUnique(new_instance));
[email protected]c0b5eb02014-06-02 17:28:1039 return new_instance;
40}
41
42BlobHolder::~BlobHolder() {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44}
45
dchengf5d241082016-04-21 03:43:1146void BlobHolder::HoldBlobReference(std::unique_ptr<content::BlobHandle> blob) {
[email protected]c0b5eb02014-06-02 17:28:1047 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
48 DCHECK(!ContainsBlobHandle(blob.get()));
49
[email protected]2153dfcc2014-06-04 16:52:5350 std::string uuid = blob->GetUUID();
lazyboyf33109d2016-08-31 00:37:0851 held_blobs_.insert(make_pair(uuid, std::move(blob)));
[email protected]c0b5eb02014-06-02 17:28:1052}
53
54BlobHolder::BlobHolder(content::RenderProcessHost* render_process_host)
55 : render_process_host_(render_process_host) {
56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
57}
58
59bool BlobHolder::ContainsBlobHandle(content::BlobHandle* handle) const {
60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
jdoerriea1e1598b2018-10-10 09:10:3761 for (auto it = held_blobs_.cbegin(); it != held_blobs_.cend(); ++it) {
[email protected]c0b5eb02014-06-02 17:28:1062 if (it->second.get() == handle)
63 return true;
64 }
65
66 return false;
67}
68
69void BlobHolder::DropBlobs(const std::vector<std::string>& blob_uuids) {
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
jdoerriea1e1598b2018-10-10 09:10:3771 for (auto uuid_it = blob_uuids.cbegin(); uuid_it != blob_uuids.cend();
[email protected]c0b5eb02014-06-02 17:28:1072 ++uuid_it) {
jdoerriea1e1598b2018-10-10 09:10:3773 auto it = held_blobs_.find(*uuid_it);
[email protected]c0b5eb02014-06-02 17:28:1074
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;
jamescookda2505812015-03-20 18:01:1880 bad_message::ReceivedBadMessage(render_process_host_,
81 bad_message::BH_BLOB_NOT_OWNED);
[email protected]c0b5eb02014-06-02 17:28:1082 }
83 }
84}
85
86} // namespace extensions