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

Unified Diff: content/renderer/mojo/blink_connector_impl.cc

Issue 2643063002: Refactor Blink's ServiceConnector and add ability to mock in layout tests (Closed)
Patch Set: Fix build Created 3 years, 9 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: content/renderer/mojo/blink_connector_impl.cc
diff --git a/content/renderer/mojo/blink_connector_impl.cc b/content/renderer/mojo/blink_connector_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c2f8856ff58db998ef9d78f29522f7b582a21ef7
--- /dev/null
+++ b/content/renderer/mojo/blink_connector_impl.cc
@@ -0,0 +1,90 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/mojo/blink_connector_impl.h"
+
+#include <utility>
+
+#include "base/bind.h"
+#include "base/single_thread_task_runner.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "services/service_manager/public/cpp/identity.h"
+#include "services/service_manager/public/cpp/interface_provider.h"
+
+namespace content {
+
+BlinkConnectorImpl::BlinkConnectorImpl(
+ std::unique_ptr<service_manager::Connector> connector)
+ : connector_(std::move(connector)),
+ main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()),
+ weak_ptr_factory_(this) {}
+
+BlinkConnectorImpl::~BlinkConnectorImpl() = default;
+
+void BlinkConnectorImpl::bindInterface(const char* service_name,
+ const char* interface_name,
+ mojo::ScopedMessagePipeHandle handle) {
+ // |connector_| is null in some testing contexts.
+ if (!connector_)
+ return;
+
+ if (!main_thread_task_runner_->BelongsToCurrentThread()) {
+ main_thread_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&BlinkConnectorImpl::bindInterface, GetWeakPtr(),
+ service_name, interface_name, base::Passed(&handle)));
+ return;
+ }
+
+ // Tests might have overridden this interface with a local implementation.
+ InterfaceBinderMap* overrides_for_service =
+ GetOverridesForService(service_name);
+ if (overrides_for_service) {
+ auto it = overrides_for_service->find(std::string(interface_name));
+ if (it != overrides_for_service->end()) {
+ it->second.Run(std::move(handle));
+ return;
+ }
+ }
+
+ service_manager::Identity service_identity(
+ service_name, service_manager::mojom::kInheritUserID);
+ connector_->BindInterface(service_identity, interface_name,
+ std::move(handle));
+}
+
+void BlinkConnectorImpl::AddOverrideForTesting(
+ const std::string& service_name,
+ const std::string& interface_name,
+ const base::Callback<void(mojo::ScopedMessagePipeHandle)>& binder) {
+ if (service_binders_.find(service_name) == service_binders_.end())
+ service_binders_[service_name] = base::MakeUnique<InterfaceBinderMap>();
+
+ (*(service_binders_[service_name]))[interface_name] = binder;
+}
+
+void BlinkConnectorImpl::ClearOverridesForTesting() {
+ service_binders_.clear();
+}
+
+base::WeakPtr<BlinkConnectorImpl> BlinkConnectorImpl::GetWeakPtr() {
+ return weak_ptr_factory_.GetWeakPtr();
+}
+
+BlinkConnectorImpl::InterfaceBinderMap*
+BlinkConnectorImpl::GetOverridesForService(const char* service_name) {
+ // Short-circuit out in the case where there are no overrides (always true in
+ // production).
+ if (service_binders_.empty())
+ return nullptr;
+
+ auto it = service_binders_.find(std::string(service_name));
+
+ if (it != service_binders_.end())
+ return it->second.get();
+
+ return nullptr;
+}
+
+} // namespace content
« no previous file with comments | « content/renderer/mojo/blink_connector_impl.h ('k') | content/renderer/mojo/blink_connector_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698