ipc/chromium/src/chrome/common/transport_dib_win.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/chrome/common/transport_dib_win.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include <limits>
     1.9 +#include <windows.h>
    1.10 +
    1.11 +#include "base/logging.h"
    1.12 +#include "base/sys_info.h"
    1.13 +#include "chrome/common/transport_dib.h"
    1.14 +
    1.15 +TransportDIB::TransportDIB() {
    1.16 +}
    1.17 +
    1.18 +TransportDIB::~TransportDIB() {
    1.19 +}
    1.20 +
    1.21 +TransportDIB::TransportDIB(HANDLE handle)
    1.22 +    : shared_memory_(handle, false /* read write */) {
    1.23 +}
    1.24 +
    1.25 +// static
    1.26 +TransportDIB* TransportDIB::Create(size_t size, uint32_t sequence_num) {
    1.27 +  size_t allocation_granularity = base::SysInfo::VMAllocationGranularity();
    1.28 +  size = size / allocation_granularity + 1;
    1.29 +  size = size * allocation_granularity;
    1.30 +
    1.31 +  TransportDIB* dib = new TransportDIB;
    1.32 +
    1.33 +  if (!dib->shared_memory_.Create("", false /* read write */,
    1.34 +                                  true /* open existing */, size)) {
    1.35 +    delete dib;
    1.36 +    return NULL;
    1.37 +  }
    1.38 +
    1.39 +  dib->size_ = size;
    1.40 +  dib->sequence_num_ = sequence_num;
    1.41 +
    1.42 +  return dib;
    1.43 +}
    1.44 +
    1.45 +// static
    1.46 +TransportDIB* TransportDIB::Map(TransportDIB::Handle handle) {
    1.47 +  TransportDIB* dib = new TransportDIB(handle);
    1.48 +  if (!dib->shared_memory_.Map(0 /* map whole shared memory segment */)) {
    1.49 +    CHROMIUM_LOG(ERROR) << "Failed to map transport DIB"
    1.50 +                        << " handle:" << handle
    1.51 +                        << " error:" << GetLastError();
    1.52 +    delete dib;
    1.53 +    return NULL;
    1.54 +  }
    1.55 +
    1.56 +  // There doesn't seem to be any way to find the size of the shared memory
    1.57 +  // region! GetFileSize indicates that the handle is invalid. Thus, we
    1.58 +  // conservatively set the size to the maximum and hope that the renderer
    1.59 +  // isn't about to ask us to read off the end of the array.
    1.60 +  dib->size_ = std::numeric_limits<size_t>::max();
    1.61 +
    1.62 +  return dib;
    1.63 +}
    1.64 +
    1.65 +void* TransportDIB::memory() const {
    1.66 +  return shared_memory_.memory();
    1.67 +}
    1.68 +
    1.69 +TransportDIB::Handle TransportDIB::handle() const {
    1.70 +  return shared_memory_.handle();
    1.71 +}
    1.72 +
    1.73 +TransportDIB::Id TransportDIB::id() const {
    1.74 +  return Id(shared_memory_.handle(), sequence_num_);
    1.75 +}

mercurial