blob: 892d22746f987297435d7967b3d063a70ced55b8 [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);
61 for (BlobHandleMultimap::const_iterator it = held_blobs_.begin();
62 it != held_blobs_.end();
63 ++it) {
64 if (it->second.get() == handle)
65 return true;
66 }
67
68 return false;
69}
70
71void BlobHolder::DropBlobs(const std::vector<std::string>& blob_uuids) {
72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
73 for (std::vector<std::string>::const_iterator uuid_it = blob_uuids.begin();
74 uuid_it != blob_uuids.end();
75 ++uuid_it) {
76 BlobHandleMultimap::iterator it = held_blobs_.find(*uuid_it);
77
78 if (it != held_blobs_.end()) {
79 held_blobs_.erase(it);
80 } else {
81 DLOG(ERROR) << "Tried to release a Blob we don't have ownership to."
82 << "UUID: " << *uuid_it;
jamescookda2505812015-03-20 18:01:1883 bad_message::ReceivedBadMessage(render_process_host_,
84 bad_message::BH_BLOB_NOT_OWNED);
[email protected]c0b5eb02014-06-02 17:28:1085 }
86 }
87}
88
89} // namespace extensions