Skip to content

Commit

Permalink
Implementing Asynchronous Advertising ID Retrieval for H5vccSystem (#…
Browse files Browse the repository at this point in the history
…4794)

1. A new mojom interface for H5vccSystem is created, including an
asynchronous API method to retrieve the advertising ID.
2. The mojom server implementation handles the request by calling JNI
(Java Native Interface) functions. These JNI calls will interact with
the StarboardBridge to fetch the actual advertising ID value.
3. The renderer client then utilizes the mojom interface to call the
asynchronous method and retrieve the advertising ID.

Tested on devtools, https://screenshot.googleplex.com/43HotkbLgJSQdx3

4. Added web_tests for the mojom interface. 
// Compile the web_test
cobalt/build/gn.py -p linux-x64x11 -C devel &&
autoninja -C out/linux-x64x11_devel blink_wpt_tests &&
autoninja -C out/linux-x64x11_devel dump_syms &&
autoninja -C out/linux-x64x11_devel minidump_stackwalk &&
autoninja -C out/linux-x64x11_devel
cobalt/browser/h5vcc_system/public/mojom:mojom_js
// run the web_test
third_party/blink/tools/run_web_tests.py -t linux-x64x11_devel
wpt_internal/h5vcc/h5vcc-system

b/377049113

---------

Co-authored-by: Colin Liang <[email protected]>
  • Loading branch information
zhongqiliang and Colin Liang authored Feb 8, 2025
1 parent affaaa6 commit 7bbf2ef
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 28 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ files: |
media/starboard/|
starboard/|
third_party/blink/renderer/modules/cobalt/|
third_party/blink/web_tests/wpt_internal/h5vcc/|
components/viz/service/display/starboard/|
ui/ozone/platform/starboard/|
.pre-commit-config.yaml|
Expand Down
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",
"//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
32 changes: 32 additions & 0 deletions cobalt/browser/h5vcc_system/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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",
]

# TODO (b/395154617) clean up
if (is_android) {
deps += [ "//starboard/android/shared:starboard_platform" ]
}
}
50 changes: 50 additions & 0 deletions cobalt/browser/h5vcc_system/h5vcc_system_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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"

#if BUILDFLAG(IS_ANDROID)
#include "starboard/android/shared/starboard_bridge.h"

using starboard::android::shared::StarboardBridge;
#endif

namespace h5vcc_system {

// TODO (b/395126160): refactor mojom implementation on Android
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();
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> {
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 advertising_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(
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,44 @@
#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 {
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_;
};

} // 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;
};

This file was deleted.

Loading

0 comments on commit 7bbf2ef

Please sign in to comment.