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

Unified 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: Minor formatting. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ipc/attachment_broker_privileged.h ('k') | ipc/attachment_broker_privileged_mac.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ipc/attachment_broker_privileged.cc
diff --git a/ipc/attachment_broker_privileged.cc b/ipc/attachment_broker_privileged.cc
index 0fb9ead006d9a3234b79d8a37681c68081999537..c3ebfda2e927684617b0f82c193517230648a73f 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,28 +20,71 @@
namespace IPC {
-AttachmentBrokerPrivileged::AttachmentBrokerPrivileged() {
- IPC::AttachmentBroker::SetGlobal(this);
-}
+namespace {
-AttachmentBrokerPrivileged::~AttachmentBrokerPrivileged() {
- IPC::AttachmentBroker::SetGlobal(nullptr);
-}
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+// Passed as a constructor parameter to AttachmentBrokerPrivilegedMac.
+base::PortProvider* g_port_provider = nullptr;
+#endif // defined(OS_MACOSX) && !defined(OS_IOS)
-// 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);
#elif defined(OS_MACOSX) && !defined(OS_IOS)
return scoped_ptr<AttachmentBrokerPrivileged>(
- new IPC::AttachmentBrokerPrivilegedMac);
+ new IPC::AttachmentBrokerPrivilegedMac(g_port_provider));
#else
return nullptr;
#endif
}
+// This class is wrapped in a LazyInstance to ensure that its constructor is
+// only called once. The constructor creates an attachment broker and sets it as
+// the global broker.
+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);
+}
+
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+// static
+void AttachmentBrokerPrivileged::CreateBrokerIfNeeded(
+ base::PortProvider* provider) {
+ g_port_provider = provider;
+ g_attachment_broker_make_once.Get();
+}
+#else
+// static
+void AttachmentBrokerPrivileged::CreateBrokerIfNeeded() {
+ g_attachment_broker_make_once.Get();
+}
+#endif // defined(OS_MACOSX) && !defined(OS_IOS)
+
void AttachmentBrokerPrivileged::RegisterCommunicationChannel(
Endpoint* endpoint) {
endpoint->SetAttachmentBrokerEndpoint(true);
« no previous file with comments | « ipc/attachment_broker_privileged.h ('k') | ipc/attachment_broker_privileged_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698