Chromium Code Reviews| Index: ipc/attachment_broker_privileged.cc |
| diff --git a/ipc/attachment_broker_privileged.cc b/ipc/attachment_broker_privileged.cc |
| index 0fb9ead006d9a3234b79d8a37681c68081999537..f3e89dc798a15cdb8b6c949eea68228d6870b903 100644 |
| --- a/ipc/attachment_broker_privileged.cc |
| +++ b/ipc/attachment_broker_privileged.cc |
| @@ -6,6 +6,7 @@ |
| #include <algorithm> |
| +#include "base/lazy_instance.h" |
| #include "base/metrics/histogram_macros.h" |
| #include "ipc/ipc_endpoint.h" |
| @@ -19,17 +20,15 @@ |
| namespace IPC { |
| -AttachmentBrokerPrivileged::AttachmentBrokerPrivileged() { |
| - IPC::AttachmentBroker::SetGlobal(this); |
| -} |
| - |
| -AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() { |
| - IPC::AttachmentBroker::SetGlobal(nullptr); |
| -} |
| +namespace { |
| -// static |
| -scoped_ptr<AttachmentBrokerPrivileged> |
| -AttachmentBrokerPrivileged::CreateBroker() { |
| +// On platforms that support attachment brokering, returns a new instance of |
| +// a platform-specific attachment broker. Otherwise returns |nullptr|. |
| +// The caller takes ownership of the newly created instance, and is |
| +// responsible for ensuring that the attachment broker lives longer than |
| +// every IPC::Channel. The new instance automatically registers itself as the |
| +// global attachment broker. |
| +scoped_ptr<AttachmentBrokerPrivileged> CreateBroker() { |
| #if defined(OS_WIN) |
| return scoped_ptr<AttachmentBrokerPrivileged>( |
| new IPC::AttachmentBrokerPrivilegedWin); |
| @@ -41,6 +40,37 @@ AttachmentBrokerPrivileged::CreateBroker() { |
| #endif |
| } |
| +// This class is wrapped in a singleton to ensure that its constructor is only |
| +// called once. The constructor creates an attachment broker and |
| +// 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
|
| +class AttachmentBrokerMakeOnce { |
| + public: |
| + AttachmentBrokerMakeOnce() { |
| + attachment_broker_.reset(CreateBroker().release()); |
| + } |
| + |
| + private: |
| + scoped_ptr<IPC::AttachmentBrokerPrivileged> attachment_broker_; |
| +}; |
| + |
| +base::LazyInstance<AttachmentBrokerMakeOnce>::Leaky |
| + g_attachment_broker_make_once = LAZY_INSTANCE_INITIALIZER; |
| + |
| +} // namespace |
| + |
| +AttachmentBrokerPrivileged::AttachmentBrokerPrivileged() { |
| + IPC::AttachmentBroker::SetGlobal(this); |
| +} |
| + |
| +AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() { |
| + IPC::AttachmentBroker::SetGlobal(nullptr); |
| +} |
| + |
| +// static |
| +void AttachmentBrokerPrivileged::CreateBrokerIfNeeded() { |
| + g_attachment_broker_make_once.Get(); |
| +} |
| + |
| void AttachmentBrokerPrivileged::RegisterCommunicationChannel( |
| Endpoint* endpoint) { |
| endpoint->SetAttachmentBrokerEndpoint(true); |