PPAPI: Upgrade video decoder host shared memory API.

Use an UnsafeSharedMemoryRegion instead of the existing handle. The
shared memory on the host side is used read-only, but because a
writable region needs to be shipped to the other side of the proxy, an
unsafe region needs to be used.

Host                         | Other side of proxy
-----------------------------+---------------------------------
                             |   Request SHM
 Create SHM <-----------------------/
 Reply with SHM Handle       |
   | \-------------------------> Receive SHM
 Save SHM by ID              |
                             |
                             |   Fill SHM with video to decode
                             |       (write to SHM)
                             |   Send SHM ID to Host
 Receive decode request <-----------/
 Look up buffer by ID        |
 Decode what's in the SHM    |
  (read-only SHM access)

The host-side could use a read-only region only by adding an additional
round-trip, with the other side of the proxy either converting to
read-only after mapping writable, and shipping back to the host,
or the other side of the proxy mapping, shipping back a writable
handle, and then the host converting to read-only. This has not been
done in this CL.

Bug: 849207
Change-Id: I3e50f9ff9c65e51c21c7e4d72b3aed2402c03196
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1615021
Commit-Queue: Matthew Cary (CET) <[email protected]>
Reviewed-by: Chris Palmer <[email protected]>
Reviewed-by: Antoine Labour <[email protected]>
Reviewed-by: Bill Budge <[email protected]>
Cr-Commit-Position: refs/heads/master@{#666194}
diff --git a/ppapi/proxy/video_decoder_resource.cc b/ppapi/proxy/video_decoder_resource.cc
index 94548e14..ad00255 100644
--- a/ppapi/proxy/video_decoder_resource.cc
+++ b/ppapi/proxy/video_decoder_resource.cc
@@ -32,12 +32,12 @@
 namespace proxy {
 
 VideoDecoderResource::ShmBuffer::ShmBuffer(
-    std::unique_ptr<base::SharedMemory> shm_ptr,
-    uint32_t size,
+    base::UnsafeSharedMemoryRegion region,
     uint32_t shm_id)
-    : shm(std::move(shm_ptr)), addr(NULL), shm_id(shm_id) {
-  if (shm->Map(size))
-    addr = shm->memory();
+    : region(std::move(region)), shm_id(shm_id) {
+  mapping = this->region.Map();
+  if (mapping.IsValid())
+    addr = mapping.memory();
 }
 
 VideoDecoderResource::ShmBuffer::~ShmBuffer() {
@@ -199,7 +199,7 @@
   decode_ids_[uid % kMaximumPictureDelay] = decode_id;
 
   if (available_shm_buffers_.empty() ||
-      available_shm_buffers_.back()->shm->mapped_size() < size) {
+      available_shm_buffers_.back()->mapping.size() < size) {
     uint32_t shm_id;
     if (shm_buffers_.size() < kMaximumPendingDecodes) {
       // Signal the host to create a new shm buffer by passing an index outside
@@ -227,13 +227,12 @@
     if (!UnpackMessage<PpapiPluginMsg_VideoDecoder_GetShmReply>(reply,
                                                                 &shm_size))
       return PP_ERROR_FAILED;
-    base::SharedMemoryHandle shm_handle;
-    if (!reply_params.TakeSharedMemoryHandleAtIndex(0, &shm_handle))
+    base::UnsafeSharedMemoryRegion shm_region;
+    if (!reply_params.TakeUnsafeSharedMemoryRegionAtIndex(0, &shm_region) ||
+        !shm_region.IsValid() || shm_region.GetSize() != shm_size)
       return PP_ERROR_NOMEMORY;
-    std::unique_ptr<base::SharedMemory> shm(
-        new base::SharedMemory(shm_handle, false /* read_only */));
     std::unique_ptr<ShmBuffer> shm_buffer(
-        new ShmBuffer(std::move(shm), shm_size, shm_id));
+        new ShmBuffer(std::move(shm_region), shm_id));
     if (!shm_buffer->addr)
       return PP_ERROR_NOMEMORY;
 
@@ -246,7 +245,7 @@
 
   // At this point we should have shared memory to hold the plugin's buffer.
   DCHECK(!available_shm_buffers_.empty() &&
-         available_shm_buffers_.back()->shm->mapped_size() >= size);
+         available_shm_buffers_.back()->mapping.size() >= size);
 
   ShmBuffer* shm_buffer = available_shm_buffers_.back();
   available_shm_buffers_.pop_back();