|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #include <limits> |
|
6 #include <windows.h> |
|
7 |
|
8 #include "base/logging.h" |
|
9 #include "base/sys_info.h" |
|
10 #include "chrome/common/transport_dib.h" |
|
11 |
|
12 TransportDIB::TransportDIB() { |
|
13 } |
|
14 |
|
15 TransportDIB::~TransportDIB() { |
|
16 } |
|
17 |
|
18 TransportDIB::TransportDIB(HANDLE handle) |
|
19 : shared_memory_(handle, false /* read write */) { |
|
20 } |
|
21 |
|
22 // static |
|
23 TransportDIB* TransportDIB::Create(size_t size, uint32_t sequence_num) { |
|
24 size_t allocation_granularity = base::SysInfo::VMAllocationGranularity(); |
|
25 size = size / allocation_granularity + 1; |
|
26 size = size * allocation_granularity; |
|
27 |
|
28 TransportDIB* dib = new TransportDIB; |
|
29 |
|
30 if (!dib->shared_memory_.Create("", false /* read write */, |
|
31 true /* open existing */, size)) { |
|
32 delete dib; |
|
33 return NULL; |
|
34 } |
|
35 |
|
36 dib->size_ = size; |
|
37 dib->sequence_num_ = sequence_num; |
|
38 |
|
39 return dib; |
|
40 } |
|
41 |
|
42 // static |
|
43 TransportDIB* TransportDIB::Map(TransportDIB::Handle handle) { |
|
44 TransportDIB* dib = new TransportDIB(handle); |
|
45 if (!dib->shared_memory_.Map(0 /* map whole shared memory segment */)) { |
|
46 CHROMIUM_LOG(ERROR) << "Failed to map transport DIB" |
|
47 << " handle:" << handle |
|
48 << " error:" << GetLastError(); |
|
49 delete dib; |
|
50 return NULL; |
|
51 } |
|
52 |
|
53 // There doesn't seem to be any way to find the size of the shared memory |
|
54 // region! GetFileSize indicates that the handle is invalid. Thus, we |
|
55 // conservatively set the size to the maximum and hope that the renderer |
|
56 // isn't about to ask us to read off the end of the array. |
|
57 dib->size_ = std::numeric_limits<size_t>::max(); |
|
58 |
|
59 return dib; |
|
60 } |
|
61 |
|
62 void* TransportDIB::memory() const { |
|
63 return shared_memory_.memory(); |
|
64 } |
|
65 |
|
66 TransportDIB::Handle TransportDIB::handle() const { |
|
67 return shared_memory_.handle(); |
|
68 } |
|
69 |
|
70 TransportDIB::Id TransportDIB::id() const { |
|
71 return Id(shared_memory_.handle(), sequence_num_); |
|
72 } |