| 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/lazy_instance.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "ipc/ipc_endpoint.h" | 12 #include "ipc/ipc_endpoint.h" |
| 13 | 13 |
| 14 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
| 15 #include "ipc/attachment_broker_privileged_win.h" | 15 #include "ipc/attachment_broker_privileged_win.h" |
| 16 #endif | 16 #endif |
| 17 | 17 |
| 18 #if defined(OS_MACOSX) && !defined(OS_IOS) | 18 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 19 #include <mach/mach.h> |
| 20 |
| 21 #include "base/process/port_provider_mac.h" |
| 19 #include "ipc/attachment_broker_privileged_mac.h" | 22 #include "ipc/attachment_broker_privileged_mac.h" |
| 20 #endif | 23 #endif |
| 21 | 24 |
| 22 namespace IPC { | 25 namespace IPC { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 #if defined(OS_MACOSX) && !defined(OS_IOS) | 29 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 30 |
| 31 // A fake port provider that does nothing. Intended for single process unit |
| 32 // tests. |
| 33 class FakePortProvider : public base::PortProvider { |
| 34 mach_port_t TaskForPid(base::ProcessHandle process) const override { |
| 35 DCHECK_EQ(process, getpid()); |
| 36 return mach_task_self(); |
| 37 } |
| 38 }; |
| 39 |
| 40 base::LazyInstance<FakePortProvider>::Leaky |
| 41 g_fake_port_provider = LAZY_INSTANCE_INITIALIZER; |
| 42 |
| 27 // Passed as a constructor parameter to AttachmentBrokerPrivilegedMac. | 43 // Passed as a constructor parameter to AttachmentBrokerPrivilegedMac. |
| 28 base::PortProvider* g_port_provider = nullptr; | 44 base::PortProvider* g_port_provider = nullptr; |
| 29 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 45 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 30 | 46 |
| 31 // On platforms that support attachment brokering, returns a new instance of | 47 // On platforms that support attachment brokering, returns a new instance of |
| 32 // a platform-specific attachment broker. Otherwise returns |nullptr|. | 48 // a platform-specific attachment broker. Otherwise returns |nullptr|. |
| 33 // The caller takes ownership of the newly created instance, and is | 49 // The caller takes ownership of the newly created instance, and is |
| 34 // responsible for ensuring that the attachment broker lives longer than | 50 // responsible for ensuring that the attachment broker lives longer than |
| 35 // every IPC::Channel. The new instance automatically registers itself as the | 51 // every IPC::Channel. The new instance automatically registers itself as the |
| 36 // global attachment broker. | 52 // global attachment broker. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 g_port_provider = provider; | 95 g_port_provider = provider; |
| 80 g_attachment_broker_make_once.Get(); | 96 g_attachment_broker_make_once.Get(); |
| 81 } | 97 } |
| 82 #else | 98 #else |
| 83 // static | 99 // static |
| 84 void AttachmentBrokerPrivileged::CreateBrokerIfNeeded() { | 100 void AttachmentBrokerPrivileged::CreateBrokerIfNeeded() { |
| 85 g_attachment_broker_make_once.Get(); | 101 g_attachment_broker_make_once.Get(); |
| 86 } | 102 } |
| 87 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 103 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 88 | 104 |
| 105 // static |
| 106 void AttachmentBrokerPrivileged::CreateBrokerForSingleProcessTests() { |
| 107 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 108 CreateBrokerIfNeeded(&g_fake_port_provider.Get()); |
| 109 #else |
| 110 CreateBrokerIfNeeded(); |
| 111 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 112 } |
| 113 |
| 89 void AttachmentBrokerPrivileged::RegisterCommunicationChannel( | 114 void AttachmentBrokerPrivileged::RegisterCommunicationChannel( |
| 90 Endpoint* endpoint) { | 115 Endpoint* endpoint) { |
| 91 base::AutoLock auto_lock(*get_lock()); | 116 base::AutoLock auto_lock(*get_lock()); |
| 92 endpoint->SetAttachmentBrokerEndpoint(true); | 117 endpoint->SetAttachmentBrokerEndpoint(true); |
| 93 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint); | 118 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint); |
| 94 DCHECK(endpoints_.end() == it); | 119 DCHECK(endpoints_.end() == it); |
| 95 endpoints_.push_back(endpoint); | 120 endpoints_.push_back(endpoint); |
| 96 } | 121 } |
| 97 | 122 |
| 98 void AttachmentBrokerPrivileged::DeregisterCommunicationChannel( | 123 void AttachmentBrokerPrivileged::DeregisterCommunicationChannel( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 111 return nullptr; | 136 return nullptr; |
| 112 return *it; | 137 return *it; |
| 113 } | 138 } |
| 114 | 139 |
| 115 void AttachmentBrokerPrivileged::LogError(UMAError error) { | 140 void AttachmentBrokerPrivileged::LogError(UMAError error) { |
| 116 UMA_HISTOGRAM_ENUMERATION( | 141 UMA_HISTOGRAM_ENUMERATION( |
| 117 "IPC.AttachmentBrokerPrivileged.BrokerAttachmentError", error, ERROR_MAX); | 142 "IPC.AttachmentBrokerPrivileged.BrokerAttachmentError", error, ERROR_MAX); |
| 118 } | 143 } |
| 119 | 144 |
| 120 } // namespace IPC | 145 } // namespace IPC |
| OLD | NEW |