Commit 7cabf412 authored by Gyuyoung Kim's avatar Gyuyoung Kim Committed by Commit Bot

Migrate nfc.mojom and related classes to the new Mojo types

This CL converts implementations of nfc.mojom in the renderer process.
Major changes are as below,

 - Change the argument of SetClient from NFCClient to pending_remote<NFCClient>
   in nfc.mojom
 - Change mojo::Binding with mojo::Receiver
 - Change NFCClientPtr with mojo::Remote<NFCClient>

Bug: 955171
Change-Id: Ibfc900f35dc22736e8af3753705e55fadd5ac76a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1750517
Commit-Queue: Gyuyoung Kim <gyuyoung@igalia.com>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#686678}
parent d73bade5
......@@ -112,7 +112,7 @@ struct NFCReaderOptions {
interface NFC {
// NFCClient interface is used to notify |client| when NDEFMessage matches one
// or more pending watch operations.
SetClient(NFCClient client);
SetClient(pending_remote<NFCClient> client);
// Pushes data to NFC device.
// NFCPushOptions specify timeout and type of device where data should be
......
......@@ -6,6 +6,7 @@
#include <utility>
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
......@@ -29,8 +30,7 @@ namespace blink {
NFC::NFC(LocalFrame* frame)
: PageVisibilityObserver(frame->GetPage()),
ContextLifecycleObserver(frame->GetDocument()),
client_binding_(this) {
ContextLifecycleObserver(frame->GetDocument()) {
String error_message;
// Only connect to NFC if we are in a context that supports it.
......@@ -40,12 +40,11 @@ NFC::NFC(LocalFrame* frame)
// See https://bit.ly/2S0zRAS for task types.
auto task_runner = frame->GetTaskRunner(TaskType::kMiscPlatformAPI);
frame->GetInterfaceProvider().GetInterface(
mojo::MakeRequest(&nfc_, task_runner));
nfc_.set_connection_error_handler(
nfc_remote_.BindNewPipeAndPassReceiver());
nfc_remote_.set_disconnect_handler(
WTF::Bind(&NFC::OnConnectionError, WrapWeakPersistent(this)));
device::mojom::blink::NFCClientPtr client;
client_binding_.Bind(mojo::MakeRequest(&client, task_runner), task_runner);
nfc_->SetClient(std::move(client));
nfc_remote_->SetClient(
client_receiver_.BindNewPipeAndPassRemote(task_runner));
}
NFC* NFC::Create(LocalFrame* frame) {
......@@ -60,11 +59,11 @@ NFC::~NFC() {
}
void NFC::Dispose() {
client_binding_.Close();
client_receiver_.reset();
}
void NFC::ContextDestroyed(ExecutionContext*) {
nfc_.reset();
nfc_remote_.reset();
requests_.clear();
callbacks_.clear();
}
......@@ -121,9 +120,9 @@ ScriptPromise NFC::push(ScriptState* script_state,
requests_.insert(resolver);
auto callback = WTF::Bind(&NFC::OnRequestCompleted, WrapPersistent(this),
WrapPersistent(resolver));
nfc_->Push(std::move(message),
device::mojom::blink::NFCPushOptions::From(options),
std::move(callback));
nfc_remote_->Push(std::move(message),
device::mojom::blink::NFCPushOptions::From(options),
std::move(callback));
return resolver->Promise();
}
......@@ -138,7 +137,7 @@ ScriptPromise NFC::cancelPush(ScriptState* script_state, const String& target) {
requests_.insert(resolver);
auto callback = WTF::Bind(&NFC::OnRequestCompleted, WrapPersistent(this),
WrapPersistent(resolver));
nfc_->CancelPush(StringToNFCPushTarget(target), std::move(callback));
nfc_remote_->CancelPush(StringToNFCPushTarget(target), std::move(callback));
return resolver->Promise();
}
......@@ -168,8 +167,8 @@ ScriptPromise NFC::watch(ScriptState* script_state,
auto watch_callback = WTF::Bind(&NFC::OnWatchRegistered, WrapPersistent(this),
WrapPersistent(callback),
WrapPersistent(resolver), next_watch_id_);
nfc_->Watch(device::mojom::blink::NFCReaderOptions::From(options),
next_watch_id_, std::move(watch_callback));
nfc_remote_->Watch(device::mojom::blink::NFCReaderOptions::From(options),
next_watch_id_, std::move(watch_callback));
next_watch_id_++;
return resolver->Promise();
}
......@@ -190,9 +189,9 @@ ScriptPromise NFC::cancelWatch(ScriptState* script_state, int32_t id) {
callbacks_.erase(id);
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
requests_.insert(resolver);
nfc_->CancelWatch(id,
WTF::Bind(&NFC::OnRequestCompleted, WrapPersistent(this),
WrapPersistent(resolver)));
nfc_remote_->CancelWatch(
id, WTF::Bind(&NFC::OnRequestCompleted, WrapPersistent(this),
WrapPersistent(resolver)));
return resolver->Promise();
}
......@@ -206,23 +205,23 @@ ScriptPromise NFC::cancelWatch(ScriptState* script_state) {
callbacks_.clear();
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
requests_.insert(resolver);
nfc_->CancelAllWatches(WTF::Bind(&NFC::OnRequestCompleted,
WrapPersistent(this),
WrapPersistent(resolver)));
nfc_remote_->CancelAllWatches(WTF::Bind(&NFC::OnRequestCompleted,
WrapPersistent(this),
WrapPersistent(resolver)));
return resolver->Promise();
}
void NFC::PageVisibilityChanged() {
// If service is not initialized, there cannot be any pending NFC activities
if (!nfc_)
if (!nfc_remote_)
return;
// NFC operations should be suspended.
// https://w3c.github.io/web-nfc/#nfc-suspended
if (GetPage()->IsPageVisible())
nfc_->ResumeNFCOperations();
nfc_remote_->ResumeNFCOperations();
else
nfc_->SuspendNFCOperations();
nfc_remote_->SuspendNFCOperations();
}
void NFC::OnRequestCompleted(ScriptPromiseResolver* resolver,
......@@ -238,8 +237,8 @@ void NFC::OnRequestCompleted(ScriptPromiseResolver* resolver,
}
void NFC::OnConnectionError() {
nfc_.reset();
client_binding_.Close();
nfc_remote_.reset();
client_receiver_.reset();
callbacks_.clear();
// If NFCService is not available or disappears when NFC hardware is
......@@ -294,7 +293,7 @@ ScriptPromise NFC::RejectIfNotSupported(ScriptState* script_state) {
DOMExceptionCode::kNotAllowedError, error_message));
}
if (!nfc_) {
if (!nfc_remote_) {
return ScriptPromise::RejectWithDOMException(
script_state,
MakeGarbageCollected<DOMException>(DOMExceptionCode::kNotSupportedError,
......
......@@ -5,7 +5,8 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_NFC_NFC_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_NFC_NFC_H_
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_message_callback.h"
......@@ -88,8 +89,8 @@ class NFC final : public ScriptWrappable,
device::mojom::blink::NDEFMessagePtr) override;
private:
device::mojom::blink::NFCPtr nfc_;
mojo::Binding<device::mojom::blink::NFCClient> client_binding_;
mojo::Remote<device::mojom::blink::NFC> nfc_remote_;
mojo::Receiver<device::mojom::blink::NFCClient> client_receiver_{this};
HeapHashSet<Member<ScriptPromiseResolver>> requests_;
using WatchCallbacksMap = HeapHashMap<uint32_t, Member<V8MessageCallback>>;
WatchCallbacksMap callbacks_;
......
......@@ -38,12 +38,12 @@ NFCProxy::NFCProxy(Document& document)
: PageVisibilityObserver(document.GetPage()),
FocusChangedObserver(document.GetPage()),
Supplement<Document>(document),
client_binding_(this) {}
client_receiver_(this) {}
NFCProxy::~NFCProxy() = default;
void NFCProxy::Dispose() {
client_binding_.Close();
client_receiver_.reset();
}
void NFCProxy::Trace(blink::Visitor* visitor) {
......@@ -60,10 +60,11 @@ void NFCProxy::StartReading(NFCReader* reader) {
return;
EnsureMojoConnection();
nfc_->Watch(device::mojom::blink::NFCReaderOptions::From(reader->options()),
next_watch_id_,
WTF::Bind(&NFCProxy::OnReaderRegistered, WrapPersistent(this),
WrapPersistent(reader), next_watch_id_));
nfc_remote_->Watch(
device::mojom::blink::NFCReaderOptions::From(reader->options()),
next_watch_id_,
WTF::Bind(&NFCProxy::OnReaderRegistered, WrapPersistent(this),
WrapPersistent(reader), next_watch_id_));
readers_.insert(reader, next_watch_id_);
next_watch_id_++;
}
......@@ -72,10 +73,10 @@ void NFCProxy::StopReading(NFCReader* reader) {
DCHECK(reader);
auto iter = readers_.find(reader);
if (iter != readers_.end()) {
if (nfc_) {
if (nfc_remote_) {
// We do not need to notify |reader| of anything.
nfc_->CancelWatch(iter->value,
device::mojom::blink::NFC::CancelWatchCallback());
nfc_remote_->CancelWatch(
iter->value, device::mojom::blink::NFC::CancelWatchCallback());
}
readers_.erase(iter);
}
......@@ -95,14 +96,14 @@ void NFCProxy::Push(device::mojom::blink::NDEFMessagePtr message,
device::mojom::blink::NFCPushOptionsPtr options,
device::mojom::blink::NFC::PushCallback cb) {
EnsureMojoConnection();
nfc_->Push(std::move(message), std::move(options), std::move(cb));
nfc_remote_->Push(std::move(message), std::move(options), std::move(cb));
}
void NFCProxy::CancelPush(
const String& target,
device::mojom::blink::NFC::CancelPushCallback callback) {
DCHECK(nfc_);
nfc_->CancelPush(StringToNFCPushTarget(target), std::move(callback));
DCHECK(nfc_remote_);
nfc_remote_->CancelPush(StringToNFCPushTarget(target), std::move(callback));
}
// device::mojom::blink::NFCClient implementation.
......@@ -153,7 +154,7 @@ void NFCProxy::FocusedFrameChanged() {
void NFCProxy::UpdateSuspendedStatus() {
// If service is not initialized, there cannot be any pending NFC activities.
if (!nfc_)
if (!nfc_remote_)
return;
// NFC operations should be suspended.
......@@ -161,9 +162,9 @@ void NFCProxy::UpdateSuspendedStatus() {
// TODO(https://crbug.com/520391): Suspend/Resume NFC in the browser process
// instead to prevent a compromised renderer from using NFC in the background.
if (ShouldSuspendNFC())
nfc_->SuspendNFCOperations();
nfc_remote_->SuspendNFCOperations();
else
nfc_->ResumeNFCOperations();
nfc_remote_->ResumeNFCOperations();
}
bool NFCProxy::ShouldSuspendNFC() const {
......@@ -183,27 +184,25 @@ bool NFCProxy::ShouldSuspendNFC() const {
}
void NFCProxy::EnsureMojoConnection() {
if (nfc_)
if (nfc_remote_)
return;
// See https://bit.ly/2S0zRAS for task types.
auto task_runner =
GetSupplementable()->GetTaskRunner(TaskType::kMiscPlatformAPI);
GetSupplementable()->GetInterfaceProvider()->GetInterface(
mojo::MakeRequest(&nfc_, task_runner));
nfc_.set_connection_error_handler(
nfc_remote_.BindNewPipeAndPassReceiver());
nfc_remote_.set_disconnect_handler(
WTF::Bind(&NFCProxy::OnMojoConnectionError, WrapWeakPersistent(this)));
// See https://bit.ly/2S0zRAS for task types.
auto task_runner =
GetSupplementable()->GetTaskRunner(TaskType::kMiscPlatformAPI);
// Set client for OnWatch event.
DCHECK(!client_binding_);
device::mojom::blink::NFCClientPtr client;
client_binding_.Bind(mojo::MakeRequest(&client, task_runner), task_runner);
nfc_->SetClient(std::move(client));
nfc_remote_->SetClient(
client_receiver_.BindNewPipeAndPassRemote(task_runner));
}
void NFCProxy::OnMojoConnectionError() {
nfc_.reset();
client_binding_.Close();
nfc_remote_.reset();
client_receiver_.reset();
// Notify all active readers about the connection error and clear the list.
ReaderMap readers = std::move(readers_);
......
......@@ -5,7 +5,8 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_NFC_NFC_PROXY_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_NFC_NFC_PROXY_H_
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/nfc.mojom-blink.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/page/focus_changed_observer.h"
......@@ -90,8 +91,8 @@ class MODULES_EXPORT NFCProxy final
using WriterSet = HeapHashSet<WeakMember<NFCWriter>>;
WriterSet writers_;
device::mojom::blink::NFCPtr nfc_;
mojo::Binding<device::mojom::blink::NFCClient> client_binding_;
mojo::Remote<device::mojom::blink::NFC> nfc_remote_;
mojo::Receiver<device::mojom::blink::NFCClient> client_receiver_;
};
} // namespace blink
......
......@@ -8,6 +8,7 @@
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
......@@ -57,20 +58,18 @@ class MockNFCReader : public NFCReader {
class FakeNfcService : public device::mojom::blink::NFC {
public:
FakeNfcService() : binding_(this) {}
FakeNfcService() : receiver_(this) {}
~FakeNfcService() override = default;
void BindRequest(mojo::ScopedMessagePipeHandle handle) {
DCHECK(!binding_.is_bound());
binding_.Bind(device::mojom::blink::NFCRequest(std::move(handle)));
binding_.set_connection_error_handler(
DCHECK(!receiver_.is_bound());
receiver_.Bind(device::mojom::blink::NFCRequest(std::move(handle)));
receiver_.set_disconnect_handler(
WTF::Bind(&FakeNfcService::OnConnectionError, WTF::Unretained(this)));
}
void OnConnectionError() {
if (binding_.is_bound())
binding_.Unbind();
receiver_.reset();
client_.reset();
}
......@@ -106,8 +105,9 @@ class FakeNfcService : public device::mojom::blink::NFC {
private:
// Override methods from device::mojom::blink::NFC.
void SetClient(device::mojom::blink::NFCClientPtr client) override {
client_ = std::move(client);
void SetClient(
mojo::PendingRemote<device::mojom::blink::NFCClient> client) override {
client_.Bind(std::move(client));
}
void Push(device::mojom::blink::NDEFMessagePtr message,
device::mojom::blink::NFCPushOptionsPtr options,
......@@ -141,9 +141,9 @@ class FakeNfcService : public device::mojom::blink::NFC {
void ResumeNFCOperations() override {}
device::mojom::blink::NDEFMessagePtr tag_message_;
device::mojom::blink::NFCClientPtr client_;
mojo::Remote<device::mojom::blink::NFCClient> client_;
std::map<uint32_t, device::mojom::blink::NFCReaderOptionsPtr> watches_;
mojo::Binding<device::mojom::blink::NFC> binding_;
mojo::Receiver<device::mojom::blink::NFC> receiver_;
};
// Overrides requests for NFC mojo requests with FakeNfcService instances.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment