| 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" | |
| 8 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 9 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 10 #include "ipc/ipc_channel.h" | 9 #include "ipc/ipc_channel.h" |
| 11 #include "ipc/ipc_endpoint.h" | 10 #include "ipc/ipc_endpoint.h" |
| 12 | 11 |
| 13 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 14 #include "ipc/attachment_broker_unprivileged_win.h" | 13 #include "ipc/attachment_broker_unprivileged_win.h" |
| 15 #endif | 14 #endif |
| 16 | 15 |
| 17 #if defined(OS_MACOSX) && !defined(OS_IOS) | 16 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 18 #include "ipc/attachment_broker_unprivileged_mac.h" | 17 #include "ipc/attachment_broker_unprivileged_mac.h" |
| 19 #endif | 18 #endif |
| 20 | 19 |
| 21 namespace IPC { | 20 namespace IPC { |
| 22 | 21 |
| 23 namespace { | 22 AttachmentBrokerUnprivileged::AttachmentBrokerUnprivileged() |
| 23 : sender_(nullptr) { |
| 24 IPC::AttachmentBroker::SetGlobal(this); |
| 25 } |
| 24 | 26 |
| 25 // On platforms that support attachment brokering, returns a new instance of | 27 AttachmentBrokerUnprivileged::~AttachmentBrokerUnprivileged() { |
| 26 // a platform-specific attachment broker. Otherwise returns |nullptr|. | 28 IPC::AttachmentBroker::SetGlobal(nullptr); |
| 27 // The caller takes ownership of the newly created instance, and is | 29 } |
| 28 // responsible for ensuring that the attachment broker lives longer than | 30 |
| 29 // every IPC::Channel. The new instance automatically registers itself as the | 31 // static |
| 30 // global attachment broker. | 32 scoped_ptr<AttachmentBrokerUnprivileged> |
| 31 scoped_ptr<AttachmentBrokerUnprivileged> CreateBroker() { | 33 AttachmentBrokerUnprivileged::CreateBroker() { |
| 32 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 33 return scoped_ptr<AttachmentBrokerUnprivileged>( | 35 return scoped_ptr<AttachmentBrokerUnprivileged>( |
| 34 new IPC::AttachmentBrokerUnprivilegedWin); | 36 new IPC::AttachmentBrokerUnprivilegedWin); |
| 35 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 37 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 36 return scoped_ptr<AttachmentBrokerUnprivileged>( | 38 return scoped_ptr<AttachmentBrokerUnprivileged>( |
| 37 new IPC::AttachmentBrokerUnprivilegedMac); | 39 new IPC::AttachmentBrokerUnprivilegedMac); |
| 38 #else | 40 #else |
| 39 return nullptr; | 41 return nullptr; |
| 40 #endif | 42 #endif |
| 41 } | 43 } |
| 42 | 44 |
| 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::DesignateBrokerCommunicationChannel( | 45 void AttachmentBrokerUnprivileged::DesignateBrokerCommunicationChannel( |
| 79 Endpoint* endpoint) { | 46 Endpoint* endpoint) { |
| 80 DCHECK(endpoint); | 47 DCHECK(endpoint); |
| 81 DCHECK(!sender_); | 48 DCHECK(!sender_); |
| 82 sender_ = endpoint; | 49 sender_ = endpoint; |
| 83 endpoint->SetAttachmentBrokerEndpoint(true); | 50 endpoint->SetAttachmentBrokerEndpoint(true); |
| 84 } | 51 } |
| 85 | 52 |
| 86 bool AttachmentBrokerUnprivileged::IsPrivilegedBroker() { | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 void AttachmentBrokerUnprivileged::LogError(UMAError error) { | 53 void AttachmentBrokerUnprivileged::LogError(UMAError error) { |
| 91 UMA_HISTOGRAM_ENUMERATION( | 54 UMA_HISTOGRAM_ENUMERATION( |
| 92 "IPC.AttachmentBrokerUnprivileged.BrokerAttachmentError", error, | 55 "IPC.AttachmentBrokerUnprivileged.BrokerAttachmentError", error, |
| 93 ERROR_MAX); | 56 ERROR_MAX); |
| 94 } | 57 } |
| 95 | 58 |
| 96 } // namespace IPC | 59 } // namespace IPC |
| OLD | NEW |