summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/nacl_desc_wrapper_chrome.cc
blob: eb142993383181d62560d9199d17bc24d12de5ce (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
// Copyright (c) 2011 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 "base/shared_memory.h"
#include "base/sync_socket.h"
#include "native_client/src/trusted/desc/nacl_desc_base.h"
#include "native_client/src/trusted/desc/nacl_desc_sync_socket.h"
#include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
#include "ui/gfx/surface/transport_dib.h"

namespace nacl {

DescWrapper* DescWrapperFactory::ImportPepperSharedMemory(intptr_t shm_int,
                                                          size_t size) {
  base::SharedMemory* shm = reinterpret_cast<base::SharedMemory*>(shm_int);
#if defined(OS_LINUX) || defined(OS_MACOSX)
  return ImportShmHandle(shm->handle().fd, size);
#elif defined(OS_WIN)
  return ImportShmHandle(shm->handle(), size);
#else
# error "What platform?"
#endif
}

DescWrapper* DescWrapperFactory::ImportPepper2DSharedMemory(intptr_t shm_int) {
  TransportDIB* dib = reinterpret_cast<TransportDIB*>(shm_int);
#if defined(OS_LINUX)
  // TransportDIBs use SysV (X) shared memory on Linux.
  return ImportSysvShm(dib->handle(), dib->size());
#elif defined(OS_MACOSX)
  // TransportDIBs use mmap shared memory on OSX.
  return ImportShmHandle(dib->handle().fd, dib->size());
#elif defined(OS_WIN)
  // TransportDIBs use MapViewOfFile shared memory on Windows.
  return ImportShmHandle(dib->handle(), dib->size());
#else
# error "What platform?"
#endif
}

DescWrapper* DescWrapperFactory::ImportPepperSync(intptr_t sync_int) {
  base::SyncSocket* sock = reinterpret_cast<base::SyncSocket*>(sync_int);
  struct NaClDescSyncSocket* ss_desc = NULL;
  DescWrapper* wrapper = NULL;

  ss_desc = static_cast<NaClDescSyncSocket*>(
      calloc(1, sizeof(*ss_desc)));
  if (NULL == ss_desc) {
    // TODO(sehr): Gotos are awful.  Add a scoped_ptr variant that
    // invokes NaClDescSafeUnref.
    goto cleanup;
  }
  if (!NaClDescSyncSocketCtor(ss_desc, sock->handle())) {
    free(ss_desc);
    ss_desc = NULL;
    goto cleanup;
  }
  wrapper = new DescWrapper(common_data_, &ss_desc->base);
  if (NULL == wrapper) {
    goto cleanup;
  }
  ss_desc = NULL;  // DescWrapper takes ownership of ss_desc.
  return wrapper;

 cleanup:
  NaClDescSafeUnref(&ss_desc->base);
  return NULL;
}

}  // namespace nacl