OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ipc/attachment_broker_unprivileged.h" | 5 #include "ipc/attachment_broker_unprivileged.h" |
6 | 6 |
| 7 #include "base/lazy_instance.h" |
7 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
9 #include "ipc/ipc_channel.h" | 10 #include "ipc/ipc_channel.h" |
10 #include "ipc/ipc_endpoint.h" | 11 #include "ipc/ipc_endpoint.h" |
11 | 12 |
12 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
13 #include "ipc/attachment_broker_unprivileged_win.h" | 14 #include "ipc/attachment_broker_unprivileged_win.h" |
14 #endif | 15 #endif |
15 | 16 |
16 #if defined(OS_MACOSX) && !defined(OS_IOS) | 17 #if defined(OS_MACOSX) && !defined(OS_IOS) |
17 #include "ipc/attachment_broker_unprivileged_mac.h" | 18 #include "ipc/attachment_broker_unprivileged_mac.h" |
18 #endif | 19 #endif |
19 | 20 |
20 namespace IPC { | 21 namespace IPC { |
21 | 22 |
22 AttachmentBrokerUnprivileged::AttachmentBrokerUnprivileged() | 23 namespace { |
23 : sender_(nullptr) { | |
24 IPC::AttachmentBroker::SetGlobal(this); | |
25 } | |
26 | 24 |
27 AttachmentBrokerUnprivileged::~AttachmentBrokerUnprivileged() { | 25 // On platforms that support attachment brokering, returns a new instance of |
28 IPC::AttachmentBroker::SetGlobal(nullptr); | 26 // a platform-specific attachment broker. Otherwise returns |nullptr|. |
29 } | 27 // The caller takes ownership of the newly created instance, and is |
30 | 28 // responsible for ensuring that the attachment broker lives longer than |
31 // static | 29 // every IPC::Channel. The new instance automatically registers itself as the |
32 scoped_ptr<AttachmentBrokerUnprivileged> | 30 // global attachment broker. |
33 AttachmentBrokerUnprivileged::CreateBroker() { | 31 scoped_ptr<AttachmentBrokerUnprivileged> CreateBroker() { |
34 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
35 return scoped_ptr<AttachmentBrokerUnprivileged>( | 33 return scoped_ptr<AttachmentBrokerUnprivileged>( |
36 new IPC::AttachmentBrokerUnprivilegedWin); | 34 new IPC::AttachmentBrokerUnprivilegedWin); |
37 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 35 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
38 return scoped_ptr<AttachmentBrokerUnprivileged>( | 36 return scoped_ptr<AttachmentBrokerUnprivileged>( |
39 new IPC::AttachmentBrokerUnprivilegedMac); | 37 new IPC::AttachmentBrokerUnprivilegedMac); |
40 #else | 38 #else |
41 return nullptr; | 39 return nullptr; |
42 #endif | 40 #endif |
43 } | 41 } |
44 | 42 |
45 void AttachmentBrokerUnprivileged::DesignateBrokerCommunicationChannel( | 43 // This class is wrapped in a LazyInstance to ensure that its constructor is |
| 44 // only called once. The constructor creates an attachment broker and sets it as |
| 45 // the global broker. |
| 46 class AttachmentBrokerMakeOnce { |
| 47 public: |
| 48 AttachmentBrokerMakeOnce() { |
| 49 // Single process tests can cause an attachment broker to already exist. |
| 50 if (AttachmentBroker::GetGlobal()) |
| 51 return; |
| 52 attachment_broker_ = CreateBroker(); |
| 53 } |
| 54 |
| 55 private: |
| 56 scoped_ptr<IPC::AttachmentBrokerUnprivileged> attachment_broker_; |
| 57 }; |
| 58 |
| 59 base::LazyInstance<AttachmentBrokerMakeOnce>::Leaky |
| 60 g_attachment_broker_make_once = LAZY_INSTANCE_INITIALIZER; |
| 61 |
| 62 } // namespace |
| 63 |
| 64 AttachmentBrokerUnprivileged::AttachmentBrokerUnprivileged() |
| 65 : sender_(nullptr) { |
| 66 IPC::AttachmentBroker::SetGlobal(this); |
| 67 } |
| 68 |
| 69 AttachmentBrokerUnprivileged::~AttachmentBrokerUnprivileged() { |
| 70 IPC::AttachmentBroker::SetGlobal(nullptr); |
| 71 } |
| 72 |
| 73 // static |
| 74 void AttachmentBrokerUnprivileged::CreateBrokerIfNeeded() { |
| 75 g_attachment_broker_make_once.Get(); |
| 76 } |
| 77 |
| 78 void AttachmentBrokerUnprivileged::RegisterBrokerCommunicationChannel( |
46 Endpoint* endpoint) { | 79 Endpoint* endpoint) { |
47 DCHECK(endpoint); | 80 DCHECK(endpoint); |
48 DCHECK(!sender_); | 81 DCHECK(!sender_); |
49 sender_ = endpoint; | 82 sender_ = endpoint; |
50 endpoint->SetAttachmentBrokerEndpoint(true); | 83 endpoint->SetAttachmentBrokerEndpoint(true); |
51 } | 84 } |
52 | 85 |
| 86 void AttachmentBrokerUnprivileged::DeregisterBrokerCommunicationChannel( |
| 87 Endpoint* endpoint) { |
| 88 DCHECK(endpoint); |
| 89 DCHECK_EQ(endpoint, sender_); |
| 90 sender_ = nullptr; |
| 91 } |
| 92 |
| 93 bool AttachmentBrokerUnprivileged::IsPrivilegedBroker() { |
| 94 return false; |
| 95 } |
| 96 |
53 void AttachmentBrokerUnprivileged::LogError(UMAError error) { | 97 void AttachmentBrokerUnprivileged::LogError(UMAError error) { |
54 UMA_HISTOGRAM_ENUMERATION( | 98 UMA_HISTOGRAM_ENUMERATION( |
55 "IPC.AttachmentBrokerUnprivileged.BrokerAttachmentError", error, | 99 "IPC.AttachmentBrokerUnprivileged.BrokerAttachmentError", error, |
56 ERROR_MAX); | 100 ERROR_MAX); |
57 } | 101 } |
58 | 102 |
59 } // namespace IPC | 103 } // namespace IPC |
OLD | NEW |