Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Asynchronous Advertising ID Retrieval for H5vccSystem #4794

Merged
merged 21 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ protected String getUserAgentAuxField() {
// Used in starboard/android/shared/system_get_property.cc
/** Returns string for kSbSystemPropertyAdvertisingId */
@SuppressWarnings("unused")
@UsedByNative
@CalledByNative
protected String getAdvertisingId() {
return this.advertisingId.getId();
}
Expand Down
2 changes: 2 additions & 0 deletions cobalt/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ source_set("browser") {
":embed_polyfilled_javascript",
"//cobalt/browser/crash_annotator",
"//cobalt/browser/crash_annotator/public/mojom",
"//cobalt/browser/h5vcc_system",
andrewsavage1 marked this conversation as resolved.
Show resolved Hide resolved
"//cobalt/browser/h5vcc_system/public/mojom",
"//cobalt/user_agent",
"//components/js_injection/browser:browser",
"//content/public/browser",
Expand Down
4 changes: 4 additions & 0 deletions cobalt/browser/cobalt_browser_interface_binders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "base/functional/bind.h"
#include "cobalt/browser/crash_annotator/crash_annotator_impl.h"
#include "cobalt/browser/crash_annotator/public/mojom/crash_annotator.mojom.h"
#include "cobalt/browser/h5vcc_system/h5vcc_system_impl.h"
#include "cobalt/browser/h5vcc_system/public/mojom/h5vcc_system.mojom.h"

namespace cobalt {

Expand All @@ -25,6 +27,8 @@ void PopulateCobaltFrameBinders(
mojo::BinderMapWithContext<content::RenderFrameHost*>* binder_map) {
binder_map->Add<crash_annotator::mojom::CrashAnnotator>(
base::BindRepeating(&crash_annotator::CrashAnnotatorImpl::Create));
binder_map->Add<h5vcc_system::mojom::H5vccSystem>(
base::BindRepeating(&h5vcc_system::H5vccSystemImpl::Create));
}

} // namespace cobalt
28 changes: 28 additions & 0 deletions cobalt/browser/h5vcc_system/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source_set("h5vcc_system") {
sources = [
"h5vcc_system_impl.cc",
"h5vcc_system_impl.h",
]

deps = [
"//base",
"//cobalt/browser/h5vcc_system/public/mojom",
"//content/public/browser",
"//mojo/public/cpp/bindings",
"//starboard/android/shared:starboard_platform",
]
}
46 changes: 46 additions & 0 deletions cobalt/browser/h5vcc_system/h5vcc_system_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cobalt/browser/h5vcc_system/h5vcc_system_impl.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "starboard/android/shared/starboard_bridge.h"

using starboard::android::shared::StarboardBridge;

namespace h5vcc_system {

H5vccSystemImpl::H5vccSystemImpl(
content::RenderFrameHost& render_frame_host,
mojo::PendingReceiver<mojom::H5vccSystem> receiver)
: content::DocumentService<mojom::H5vccSystem>(render_frame_host,
std::move(receiver)) {}

void H5vccSystemImpl::Create(
content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<mojom::H5vccSystem> receiver) {
new H5vccSystemImpl(*render_frame_host, std::move(receiver));
}

void H5vccSystemImpl::GetAdvertisingId(GetAdvertisingIdCallback callback) {
std::string advertising_id;
#if BUILDFLAG(IS_ANDROID)
JNIEnv* env = base::android::AttachCurrentThread();
zhongqiliang marked this conversation as resolved.
Show resolved Hide resolved
StarboardBridge* starbooard_bridge = StarboardBridge::GetInstance();
advertising_id = starbooard_bridge->GetAdvertisingId(env);
#endif
std::move(callback).Run(advertising_id);
}

} // namespace h5vcc_system
52 changes: 52 additions & 0 deletions cobalt/browser/h5vcc_system/h5vcc_system_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COBALT_BROWSER_H5VCC_SYSTEM_H5VCC_SYSTEM_IMPL_H_
#define COBALT_BROWSER_H5VCC_SYSTEM_H5VCC_SYSTEM_IMPL_H_

#include <string>

#include "cobalt/browser/h5vcc_system/public/mojom/h5vcc_system.mojom.h"
#include "content/public/browser/document_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"

namespace content {
class RenderFrameHost;
} // namespace content

namespace h5vcc_system {

// Implements the H5vccSystem Mojo interface and extends
// DocumentService so that an object's lifetime is scoped to the corresponding
// document / RenderFrameHost (see DocumentService for details).
class H5vccSystemImpl : public content::DocumentService<mojom::H5vccSystem> {
andrewsavage1 marked this conversation as resolved.
Show resolved Hide resolved
public:
// Creates a H5vccSystemImpl. The H5vccSystemImpl is bound to the
// receiver and its lifetime is scoped to the render_frame_host.
static void Create(content::RenderFrameHost* render_frame_host,
mojo::PendingReceiver<mojom::H5vccSystem> receiver);

H5vccSystemImpl(const H5vccSystemImpl&) = delete;
H5vccSystemImpl& operator=(const H5vccSystemImpl&) = delete;

void GetAdvertisingId(GetAdvertisingIdCallback) override;

private:
H5vccSystemImpl(content::RenderFrameHost& render_frame_host,
mojo::PendingReceiver<mojom::H5vccSystem> receiver);
};

} // namespace h5vcc_system

#endif // COBALT_BROWSER_H5VCC_SYSTEM_H5VCC_SYSTEM_IMPL_H_
19 changes: 19 additions & 0 deletions cobalt/browser/h5vcc_system/public/mojom/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//mojo/public/tools/bindings/mojom.gni")

mojom("mojom") {
sources = [ "h5vcc_system.mojom" ]
}
22 changes: 22 additions & 0 deletions cobalt/browser/h5vcc_system/public/mojom/h5vcc_system.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module h5vcc_system.mojom;

// The browser process must provide an implementation of this interface so that
// the renderer process can implement the H5vccSystem Blink API.
interface H5vccSystem {
// Get the GetAdvertisingId for the device.
GetAdvertisingId() => (string id);
};
8 changes: 8 additions & 0 deletions starboard/android/shared/starboard_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ ScopedJavaLocalRef<jobject> StarboardBridge::GetTextToSpeechHelper(
SB_DCHECK(env);
return Java_StarboardBridge_getTextToSpeechHelper(env, j_starboard_bridge_);
}

std::string StarboardBridge::GetAdvertisingId(JNIEnv* env) {
SB_DCHECK(env);
ScopedJavaLocalRef<jstring> advertising_id_java =
Java_StarboardBridge_getAdvertisingId(env, j_starboard_bridge_);
return ConvertJavaStringToUTF8(env, advertising_id_java);
}

} // namespace shared
} // namespace android
} // namespace starboard
2 changes: 2 additions & 0 deletions starboard/android/shared/starboard_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class StarboardBridge {

ScopedJavaLocalRef<jobject> GetTextToSpeechHelper(JNIEnv* env);

std::string GetAdvertisingId(JNIEnv* env);

private:
StarboardBridge() = default;
~StarboardBridge() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ blink_modules_sources("h5vcc_system") {
"h_5_vcc_system.cc",
"h_5_vcc_system.h",
]

deps = [ "//cobalt/browser/h5vcc_system/public/mojom:mojom_blink" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,48 @@
// limitations under the License.

#include "third_party/blink/renderer/modules/cobalt/h5vcc_system/h_5_vcc_system.h"

#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"

namespace blink {

H5vccSystem::H5vccSystem(LocalDOMWindow& window) {}
H5vccSystem::H5vccSystem(LocalDOMWindow& window)
: ExecutionContextLifecycleObserver(window.GetExecutionContext()),
remote_h5vcc_system_(window.GetExecutionContext()) {}

const String H5vccSystem::advertisingId() const {
NOTIMPLEMENTED();
void H5vccSystem::ContextDestroyed() {}

ScriptPromise H5vccSystem::getAdvertisingId(ScriptState* script_state,
ExceptionState& exception_state) {
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(
script_state, exception_state.GetContext());

EnsureReceiverIsBound();

remote_h5vcc_system_->GetAdvertisingId(
zhongqiliang marked this conversation as resolved.
Show resolved Hide resolved
WTF::BindOnce(&H5vccSystem::OnGetAdvertisingId, WrapPersistent(this),
WrapPersistent(resolver)));

return resolver->Promise();
}

void H5vccSystem::OnGetAdvertisingId(ScriptPromiseResolver* resolver,
const String& result) {
resolver->Resolve(result);
}

void H5vccSystem::EnsureReceiverIsBound() {
DCHECK(GetExecutionContext());

if (remote_h5vcc_system_.is_bound()) {
return;
}

// TODO(b/377049113) add a mojom service and populate the value for
// advertising_id_.
// return advertising_id_;
return String("fake advertisingId");
auto task_runner =
GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI);
GetExecutionContext()->GetBrowserInterfaceBroker().GetInterface(
remote_h5vcc_system_.BindNewPipeAndPassReceiver(task_runner));
}

bool H5vccSystem::limitAdTracking() const {
Expand All @@ -38,6 +66,8 @@ bool H5vccSystem::limitAdTracking() const {

void H5vccSystem::Trace(Visitor* visitor) const {
ScriptWrappable::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor);
visitor->Trace(remote_h5vcc_system_);
}

} // namespace blink
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,45 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_H5VCC_SYSTEM_H_5_VCC_SYSTEM_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_H5VCC_SYSTEM_H_5_VCC_SYSTEM_H_

#include "cobalt/browser/h5vcc_system/public/mojom/h5vcc_system.mojom-blink.h"

#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class ExecutionContext;
class LocalDOMWindow;
class ScriptState;
class ScriptPromiseResolver;

class MODULES_EXPORT H5vccSystem final : public ScriptWrappable {
class MODULES_EXPORT H5vccSystem final
: public ScriptWrappable,
public ExecutionContextLifecycleObserver {
zhongqiliang marked this conversation as resolved.
Show resolved Hide resolved
DEFINE_WRAPPERTYPEINFO();

public:
explicit H5vccSystem(LocalDOMWindow&);

void ContextDestroyed() override;

// Web-exposed interface:
const String advertisingId() const;
ScriptPromise getAdvertisingId(ScriptState*, ExceptionState&);
bool limitAdTracking() const;

void Trace(Visitor*) const override;

private:
void OnGetAdvertisingId(ScriptPromiseResolver*, const String&);
void EnsureReceiverIsBound();
HeapMojoRemote<h5vcc_system::mojom::blink::H5vccSystem> remote_h5vcc_system_;
String advertising_id_;
zhongqiliang marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace blink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SecureContext
]
interface H5vccSystem {
readonly attribute DOMString advertisingId;
[CallWith=ScriptState, RaisesException]
Promise<DOMString> getAdvertisingId();
readonly attribute boolean limitAdTracking;
};
Loading