OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <sys/ipc.h> | 7 #include <sys/ipc.h> |
8 #include <sys/shm.h> | 8 #include <sys/shm.h> |
9 | 9 |
| 10 #include "base/gfx/size.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "chrome/common/transport_dib.h" | 12 #include "chrome/common/transport_dib.h" |
| 13 #include "chrome/common/x11_util.h" |
12 | 14 |
13 // The shmat system call uses this as it's invalid return address | 15 // The shmat system call uses this as it's invalid return address |
14 static void *const kInvalidAddress = (void*) -1; | 16 static void *const kInvalidAddress = (void*) -1; |
15 | 17 |
16 TransportDIB::TransportDIB() | 18 TransportDIB::TransportDIB() |
17 : key_(-1), | 19 : key_(-1), |
18 address_(kInvalidAddress), | 20 address_(kInvalidAddress), |
| 21 x_shm_(0), |
| 22 display_(NULL), |
19 size_(0) { | 23 size_(0) { |
20 } | 24 } |
21 | 25 |
22 TransportDIB::~TransportDIB() { | 26 TransportDIB::~TransportDIB() { |
23 if (address_ != kInvalidAddress) { | 27 if (address_ != kInvalidAddress) { |
24 shmdt(address_); | 28 shmdt(address_); |
25 address_ = kInvalidAddress; | 29 address_ = kInvalidAddress; |
26 } | 30 } |
| 31 |
| 32 if (x_shm_) { |
| 33 DCHECK(display_); |
| 34 x11_util::DetachSharedMemory(display_, x_shm_); |
| 35 } |
27 } | 36 } |
28 | 37 |
29 // static | 38 // static |
30 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { | 39 TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) { |
31 // We use a mode of 0666 since the X server won't attach to memory which is | 40 // We use a mode of 0666 since the X server won't attach to memory which is |
32 // 0600 since it can't know if it (as a root process) is being asked to map | 41 // 0600 since it can't know if it (as a root process) is being asked to map |
33 // someone else's private shared memory region. | 42 // someone else's private shared memory region. |
34 const int shmkey = shmget(IPC_PRIVATE, size, 0666); | 43 const int shmkey = shmget(IPC_PRIVATE, size, 0666); |
35 if (shmkey == -1) { | 44 if (shmkey == -1) { |
36 DLOG(ERROR) << "Failed to create SysV shared memory region" | 45 DLOG(ERROR) << "Failed to create SysV shared memory region" |
(...skipping 15 matching lines...) Expand all Loading... |
52 dib->address_ = address; | 61 dib->address_ = address; |
53 dib->size_ = size; | 62 dib->size_ = size; |
54 return dib; | 63 return dib; |
55 } | 64 } |
56 | 65 |
57 TransportDIB* TransportDIB::Map(Handle shmkey) { | 66 TransportDIB* TransportDIB::Map(Handle shmkey) { |
58 struct shmid_ds shmst; | 67 struct shmid_ds shmst; |
59 if (shmctl(shmkey, IPC_STAT, &shmst) == -1) | 68 if (shmctl(shmkey, IPC_STAT, &shmst) == -1) |
60 return NULL; | 69 return NULL; |
61 | 70 |
62 void* address = shmat(shmkey, NULL /* desired address */, 0 /* flags */); | 71 void* address = shmat(shmkey, NULL /* desired address */, SHM_RDONLY); |
63 if (address == kInvalidAddress) | 72 if (address == kInvalidAddress) |
64 return NULL; | 73 return NULL; |
65 | 74 |
66 TransportDIB* dib = new TransportDIB; | 75 TransportDIB* dib = new TransportDIB; |
67 | 76 |
68 dib->address_ = address; | 77 dib->address_ = address; |
69 dib->size_ = shmst.shm_segsz; | 78 dib->size_ = shmst.shm_segsz; |
70 dib->key_ = shmkey; | 79 dib->key_ = shmkey; |
71 return dib; | 80 return dib; |
72 } | 81 } |
73 | 82 |
74 void* TransportDIB::memory() const { | 83 void* TransportDIB::memory() const { |
75 DCHECK_NE(address_, kInvalidAddress); | 84 DCHECK_NE(address_, kInvalidAddress); |
76 return address_; | 85 return address_; |
77 } | 86 } |
78 | 87 |
79 TransportDIB::Id TransportDIB::id() const { | 88 TransportDIB::Id TransportDIB::id() const { |
80 return key_; | 89 return key_; |
81 } | 90 } |
82 | 91 |
83 TransportDIB::Handle TransportDIB::handle() const { | 92 TransportDIB::Handle TransportDIB::handle() const { |
84 return key_; | 93 return key_; |
85 } | 94 } |
| 95 |
| 96 XID TransportDIB::MapToX(Display* display) { |
| 97 if (!x_shm_) { |
| 98 x_shm_ = x11_util::AttachSharedMemory(display, key_); |
| 99 display_ = display; |
| 100 } |
| 101 |
| 102 return x_shm_; |
| 103 } |
OLD | NEW |