summaryrefslogtreecommitdiffstats
path: root/extensions/browser/blob_holder.cc
blob: 552c17ada340241aea15e57f865e3c8003c3d0c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/blob_holder.h"

#include <algorithm>
#include <utility>

#include "base/logging.h"
#include "content/public/browser/blob_handle.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/browser/bad_message.h"

namespace extensions {

namespace {

// Address to this variable used as the user data key.
const int kBlobHolderUserDataKey = 0;
}

// static
BlobHolder* BlobHolder::FromRenderProcessHost(
    content::RenderProcessHost* render_process_host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(render_process_host);
  BlobHolder* existing = static_cast<BlobHolder*>(
      render_process_host->GetUserData(&kBlobHolderUserDataKey));

  if (existing)
    return existing;

  BlobHolder* new_instance = new BlobHolder(render_process_host);
  render_process_host->SetUserData(&kBlobHolderUserDataKey, new_instance);
  return new_instance;
}

BlobHolder::~BlobHolder() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}

void BlobHolder::HoldBlobReference(scoped_ptr<content::BlobHandle> blob) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!ContainsBlobHandle(blob.get()));

  std::string uuid = blob->GetUUID();
  held_blobs_.insert(make_pair(uuid, make_linked_ptr(blob.release())));
}

BlobHolder::BlobHolder(content::RenderProcessHost* render_process_host)
    : render_process_host_(render_process_host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
}

bool BlobHolder::ContainsBlobHandle(content::BlobHandle* handle) const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  for (BlobHandleMultimap::const_iterator it = held_blobs_.begin();
       it != held_blobs_.end();
       ++it) {
    if (it->second.get() == handle)
      return true;
  }

  return false;
}

void BlobHolder::DropBlobs(const std::vector<std::string>& blob_uuids) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  for (std::vector<std::string>::const_iterator uuid_it = blob_uuids.begin();
       uuid_it != blob_uuids.end();
       ++uuid_it) {
    BlobHandleMultimap::iterator it = held_blobs_.find(*uuid_it);

    if (it != held_blobs_.end()) {
      held_blobs_.erase(it);
    } else {
      DLOG(ERROR) << "Tried to release a Blob we don't have ownership to."
                  << "UUID: " << *uuid_it;
      bad_message::ReceivedBadMessage(render_process_host_,
                                      bad_message::BH_BLOB_NOT_OWNED);
    }
  }
}

}  // namespace extensions