File: plugin_array_buffer_var.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (107 lines) | stat: -rw-r--r-- 3,156 bytes parent folder | download | duplicates (2)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2012 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 "ppapi/proxy/plugin_array_buffer_var.h"

#include <stdlib.h>

#include <limits>

#include "base/memory/shared_memory.h"
#include "ppapi/c/dev/ppb_buffer_dev.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/serialized_structs.h"
#include "ppapi/shared_impl/host_resource.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_buffer_api.h"

using base::SharedMemory;
using base::SharedMemoryHandle;
using ppapi::proxy::PluginGlobals;
using ppapi::proxy::PluginResourceTracker;

namespace ppapi {

PluginArrayBufferVar::PluginArrayBufferVar(uint32_t size_in_bytes)
    : buffer_(size_in_bytes),
      plugin_handle_(base::SharedMemory::NULLHandle()),
      size_in_bytes_(size_in_bytes) {}

PluginArrayBufferVar::PluginArrayBufferVar(uint32_t size_in_bytes,
                                           SharedMemoryHandle plugin_handle)
    : plugin_handle_(plugin_handle), size_in_bytes_(size_in_bytes) {}

PluginArrayBufferVar::~PluginArrayBufferVar() {
  Unmap();

  if (shmem_.get() == NULL) {
    // The SharedMemory destuctor can't close the handle for us.
    if (SharedMemory::IsHandleValid(plugin_handle_))
      SharedMemory::CloseHandle(plugin_handle_);
  } else {
    // Delete SharedMemory, if we have one.
    shmem_.reset();
  }
}

void* PluginArrayBufferVar::Map() {
  if (shmem_.get())
    return shmem_->memory();
  if (SharedMemory::IsHandleValid(plugin_handle_)) {
    shmem_.reset(new SharedMemory(plugin_handle_, false));
    if (!shmem_->Map(size_in_bytes_)) {
      shmem_.reset();
      return NULL;
    }
    return shmem_->memory();
  }
  if (buffer_.empty())
    return NULL;
  return &(buffer_[0]);
}

void PluginArrayBufferVar::Unmap() {
  if (shmem_.get())
    shmem_->Unmap();
}

uint32_t PluginArrayBufferVar::ByteLength() {
  return size_in_bytes_;
}

bool PluginArrayBufferVar::CopyToNewShmem(
    PP_Instance instance,
    int* host_handle_id,
    SharedMemoryHandle* plugin_out_handle) {
  ppapi::proxy::PluginDispatcher* dispatcher =
      ppapi::proxy::PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return false;

  ppapi::proxy::SerializedHandle plugin_handle;
  dispatcher->Send(new PpapiHostMsg_SharedMemory_CreateSharedMemory(
      instance, ByteLength(), host_handle_id, &plugin_handle));
  if (!plugin_handle.IsHandleValid() || !plugin_handle.is_shmem() ||
      *host_handle_id == -1)
    return false;

  base::SharedMemoryHandle tmp_handle = plugin_handle.shmem();
  SharedMemory s(tmp_handle, false);
  if (!s.Map(ByteLength()))
    return false;
  memcpy(s.memory(), Map(), ByteLength());
  s.Unmap();

  // We don't need to keep the shared memory around on the plugin side;
  // we've already copied all our data into it. We'll make it invalid
  // just to be safe.
  *plugin_out_handle = base::SharedMemory::NULLHandle();

  return true;
}

}  // namespace ppapi