Skip to content

Commit 1907448

Browse files
Cherry pick PR #7678: Change getFeature to async (#8172)
Refer to the original PR: #7678 Bug: 450080869 getFeature needs mojo call to access the browser process, therefore changing its definition to async. getFeatureParam can access all the params within the renderer process, so removing the mojo calls and implementations to keep it synchronous. --------- Co-authored-by: Yijia Zhang <yijiazhang@google.com>
1 parent a08ec55 commit 1907448

File tree

10 files changed

+96
-49
lines changed

10 files changed

+96
-49
lines changed

cobalt/browser/h5vcc_experiments/h5vcc_experiments_impl.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,6 @@ void H5vccExperimentsImpl::GetFeature(const std::string& feature_name,
144144
std::move(callback).Run(GetFeatureInternal(feature_name));
145145
}
146146

147-
void H5vccExperimentsImpl::GetFeatureParam(
148-
const std::string& feature_param_name,
149-
GetFeatureParamCallback callback) {
150-
std::string param_value = base::GetFieldTrialParamValue(
151-
cobalt::kCobaltExperimentName, feature_param_name);
152-
std::move(callback).Run(param_value);
153-
}
154-
155147
void H5vccExperimentsImpl::GetLatestExperimentConfigHashData(
156148
GetLatestExperimentConfigHashDataCallback callback) {
157149
std::move(callback).Run(

cobalt/browser/h5vcc_experiments/h5vcc_experiments_impl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ class H5vccExperimentsImpl
5050
void GetActiveExperimentConfigData(
5151
GetActiveExperimentConfigDataCallback) override;
5252
void GetFeature(const std::string& feature_name, GetFeatureCallback) override;
53-
void GetFeatureParam(const std::string& feature_param_name,
54-
GetFeatureParamCallback) override;
5553
void GetLatestExperimentConfigHashData(
5654
GetLatestExperimentConfigHashDataCallback) override;
5755
void SetLatestExperimentConfigHashData(

cobalt/browser/h5vcc_experiments/public/mojom/h5vcc_experiments.mojom

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ enum OverrideState {
2929
interface H5vccExperiments {
3030
SetExperimentState(mojo_base.mojom.DictionaryValue experiment_config) => ();
3131
ResetExperimentState() => ();
32-
[Sync]
3332
GetFeature(string feature_name) => (OverrideState feature_value);
34-
[Sync]
35-
GetFeatureParam(string feature_param_name) => (string feature_param_value);
3633
GetActiveExperimentConfigData() => (string active_experiment_config_data);
3734
GetLatestExperimentConfigHashData() => (string latest_experiment_config_hash_data);
3835
SetLatestExperimentConfigHashData(string hash_data) => ();

third_party/blink/renderer/modules/cobalt/h5vcc_experiments/h_5_vcc_experiments.cc

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "third_party/blink/renderer/modules/cobalt/h5vcc_experiments/h_5_vcc_experiments.h"
1616

17+
#include "base/metrics/field_trial_params.h"
1718
#include "base/values.h"
1819
#include "cobalt/browser/constants/cobalt_experiment_names.h"
1920
#include "cobalt/browser/h5vcc_experiments/public/mojom/h5vcc_experiments.mojom-blink.h"
@@ -78,29 +79,26 @@ ScriptPromise H5vccExperiments::resetExperimentState(
7879
return resolver->Promise();
7980
}
8081

81-
String H5vccExperiments::getFeature(const String& feature_name) {
82+
ScriptPromise H5vccExperiments::getFeature(ScriptState* script_state,
83+
const String& feature_name,
84+
ExceptionState& exception_state) {
85+
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(
86+
script_state, exception_state.GetContext());
87+
auto promise = resolver->Promise();
8288
EnsureReceiverIsBound();
83-
h5vcc_experiments::mojom::blink::OverrideState feature_state;
84-
remote_h5vcc_experiments_->GetFeature(feature_name, &feature_state);
85-
switch (feature_state) {
86-
case h5vcc_experiments::mojom::blink::OverrideState::OVERRIDE_USE_DEFAULT:
87-
return V8OverrideState(V8OverrideState::Enum::kDEFAULT);
88-
case h5vcc_experiments::mojom::blink::OverrideState::
89-
OVERRIDE_ENABLE_FEATURE:
90-
return V8OverrideState(V8OverrideState::Enum::kENABLED);
91-
case h5vcc_experiments::mojom::blink::OverrideState::
92-
OVERRIDE_DISABLE_FEATURE:
93-
return V8OverrideState(V8OverrideState::Enum::kDISABLED);
94-
}
95-
NOTREACHED_NORETURN() << "Invalid feature OverrideState for feature "
96-
<< feature_name;
89+
90+
ongoing_requests_.insert(resolver);
91+
remote_h5vcc_experiments_->GetFeature(
92+
feature_name,
93+
WTF::BindOnce(&H5vccExperiments::OnGetFeature, WrapPersistent(this),
94+
WrapPersistent(resolver)));
95+
return promise;
9796
}
9897

9998
const String& H5vccExperiments::getFeatureParam(
10099
const String& feature_param_name) {
101-
EnsureReceiverIsBound();
102-
remote_h5vcc_experiments_->GetFeatureParam(feature_param_name,
103-
&feature_param_value_);
100+
feature_param_value_ = String::FromUTF8(base::GetFieldTrialParamValue(
101+
cobalt::kCobaltExperimentName, feature_param_name.Utf8()));
104102
return feature_param_value_;
105103
}
106104

@@ -217,6 +215,26 @@ void H5vccExperiments::OnResetExperimentState(ScriptPromiseResolver* resolver) {
217215
resolver->Resolve();
218216
}
219217

218+
void H5vccExperiments::OnGetFeature(
219+
ScriptPromiseResolver* resolver,
220+
h5vcc_experiments::mojom::blink::OverrideState feature_state) {
221+
ongoing_requests_.erase(resolver);
222+
switch (feature_state) {
223+
case h5vcc_experiments::mojom::blink::OverrideState::OVERRIDE_USE_DEFAULT:
224+
resolver->Resolve(V8OverrideState(V8OverrideState::Enum::kDEFAULT));
225+
return;
226+
case h5vcc_experiments::mojom::blink::OverrideState::
227+
OVERRIDE_ENABLE_FEATURE:
228+
resolver->Resolve(V8OverrideState(V8OverrideState::Enum::kENABLED));
229+
return;
230+
case h5vcc_experiments::mojom::blink::OverrideState::
231+
OVERRIDE_DISABLE_FEATURE:
232+
resolver->Resolve(V8OverrideState(V8OverrideState::Enum::kDISABLED));
233+
return;
234+
}
235+
NOTREACHED() << "Invalid feature OverrideState for feature";
236+
}
237+
220238
void H5vccExperiments::OnConnectionError() {
221239
remote_h5vcc_experiments_.reset();
222240
HeapHashSet<Member<ScriptPromiseResolver>> h5vcc_experiments_promises;

third_party/blink/renderer/modules/cobalt/h5vcc_experiments/h_5_vcc_experiments.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MODULES_EXPORT H5vccExperiments final
4646
const ExperimentConfiguration*,
4747
ExceptionState&);
4848
ScriptPromise resetExperimentState(ScriptState*, ExceptionState&);
49-
String getFeature(const String&);
49+
ScriptPromise getFeature(ScriptState*, const String&, ExceptionState&);
5050
const String& getFeatureParam(const String&);
5151
ScriptPromise getActiveExperimentConfigData(ScriptState*, ExceptionState&);
5252
ScriptPromise getLatestExperimentConfigHashData(ScriptState*,
@@ -71,6 +71,8 @@ class MODULES_EXPORT H5vccExperiments final
7171
void OnSetFinchParameters(ScriptPromiseResolver*);
7272
void OnSetLatestExperimentConfigHashData(ScriptPromiseResolver*);
7373
void OnResetExperimentState(ScriptPromiseResolver*);
74+
void OnGetFeature(ScriptPromiseResolver*,
75+
h5vcc_experiments::mojom::blink::OverrideState);
7476
void OnConnectionError();
7577
void EnsureReceiverIsBound();
7678
HeapMojoRemote<h5vcc_experiments::mojom::blink::H5vccExperiments>

third_party/blink/renderer/modules/cobalt/h5vcc_experiments/h_5_vcc_experiments.idl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ interface H5vccExperiments {
4040

4141
// The following APIs fetch Cobalt's current value for the given feature name
4242
// or feature param.
43-
OverrideState getFeature(DOMString feature_name);
43+
[CallWith=ScriptState, RaisesException]
44+
Promise<OverrideState> getFeature(DOMString feature_name);
4445

4546
DOMString getFeatureParam(DOMString feature_param_name);
4647

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// META: global=window
2+
// META: script=/resources/test-only-api.js
3+
// META: script=resources/automation.js
4+
h5vcc_experiments_tests(async (t, mockH5vccExperiments) => {
5+
const { OverrideState } = await import(
6+
'/gen/cobalt/browser/h5vcc_experiments/public/mojom/h5vcc_experiments.mojom.m.js');
7+
const test_feature_name = "test_feature_name";
8+
9+
mockH5vccExperiments.stubResult(test_feature_name, OverrideState.OVERRIDE_USE_DEFAULT);
10+
let actual = await window.h5vcc.experiments.getFeature(test_feature_name);
11+
assert_equals(actual, 'DEFAULT');
12+
13+
mockH5vccExperiments.stubResult(test_feature_name, OverrideState.OVERRIDE_DISABLE_FEATURE);
14+
actual = await window.h5vcc.experiments.getFeature(test_feature_name);
15+
assert_equals(actual, 'DISABLED');
16+
17+
mockH5vccExperiments.stubResult(test_feature_name, OverrideState.OVERRIDE_ENABLE_FEATURE);
18+
actual = await window.h5vcc.experiments.getFeature(test_feature_name);
19+
assert_equals(actual, 'ENABLED');
20+
}, 'exercises H5vccExperiments.getFeature()');

third_party/blink/web_tests/wpt_internal/cobalt/h5vcc-experiments/h5vcc-experiments-setExperimentState.https.any.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,21 @@ const test_experiment_config = {
66
"fake_feature": true
77
},
88
"featureParams": {
9-
"fake_feature_param": 1,
9+
"fake_feature_param": 2.0,
10+
"fake_feature_param_2": 2,
11+
"fake_feature_param_3": 1.234,
12+
"fake_feature_param_4": true,
13+
"fake_feature_param_5": "test_param_string",
14+
},
15+
"activeExperimentConfigData": "test_config_data",
16+
"latestExperimentConfigHashData": "test_config_hash",
17+
}
18+
19+
const test_experiment_config_2 = {
20+
"features": {
21+
"fake_feature": true
22+
},
23+
"featureParams": {
1024
},
1125
"activeExperimentConfigData": "test_config_data",
1226
"latestExperimentConfigHashData": "test_config_hash",
@@ -18,12 +32,14 @@ h5vcc_experiments_tests(async (t, mockH5vccExperiments) => {
1832
assert_true(mockH5vccExperiments.hasCalledSetExperimentState());
1933

2034
assert_true(mockH5vccExperiments.getStubResult('fake_feature'));
21-
// All feature params are stored as strings and all numbers are 64-bit
22-
// floating-point value in JS.
23-
assert_equals(mockH5vccExperiments.getStubResult('fake_feature_param'), "1.000000");
35+
// Floats that represent integers get converted into integer.
36+
assert_equals(mockH5vccExperiments.getStubResult('fake_feature_param'), "2");
37+
assert_equals(mockH5vccExperiments.getStubResult('fake_feature_param_2'), "2");
38+
assert_equals(mockH5vccExperiments.getStubResult('fake_feature_param_3'), "1.234");
39+
assert_equals(mockH5vccExperiments.getStubResult('fake_feature_param_4'), "true");
40+
assert_equals(mockH5vccExperiments.getStubResult('fake_feature_param_5'), "test_param_string");
2441
}, 'setExperimentState() sends expected config content to browser endpoint');
2542

26-
2743
h5vcc_experiments_mojo_disconnection_tests(async (t) => {
2844
return promise_rejects_exactly(t, 'Mojo connection error.',
2945
window.h5vcc.experiments.setExperimentState(test_experiment_config));

third_party/blink/web_tests/wpt_internal/cobalt/h5vcc-experiments/h5vcc-experiments-setLatestExperimentConfigHashData.https.any renamed to third_party/blink/web_tests/wpt_internal/cobalt/h5vcc-experiments/h5vcc-experiments-setLatestExperimentConfigHashData.https.any.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ h5vcc_experiments_tests(async (t, mockH5vccExperiments) => {
1212

1313

1414
h5vcc_experiments_mojo_disconnection_tests(async (t) => {
15+
const test_config_hash = "test_config_hash";
1516
return promise_rejects_exactly(t, 'Mojo connection error.',
1617
window.h5vcc.experiments.setLatestExperimentConfigHashData(test_config_hash));
1718
}, 'setLatestExperimentConfigHashData() rejects when unimplemented due to pipe closure');

third_party/blink/web_tests/wpt_internal/cobalt/h5vcc-experiments/resources/mock-h5vcc-experiments.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,25 @@ class MockH5vccExperiments {
4949
this.called_set_latest_experiment_config_hash_data = true;
5050
}
5151

52-
// Added for stubbing getFeature() and getFeatureParam() result in tests.
52+
// Added for stubbing getFeature() result in tests.
5353
stubResult(key, value) {
5454
this.stub_result_.set(key, value);
5555
}
5656

5757
async getActiveExperimentConfigData() {
58-
return this.stub_result_.get(this.STUB_KEY_ACTIVE_CONFIG_DATA);
58+
return {
59+
activeExperimentConfigData: this.stub_result_.get(this.STUB_KEY_ACTIVE_CONFIG_DATA)
60+
};
5961
}
6062

6163
getConfigHash() {
6264
return this.STUB_KEY_CONFIG_CONFIG_HASH;
6365
}
6466

65-
getFeature(feature_name) {
66-
throw new Error('Sync methods not supported in MojoJS (b/406809316');
67-
}
68-
69-
getFeatureParam(feature_param_name) {
70-
throw new Error('Sync methods not supported in MojoJS (b/406809316');
67+
async getFeature(feature_name) {
68+
return {
69+
featureValue: this.stub_result_.get(feature_name)
70+
};
7171
}
7272

7373
// Helper function to get stubbed feature state.
@@ -76,7 +76,9 @@ class MockH5vccExperiments {
7676
}
7777

7878
async getLatestExperimentConfigHashData() {
79-
return this.stub_result_.get(this.STUB_KEY_CONFIG_CONFIG_HASH);
79+
return {
80+
latestExperimentConfigHashData: this.stub_result_.get(this.STUB_KEY_CONFIG_CONFIG_HASH)
81+
};
8082
}
8183

8284
hasCalledResetExperimentState() {
@@ -154,8 +156,8 @@ class MockH5vccExperiments {
154156
return;
155157
}
156158

157-
async setLatestExperimentConfigHashData(config_hash) {
158-
this.stubConfigHashData(config_hash);
159+
async setLatestExperimentConfigHashData(hash_data) {
160+
this.stubConfigHashData(hash_data);
159161
return;
160162
}
161163
}

0 commit comments

Comments
 (0)