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