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