Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: ipc/attachment_broker_privileged.cc

Issue 1414603003: ipc: Move AttachmentBrokerPrivileged singleton logic into ipc/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ipc/attachment_broker_privileged.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
22 AttachmentBrokerPrivileged::AttachmentBrokerPrivileged() { 23 namespace {
23 IPC::AttachmentBroker::SetGlobal(this);
24 }
25 24
26 AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() { 25 // On platforms that support attachment brokering, returns a new instance of
27 IPC::AttachmentBroker::SetGlobal(nullptr); 26 // a platform-specific attachment broker. Otherwise returns |nullptr|.
28 } 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 // static 29 // every IPC::Channel. The new instance automatically registers itself as the
31 scoped_ptr<AttachmentBrokerPrivileged> 30 // global attachment broker.
32 AttachmentBrokerPrivileged::CreateBroker() { 31 scoped_ptr<AttachmentBrokerPrivileged> CreateBroker() {
33 #if defined(OS_WIN) 32 #if defined(OS_WIN)
34 return scoped_ptr<AttachmentBrokerPrivileged>( 33 return scoped_ptr<AttachmentBrokerPrivileged>(
35 new IPC::AttachmentBrokerPrivilegedWin); 34 new IPC::AttachmentBrokerPrivilegedWin);
36 #elif defined(OS_MACOSX) && !defined(OS_IOS) 35 #elif defined(OS_MACOSX) && !defined(OS_IOS)
37 return scoped_ptr<AttachmentBrokerPrivileged>( 36 return scoped_ptr<AttachmentBrokerPrivileged>(
38 new IPC::AttachmentBrokerPrivilegedMac); 37 new IPC::AttachmentBrokerPrivilegedMac);
39 #else 38 #else
40 return nullptr; 39 return nullptr;
41 #endif 40 #endif
42 } 41 }
43 42
43 // This class is wrapped in a singleton to ensure that its constructor is only
44 // called once. The constructor creates an attachment broker and
45 // sets it as the global broker.
Tom Sepez 2015/10/21 20:07:38 Can't lazy instance take care of this for us?
erikchen 2015/10/21 21:54:41 I do use a LazyInstance - I updated the comment to
46 class AttachmentBrokerMakeOnce {
47 public:
48 AttachmentBrokerMakeOnce() {
49 attachment_broker_.reset(CreateBroker().release());
50 }
51
52 private:
53 scoped_ptr<IPC::AttachmentBrokerPrivileged> attachment_broker_;
54 };
55
56 base::LazyInstance<AttachmentBrokerMakeOnce>::Leaky
57 g_attachment_broker_make_once = LAZY_INSTANCE_INITIALIZER;
58
59 } // namespace
60
61 AttachmentBrokerPrivileged::AttachmentBrokerPrivileged() {
62 IPC::AttachmentBroker::SetGlobal(this);
63 }
64
65 AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() {
66 IPC::AttachmentBroker::SetGlobal(nullptr);
67 }
68
69 // static
70 void AttachmentBrokerPrivileged::CreateBrokerIfNeeded() {
71 g_attachment_broker_make_once.Get();
72 }
73
44 void AttachmentBrokerPrivileged::RegisterCommunicationChannel( 74 void AttachmentBrokerPrivileged::RegisterCommunicationChannel(
45 Endpoint* endpoint) { 75 Endpoint* endpoint) {
46 endpoint->SetAttachmentBrokerEndpoint(true); 76 endpoint->SetAttachmentBrokerEndpoint(true);
47 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint); 77 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint);
48 DCHECK(endpoints_.end() == it); 78 DCHECK(endpoints_.end() == it);
49 endpoints_.push_back(endpoint); 79 endpoints_.push_back(endpoint);
50 } 80 }
51 81
52 void AttachmentBrokerPrivileged::DeregisterCommunicationChannel( 82 void AttachmentBrokerPrivileged::DeregisterCommunicationChannel(
53 Endpoint* endpoint) { 83 Endpoint* endpoint) {
54 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint); 84 auto it = std::find(endpoints_.begin(), endpoints_.end(), endpoint);
55 if (it != endpoints_.end()) 85 if (it != endpoints_.end())
56 endpoints_.erase(it); 86 endpoints_.erase(it);
57 } 87 }
58 88
59 Sender* AttachmentBrokerPrivileged::GetSenderWithProcessId(base::ProcessId id) { 89 Sender* AttachmentBrokerPrivileged::GetSenderWithProcessId(base::ProcessId id) {
60 auto it = std::find_if(endpoints_.begin(), endpoints_.end(), 90 auto it = std::find_if(endpoints_.begin(), endpoints_.end(),
61 [id](Endpoint* c) { return c->GetPeerPID() == id; }); 91 [id](Endpoint* c) { return c->GetPeerPID() == id; });
62 if (it == endpoints_.end()) 92 if (it == endpoints_.end())
63 return nullptr; 93 return nullptr;
64 return *it; 94 return *it;
65 } 95 }
66 96
67 void AttachmentBrokerPrivileged::LogError(UMAError error) { 97 void AttachmentBrokerPrivileged::LogError(UMAError error) {
68 UMA_HISTOGRAM_ENUMERATION( 98 UMA_HISTOGRAM_ENUMERATION(
69 "IPC.AttachmentBrokerPrivileged.BrokerAttachmentError", error, ERROR_MAX); 99 "IPC.AttachmentBrokerPrivileged.BrokerAttachmentError", error, ERROR_MAX);
70 } 100 }
71 101
72 } // namespace IPC 102 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/attachment_broker_privileged.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698