Commit a5a1f55e authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[Contacts API] Update the select method.

Pass the properties as a parameter of their own since `options`
dictionaries don't generally include required parameters.

Change-Id: Icce60989009611cab2c23fd336981ffae4c1234b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1643512
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#665981}
parent f466c53d
......@@ -81,6 +81,7 @@ mojom::blink::ContactsManagerPtr& ContactsManager::GetContactsManager(
}
ScriptPromise ContactsManager::select(ScriptState* script_state,
const Vector<String>& properties,
ContactsSelectOptions* options) {
Document* document = To<Document>(ExecutionContext::From(script_state));
if (!LocalFrame::HasTransientUserActivation(document ? document->GetFrame()
......@@ -91,7 +92,7 @@ ScriptPromise ContactsManager::select(ScriptState* script_state,
"A user gesture is required to call this method"));
}
if (!options->hasProperties() || !options->properties().size()) {
if (properties.IsEmpty()) {
return ScriptPromise::Reject(script_state,
V8ThrowException::CreateTypeError(
script_state->GetIsolate(),
......@@ -105,7 +106,7 @@ ScriptPromise ContactsManager::select(ScriptState* script_state,
bool include_emails = false;
bool include_tel = false;
for (const String& property : options->properties()) {
for (const String& property : properties) {
if (property == "name")
include_names = true;
else if (property == "email")
......
......@@ -10,6 +10,7 @@
#include "third_party/blink/renderer/modules/contacts_picker/contacts_select_options.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
namespace blink {
......@@ -26,6 +27,7 @@ class ContactsManager final : public ScriptWrappable {
// Web-exposed function defined in the IDL file.
ScriptPromise select(ScriptState* script_state,
const Vector<String>& properties,
ContactsSelectOptions* options);
private:
......
......@@ -9,5 +9,5 @@
SecureContext,
RuntimeEnabled=ContactsManager
] interface ContactsManager {
[CallWith=ScriptState] Promise<sequence<ContactInfo>> select(ContactsSelectOptions options);
[CallWith=ScriptState] Promise<sequence<ContactInfo>> select(sequence<ContactProperty> properties, optional ContactsSelectOptions options);
};
......@@ -7,6 +7,5 @@
enum ContactProperty { "email", "name", "tel" };
dictionary ContactsSelectOptions {
sequence<ContactProperty> properties;
boolean multiple = false;
};
......@@ -8,5 +8,5 @@
Exposed=Window,
ImplementedAs=NavigatorContacts
] partial interface Navigator {
[SecureContext, RuntimeEnabled=ContactsManager] readonly attribute ContactsManager contacts;
[SecureContext, SameObject, RuntimeEnabled=ContactsManager] readonly attribute ContactsManager contacts;
};
......@@ -2,13 +2,8 @@
'use strict';
window.onload = function() {
navigator.contacts.select({
multiple: "true",
properties: ['name', 'email']
}).then(results => {
parent.postMessage({ errorMsg: '' }, '*');
}).catch(exception => {
parent.postMessage({ errorMsg: exception.toString() }, '*');
});
navigator.contacts.select(['name', 'email'], { multiple: true })
.then(results => parent.postMessage({ errorMsg: '' }, '*'))
.catch(exception => parent.postMessage({ errorMsg: exception.toString() }, '*'));
}
</script>
......@@ -35,7 +35,7 @@ test(() => {
promise_test(async () => {
await expectTypeError(() =>
navigator.contacts.select({ properties: ['name'] }));
navigator.contacts.select(['name']));
}, 'The Contact API requires a user gesture')
......@@ -44,17 +44,17 @@ promise_test(async () => {
// At least one property must be provided.
await expectTypeError(() => navigator.contacts.select());
await expectTypeError(() => navigator.contacts.select({ properties: [] }));
await expectTypeError(() => navigator.contacts.select([]));
// Per WebIDL parsing, no invalid values may be provided.
await expectTypeError(() =>
navigator.contacts.select({ properties: [''] }));
navigator.contacts.select(['']));
await expectTypeError(() =>
navigator.contacts.select({ properties: ['foo'] }));
navigator.contacts.select(['foo']));
await expectTypeError(() =>
navigator.contacts.select({ properties: ['name', 'photo'] }));
navigator.contacts.select(['name', 'photo']));
}, 'The Contact API requires at least one valid property to be provided');
}, 'The Contact API requires valid properties to be provided');
promise_test(async () => {
triggerUserGesture();
......@@ -64,8 +64,7 @@ promise_test(async () => {
return { contacts: null };
});
await expectTypeError(() =>
navigator.contacts.select({ properties: ['name'] }));
await expectTypeError(() => navigator.contacts.select(['name']));
}, 'The Contact API can fail when the selector cannot be opened');
......@@ -83,10 +82,7 @@ promise_test(async () => {
});
await expectTypeError(() => {
return navigator.contacts.select({
properties: ['name', 'email'],
multiple: true
});
return navigator.contacts.select(['name', 'email'], { multiple: true });
});
assert_not_equals(storedOptions, null);
......@@ -110,10 +106,7 @@ promise_test(async () => {
};
});
const results = await navigator.contacts.select({
properties: ['name', 'email', 'tel'],
multiple: true
});
const results = await navigator.contacts.select(['name', 'email', 'tel'], { multiple: true });
assert_equals(results.length, 2);
......@@ -153,9 +146,7 @@ promise_test(async () => {
};
});
const results = await navigator.contacts.select({
properties: ['name']
});
const results = await navigator.contacts.select(['name']);
assert_equals(results.length, 1);
......
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