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_privileged.h" | 5 #include "ipc/attachment_broker_privileged.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/lazy_instance.h" |
9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.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_privileged_win.h" | 14 #include "ipc/attachment_broker_privileged_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_privileged_mac.h" | 18 #include "ipc/attachment_broker_privileged_mac.h" |
18 #endif | 19 #endif |
19 | 20 |
20 namespace IPC { | 21 namespace IPC { |
21 | 22 |
| 23 namespace { |
| 24 |
| 25 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 26 // Passed as a constructor parameter to AttachmentBrokerPrivilegedMac. |
| 27 base::PortProvider* g_port_provider = nullptr; |
| 28 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 29 |
| 30 // On platforms that support attachment brokering, returns a new instance of |
| 31 // a platform-specific attachment broker. Otherwise returns |nullptr|. |
| 32 // The caller takes ownership of the newly created instance, and is |
| 33 // responsible for ensuring that the attachment broker lives longer than |
| 34 // every IPC::Channel. The new instance automatically registers itself as the |
| 35 // global attachment broker. |
| 36 scoped_ptr<AttachmentBrokerPrivileged> CreateBroker() { |
| 37 #if defined(OS_WIN) |
| 38 return scoped_ptr<AttachmentBrokerPrivileged>( |
| 39 new IPC::AttachmentBrokerPrivilegedWin); |
| 40 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 41 return scoped_ptr<AttachmentBrokerPrivileged>( |
| 42 new IPC::AttachmentBrokerPrivilegedMac(g_port_provider)); |
| 43 #else |
| 44 return nullptr; |
| 45 #endif |
| 46 } |
| 47 |
| 48 // This class is wrapped in a LazyInstance to ensure that its constructor is |
| 49 // only called once. The constructor creates an attachment broker and sets it as |
| 50 // the global broker. |
| 51 class AttachmentBrokerMakeOnce { |
| 52 public: |
| 53 AttachmentBrokerMakeOnce() { |
| 54 attachment_broker_.reset(CreateBroker().release()); |
| 55 } |
| 56 |
| 57 private: |
| 58 scoped_ptr<IPC::AttachmentBrokerPrivileged> attachment_broker_; |
| 59 }; |
| 60 |
| 61 base::LazyInstance<AttachmentBrokerMakeOnce>::Leaky |
| 62 g_attachment_broker_make_once = LAZY_INSTANCE_INITIALIZER; |
| 63 |
| 64 } // namespace |
| 65 |
22 AttachmentBrokerPrivileged::AttachmentBrokerPrivileged() { | 66 AttachmentBrokerPrivileged::AttachmentBrokerPrivileged() { |
23 IPC::AttachmentBroker::SetGlobal(this); | 67 IPC::AttachmentBroker::SetGlobal(this); |
24 } | 68 } |
25 | 69 |
26 AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() { | 70 AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() { |
27 IPC::AttachmentBroker::SetGlobal(nullptr); | 71 IPC::AttachmentBroker::SetGlobal(nullptr); |
28 } | 72 } |
29 | 73 |
| 74 #if defined(OS_MACOSX) && !defined(OS_IOS) |
30 // static | 75 // static |
31 scoped_ptr<AttachmentBrokerPrivileged> | 76 void AttachmentBrokerPrivileged::CreateBrokerIfNeeded( |
32 AttachmentBrokerPrivileged::CreateBroker() { | 77 base::PortProvider* provider) { |
33 #if defined(OS_WIN) | 78 g_port_provider = provider; |
34 return scoped_ptr<AttachmentBrokerPrivileged>( | 79 g_attachment_broker_make_once.Get(); |
35 new IPC::AttachmentBrokerPrivilegedWin); | 80 } |
36 #elif defined(OS_MACOSX) && !defined(OS_IOS) | |
37 return scoped_ptr<AttachmentBrokerPrivileged>( | |
38 new IPC::AttachmentBrokerPrivilegedMac); | |
39 #else | 81 #else |
40 return nullptr; | 82 // static |
41 #endif | 83 void AttachmentBrokerPrivileged::CreateBrokerIfNeeded() { |
| 84 g_attachment_broker_make_once.Get(); |
42 } | 85 } |
| 86 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
43 | 87 |
44 void AttachmentBrokerPrivileged::RegisterCommunicationChannel( | 88 void AttachmentBrokerPrivileged::RegisterCommunicationChannel( |
45 Endpoint* endpoint) { | 89 Endpoint* endpoint) { |
46 endpoint->SetAttachmentBrokerEndpoint(true); | 90 endpoint->SetAttachmentBrokerEndpoint(true); |
47 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint); | 91 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint); |
48 DCHECK(endpoints_.end() == it); | 92 DCHECK(endpoints_.end() == it); |
49 endpoints_.push_back(endpoint); | 93 endpoints_.push_back(endpoint); |
50 } | 94 } |
51 | 95 |
52 void AttachmentBrokerPrivileged::DeregisterCommunicationChannel( | 96 void AttachmentBrokerPrivileged::DeregisterCommunicationChannel( |
(...skipping 10 matching lines...) Expand all Loading... |
63 return nullptr; | 107 return nullptr; |
64 return *it; | 108 return *it; |
65 } | 109 } |
66 | 110 |
67 void AttachmentBrokerPrivileged::LogError(UMAError error) { | 111 void AttachmentBrokerPrivileged::LogError(UMAError error) { |
68 UMA_HISTOGRAM_ENUMERATION( | 112 UMA_HISTOGRAM_ENUMERATION( |
69 "IPC.AttachmentBrokerPrivileged.BrokerAttachmentError", error, ERROR_MAX); | 113 "IPC.AttachmentBrokerPrivileged.BrokerAttachmentError", error, ERROR_MAX); |
70 } | 114 } |
71 | 115 |
72 } // namespace IPC | 116 } // namespace IPC |
OLD | NEW |