Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / extensions / browser / blob_holder.cc
blob552c17ada340241aea15e57f865e3c8003c3d0c1
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"
7 #include <algorithm>
8 #include <utility>
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 {
18 namespace {
20 // Address to this variable used as the user data key.
21 const int kBlobHolderUserDataKey = 0;
24 // static
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));
32 if (existing)
33 return existing;
35 BlobHolder* new_instance = new BlobHolder(render_process_host);
36 render_process_host->SetUserData(&kBlobHolderUserDataKey, new_instance);
37 return 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();
61 ++it) {
62 if (it->second.get() == handle)
63 return true;
66 return false;
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();
73 ++uuid_it) {
74 BlobHandleMultimap::iterator it = held_blobs_.find(*uuid_it);
76 if (it != held_blobs_.end()) {
77 held_blobs_.erase(it);
78 } else {
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