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

Unified Diff: ipc/attachment_broker_unprivileged.cc

Issue 1679763002: Clean up public interface of AttachmentBrokerUnprivileged. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from tsepez. Created 4 years, 10 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
Index: ipc/attachment_broker_unprivileged.cc
diff --git a/ipc/attachment_broker_unprivileged.cc b/ipc/attachment_broker_unprivileged.cc
index 9286a8900574b84b7022d64d053b1f28c6c30b69..a735bf8a337d91bbe30ef53ac530a4eb4721ae28 100644
--- a/ipc/attachment_broker_unprivileged.cc
+++ b/ipc/attachment_broker_unprivileged.cc
@@ -4,6 +4,7 @@
#include "ipc/attachment_broker_unprivileged.h"
+#include "base/lazy_instance.h"
#include "base/metrics/histogram_macros.h"
#include "build/build_config.h"
#include "ipc/ipc_channel.h"
@@ -19,18 +20,15 @@
namespace IPC {
-AttachmentBrokerUnprivileged::AttachmentBrokerUnprivileged()
- : sender_(nullptr) {
- IPC::AttachmentBroker::SetGlobal(this);
-}
-
-AttachmentBrokerUnprivileged::~AttachmentBrokerUnprivileged() {
- IPC::AttachmentBroker::SetGlobal(nullptr);
-}
+namespace {
-// static
-scoped_ptr<AttachmentBrokerUnprivileged>
-AttachmentBrokerUnprivileged::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<AttachmentBrokerUnprivileged> CreateBroker() {
#if defined(OS_WIN)
return scoped_ptr<AttachmentBrokerUnprivileged>(
new IPC::AttachmentBrokerUnprivilegedWin);
@@ -42,6 +40,41 @@ AttachmentBrokerUnprivileged::CreateBroker() {
#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() {
+ // Single process tests can cause an attachment broker to already exist.
+ if (AttachmentBroker::GetGlobal())
+ return;
+ attachment_broker_ = CreateBroker();
+ }
+
+ private:
+ scoped_ptr<IPC::AttachmentBrokerUnprivileged> attachment_broker_;
+};
+
+base::LazyInstance<AttachmentBrokerMakeOnce>::Leaky
+ g_attachment_broker_make_once = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+AttachmentBrokerUnprivileged::AttachmentBrokerUnprivileged()
+ : sender_(nullptr) {
+ IPC::AttachmentBroker::SetGlobal(this);
+}
+
+AttachmentBrokerUnprivileged::~AttachmentBrokerUnprivileged() {
+ IPC::AttachmentBroker::SetGlobal(nullptr);
+}
+
+// static
+void AttachmentBrokerUnprivileged::CreateBrokerIfNeeded() {
+ g_attachment_broker_make_once.Get();
+}
+
void AttachmentBrokerUnprivileged::DesignateBrokerCommunicationChannel(
Endpoint* endpoint) {
DCHECK(endpoint);
@@ -50,6 +83,10 @@ void AttachmentBrokerUnprivileged::DesignateBrokerCommunicationChannel(
endpoint->SetAttachmentBrokerEndpoint(true);
}
+bool AttachmentBrokerUnprivileged::IsPrivilegedBroker() {
+ return false;
+}
+
void AttachmentBrokerUnprivileged::LogError(UMAError error) {
UMA_HISTOGRAM_ENUMERATION(
"IPC.AttachmentBrokerUnprivileged.BrokerAttachmentError", error,

Powered by Google App Engine
This is Rietveld 408576698