Skip to content

Commit

Permalink
Bug 1874990: Generate JS bindings for application-services' rust comp…
Browse files Browse the repository at this point in the history
…onents r=lina,sync-reviewers,markh

Differential Revision: https://phabricator.services.mozilla.com/D198757
  • Loading branch information
Daisuke Akatsuka committed Jan 19, 2024
1 parent 13acbce commit 904a08b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
2 changes: 2 additions & 0 deletions services/sync/modules/engines/tabs.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ TabEngine.prototype = {
tabs.map(tab => {
// rust wants lastUsed in MS but the provider gives it in seconds
tab.lastUsed = tab.lastUsed * 1000;
tab.inactive = false;
return tab;
})
);
Expand Down Expand Up @@ -414,6 +415,7 @@ export const TabProvider = {
title: tab.linkedBrowser.contentTitle || "",
urlHistory: [url],
icon: "",
inactive: false,
lastUsed: Math.floor((tab.lastAccessed || 0) / 1000),
};
tabRecords.push(thisTab);
Expand Down
1 change: 1 addition & 0 deletions services/sync/tests/unit/test_tab_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ add_task(async function test_tab_engine_skips_incoming_local_record() {
title: "title2",
urlHistory: ["http://bar.com/"],
icon: "",
inactive: false,
lastUsed: 3000,
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,16 @@ Suggestion.Amo = class extends Suggestion{
this.score = score;
}
}
Suggestion.Yelp = class extends Suggestion{
constructor(
url,
title
) {
super();
this.url = url;
this.title = title;
}
}

// Export the FFIConverter object to make external types work.
export class FfiConverterTypeSuggestion extends FfiConverterArrayBuffer {
Expand Down Expand Up @@ -893,6 +903,11 @@ export class FfiConverterTypeSuggestion extends FfiConverterArrayBuffer {
FfiConverterString.read(dataStream),
FfiConverterF64.read(dataStream)
);
case 5:
return new Suggestion.Yelp(
FfiConverterString.read(dataStream),
FfiConverterString.read(dataStream)
);
default:
return new Error("Unknown Suggestion variant");
}
Expand Down Expand Up @@ -942,6 +957,12 @@ export class FfiConverterTypeSuggestion extends FfiConverterArrayBuffer {
FfiConverterF64.write(dataStream, value.score);
return;
}
if (value instanceof Suggestion.Yelp) {
dataStream.writeInt32(5);
FfiConverterString.write(dataStream, value.url);
FfiConverterString.write(dataStream, value.title);
return;
}
return new Error("Unknown Suggestion variant");
}

Expand Down Expand Up @@ -987,6 +1008,11 @@ export class FfiConverterTypeSuggestion extends FfiConverterArrayBuffer {
totalSize += FfiConverterF64.computeSize(value.score);
return totalSize;
}
if (value instanceof Suggestion.Yelp) {
totalSize += FfiConverterString.computeSize(value.url);
totalSize += FfiConverterString.computeSize(value.title);
return totalSize;
}
return new Error("Unknown Suggestion variant");
}

Expand All @@ -1004,6 +1030,7 @@ export const SuggestionProvider = {
POCKET: 2,
WIKIPEDIA: 3,
AMO: 4,
YELP: 5,
};

Object.freeze(SuggestionProvider);
Expand All @@ -1019,6 +1046,8 @@ export class FfiConverterTypeSuggestionProvider extends FfiConverterArrayBuffer
return SuggestionProvider.WIKIPEDIA
case 4:
return SuggestionProvider.AMO
case 5:
return SuggestionProvider.YELP
default:
return new Error("Unknown SuggestionProvider variant");
}
Expand All @@ -1041,6 +1070,10 @@ export class FfiConverterTypeSuggestionProvider extends FfiConverterArrayBuffer
dataStream.writeInt32(4);
return;
}
if (value === SuggestionProvider.YELP) {
dataStream.writeInt32(5);
return;
}
return new Error("Unknown SuggestionProvider variant");
}

Expand All @@ -1049,7 +1082,7 @@ export class FfiConverterTypeSuggestionProvider extends FfiConverterArrayBuffer
}

static checkType(value) {
if (!Number.isInteger(value) || value < 1 || value > 4) {
if (!Number.isInteger(value) || value < 1 || value > 5) {
throw new UniFFITypeError(`${value} is not a valid value for SuggestionProvider`);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,29 @@ export class FfiConverterI64 extends FfiConverter {
}
}

// Export the FFIConverter object to make external types work.
export class FfiConverterBool extends FfiConverter {
static computeSize() {
return 1;
}
static lift(value) {
return value == 1;
}
static lower(value) {
if (value) {
return 1;
} else {
return 0;
}
}
static write(dataStream, value) {
dataStream.writeUint8(this.lower(value))
}
static read(dataStream) {
return this.lift(dataStream.readUint8())
}
}

// Export the FFIConverter object to make external types work.
export class FfiConverterString extends FfiConverter {
static checkType(value) {
Expand Down Expand Up @@ -898,7 +921,7 @@ export class FfiConverterTypeClientRemoteTabs extends FfiConverterArrayBuffer {
}

export class RemoteTabRecord {
constructor(title,urlHistory,icon,lastUsed) {
constructor(title,urlHistory,icon,lastUsed,inactive = false) {
try {
FfiConverterString.checkType(title)
} catch (e) {
Expand Down Expand Up @@ -931,17 +954,27 @@ export class RemoteTabRecord {
}
throw e;
}
try {
FfiConverterBool.checkType(inactive)
} catch (e) {
if (e instanceof UniFFITypeError) {
e.addItemDescriptionPart("inactive");
}
throw e;
}
this.title = title;
this.urlHistory = urlHistory;
this.icon = icon;
this.lastUsed = lastUsed;
this.inactive = inactive;
}
equals(other) {
return (
this.title == other.title &&
this.urlHistory == other.urlHistory &&
this.icon == other.icon &&
this.lastUsed == other.lastUsed
this.lastUsed == other.lastUsed &&
this.inactive == other.inactive
)
}
}
Expand All @@ -953,14 +986,16 @@ export class FfiConverterTypeRemoteTabRecord extends FfiConverterArrayBuffer {
FfiConverterString.read(dataStream),
FfiConverterSequencestring.read(dataStream),
FfiConverterOptionalstring.read(dataStream),
FfiConverterI64.read(dataStream)
FfiConverterI64.read(dataStream),
FfiConverterBool.read(dataStream)
);
}
static write(dataStream, value) {
FfiConverterString.write(dataStream, value.title);
FfiConverterSequencestring.write(dataStream, value.urlHistory);
FfiConverterOptionalstring.write(dataStream, value.icon);
FfiConverterI64.write(dataStream, value.lastUsed);
FfiConverterBool.write(dataStream, value.inactive);
}

static computeSize(value) {
Expand All @@ -969,6 +1004,7 @@ export class FfiConverterTypeRemoteTabRecord extends FfiConverterArrayBuffer {
totalSize += FfiConverterSequencestring.computeSize(value.urlHistory);
totalSize += FfiConverterOptionalstring.computeSize(value.icon);
totalSize += FfiConverterI64.computeSize(value.lastUsed);
totalSize += FfiConverterBool.computeSize(value.inactive);
return totalSize
}

Expand Down Expand Up @@ -1006,6 +1042,14 @@ export class FfiConverterTypeRemoteTabRecord extends FfiConverterArrayBuffer {
}
throw e;
}
try {
FfiConverterBool.checkType(value.inactive);
} catch (e) {
if (e instanceof UniFFITypeError) {
e.addItemDescriptionPart(".inactive");
}
throw e;
}
}
}

Expand Down

0 comments on commit 904a08b

Please sign in to comment.