Commit b19e8afc authored by Peter Beverloo's avatar Peter Beverloo Committed by Commit Bot

Align the Contact API with the proposal

This implements the following two changes in Blink:
  https://github.com/beverloo/contact-api/commit/ade45e5
  https://github.com/beverloo/contact-api/commit/914261f

TBR=haraken@ (moving a file in modules_idl_files.gni)

Bug: 860467
Change-Id: I81dde8fdbd06728164a65ea570e7606f97bd9d77
Reviewed-on: https://chromium-review.googlesource.com/c/1430104
Commit-Queue: Peter Beverloo <peter@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Cr-Commit-Position: refs/heads/master@{#625321}
parent a50c1914
...@@ -6,8 +6,6 @@ import("//third_party/blink/renderer/modules/modules.gni") ...@@ -6,8 +6,6 @@ import("//third_party/blink/renderer/modules/modules.gni")
blink_modules_sources("contacts_picker") { blink_modules_sources("contacts_picker") {
sources = [ sources = [
"contact_info.cc",
"contact_info.h",
"contacts_manager.cc", "contacts_manager.cc",
"contacts_manager.h", "contacts_manager.h",
"navigator_contacts.cc", "navigator_contacts.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/contacts_picker/contact_info.h"
namespace blink {
ContactInfo::ContactInfo(base::Optional<Vector<String>> name,
base::Optional<Vector<String>> email,
base::Optional<Vector<String>> tel)
: name_(std::move(name)), email_(std::move(email)), tel_(std::move(tel)) {}
ContactInfo::~ContactInfo() = default;
const Vector<String> ContactInfo::name(bool& is_null) const {
is_null = !name_.has_value();
return is_null ? Vector<String>() : name_.value();
}
const Vector<String> ContactInfo::email(bool& is_null) const {
is_null = !email_.has_value();
return is_null ? Vector<String>() : email_.value();
}
const Vector<String> ContactInfo::tel(bool& is_null) const {
is_null = !tel_.has_value();
return is_null ? Vector<String>() : tel_.value();
}
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CONTACTS_PICKER_CONTACT_INFO_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_CONTACTS_PICKER_CONTACT_INFO_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/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
namespace blink {
// Represents an individual Contact in the Contacts Picker.
class ContactInfo final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
ContactInfo(base::Optional<Vector<String>> name,
base::Optional<Vector<String>> email,
base::Optional<Vector<String>> tel);
~ContactInfo() override;
// Web-exposed attributes defined in the IDL file.
const Vector<String> name(bool& is_null) const;
const Vector<String> email(bool& is_null) const;
const Vector<String> tel(bool& is_null) const;
private:
base::Optional<Vector<String>> name_;
base::Optional<Vector<String>> email_;
base::Optional<Vector<String>> tel_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_CONTACTS_PICKER_CONTACT_INFO_H_
...@@ -4,12 +4,8 @@ ...@@ -4,12 +4,8 @@
// https://github.com/beverloo/contact-api // https://github.com/beverloo/contact-api
[ dictionary ContactInfo {
SecureContext, sequence<USVString> name;
Exposed=Window, sequence<USVString> email;
RuntimeEnabled=ContactsManager sequence<USVString> tel;
] interface ContactInfo {
readonly attribute FrozenArray<DOMString>? name;
readonly attribute FrozenArray<DOMString>? email;
readonly attribute FrozenArray<DOMString>? tel;
}; };
...@@ -26,25 +26,39 @@ struct TypeConverter<blink::ContactInfo*, blink::mojom::blink::ContactInfoPtr> { ...@@ -26,25 +26,39 @@ struct TypeConverter<blink::ContactInfo*, blink::mojom::blink::ContactInfoPtr> {
blink::ContactInfo* blink::ContactInfo*
TypeConverter<blink::ContactInfo*, blink::mojom::blink::ContactInfoPtr>:: TypeConverter<blink::ContactInfo*, blink::mojom::blink::ContactInfoPtr>::
Convert(const blink::mojom::blink::ContactInfoPtr& contact) { Convert(const blink::mojom::blink::ContactInfoPtr& contact) {
Vector<String> names; blink::ContactInfo* contact_info = blink::ContactInfo::Create();
Vector<String> emails;
Vector<String> numbers;
if (contact->name.has_value()) { if (contact->name.has_value()) {
for (const auto& name : *contact->name) Vector<String> names;
names.ReserveInitialCapacity(contact->name->size());
for (const String& name : *contact->name)
names.push_back(name); names.push_back(name);
contact_info->setName(names);
} }
if (contact->email.has_value()) { if (contact->email.has_value()) {
for (const auto& email : *contact->email) Vector<String> emails;
emails.ReserveInitialCapacity(contact->email->size());
for (const String& email : *contact->email)
emails.push_back(email); emails.push_back(email);
contact_info->setEmail(emails);
} }
if (contact->tel.has_value()) { if (contact->tel.has_value()) {
for (const auto& number : *contact->tel) Vector<String> numbers;
numbers.ReserveInitialCapacity(contact->tel->size());
for (const String& number : *contact->tel)
numbers.push_back(number); numbers.push_back(number);
contact_info->setTel(numbers);
} }
return blink::MakeGarbageCollected<blink::ContactInfo>(
names.IsEmpty() ? base::Optional<Vector<String>>() : names, return contact_info;
emails.IsEmpty() ? base::Optional<Vector<String>>() : emails,
numbers.IsEmpty() ? base::Optional<Vector<String>>() : numbers);
} }
} // namespace mojo } // namespace mojo
...@@ -66,39 +80,34 @@ mojom::blink::ContactsManagerPtr& ContactsManager::GetContactsManager( ...@@ -66,39 +80,34 @@ mojom::blink::ContactsManagerPtr& ContactsManager::GetContactsManager(
ScriptPromise ContactsManager::select(ScriptState* script_state, ScriptPromise ContactsManager::select(ScriptState* script_state,
ContactsSelectOptions* options) { ContactsSelectOptions* options) {
if (!options->hasProperties() || !options->properties().size()) {
return ScriptPromise::Reject(script_state,
V8ThrowException::CreateTypeError(
script_state->GetIsolate(),
"At least one property must be provided"));
}
ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
ScriptPromise promise = resolver->Promise(); ScriptPromise promise = resolver->Promise();
if (!options->hasProperties()) { bool include_names = false;
resolver->Reject(DOMException::Create( bool include_emails = false;
DOMExceptionCode::kAbortError, "Dictionary must contain 'properties'")); bool include_tel = false;
return promise;
}
bool names = false;
bool emails = false;
bool tel = false;
for (const String& property : options->properties()) { for (const String& property : options->properties()) {
if (property == "name") if (property == "name")
names = true; include_names = true;
else if (property == "email") else if (property == "email")
emails = true; include_emails = true;
else if (property == "tel") else if (property == "tel")
tel = true; include_tel = true;
}
if (!names && !emails && !tel) {
resolver->Reject(
DOMException::Create(DOMExceptionCode::kAbortError,
"'properties' must contain at least one entry"));
return promise;
} }
// TODO(finnur): Figure out empty-array vs null.
GetContactsManager(script_state) GetContactsManager(script_state)
->Select(options->multiple(), names, emails, tel, ->Select(options->multiple(), include_names, include_emails, include_tel,
WTF::Bind(&ContactsManager::OnContactsSelected, WTF::Bind(&ContactsManager::OnContactsSelected,
WrapPersistent(this), WrapPersistent(resolver))); WrapPersistent(this), WrapPersistent(resolver)));
return promise; return promise;
} }
......
...@@ -9,5 +9,5 @@ ...@@ -9,5 +9,5 @@
SecureContext, SecureContext,
RuntimeEnabled=ContactsManager RuntimeEnabled=ContactsManager
] interface ContactsManager { ] interface ContactsManager {
[CallWith=ScriptState] Promise<FrozenArray<ContactInfo>> select(ContactsSelectOptions options); [CallWith=ScriptState] Promise<sequence<ContactInfo>> select(ContactsSelectOptions options);
}; };
...@@ -96,7 +96,6 @@ modules_idl_files = ...@@ -96,7 +96,6 @@ modules_idl_files =
"canvas/imagebitmap/image_bitmap_rendering_context.idl", "canvas/imagebitmap/image_bitmap_rendering_context.idl",
"canvas/offscreencanvas2d/offscreen_canvas_rendering_context_2d.idl", "canvas/offscreencanvas2d/offscreen_canvas_rendering_context_2d.idl",
"clipboard/clipboard.idl", "clipboard/clipboard.idl",
"contacts_picker/contact_info.idl",
"contacts_picker/contacts_manager.idl", "contacts_picker/contacts_manager.idl",
"cookie_store/cookie_change_event.idl", "cookie_store/cookie_change_event.idl",
"cookie_store/cookie_store.idl", "cookie_store/cookie_store.idl",
...@@ -504,6 +503,7 @@ modules_dictionary_idl_files = ...@@ -504,6 +503,7 @@ modules_dictionary_idl_files =
"canvas/canvas2d/canvas_rendering_context_2d_settings.idl", "canvas/canvas2d/canvas_rendering_context_2d_settings.idl",
"canvas/canvas2d/hit_region_options.idl", "canvas/canvas2d/hit_region_options.idl",
"canvas/htmlcanvas/canvas_context_creation_attributes_module.idl", "canvas/htmlcanvas/canvas_context_creation_attributes_module.idl",
"contacts_picker/contact_info.idl",
"contacts_picker/contacts_select_options.idl", "contacts_picker/contacts_select_options.idl",
"cookie_store/cookie_change_event_init.idl", "cookie_store/cookie_change_event_init.idl",
"cookie_store/cookie_list_item.idl", "cookie_store/cookie_list_item.idl",
......
...@@ -5,7 +5,5 @@ ...@@ -5,7 +5,5 @@
test(function() { test(function() {
assert_true('contacts' in navigator, assert_true('contacts' in navigator,
'navigator.contacts exists in navigator.'); 'navigator.contacts exists in navigator.');
assert_true('ContactInfo' in window,
'ContactInfo exists in window.');
}, 'navigator.contacts IDL test'); }, 'navigator.contacts IDL test');
</script> </script>
...@@ -1129,12 +1129,6 @@ interface ConstantSourceNode : AudioScheduledSourceNode ...@@ -1129,12 +1129,6 @@ interface ConstantSourceNode : AudioScheduledSourceNode
attribute @@toStringTag attribute @@toStringTag
getter offset getter offset
method constructor method constructor
interface ContactInfo
attribute @@toStringTag
getter email
getter name
getter tel
method constructor
interface ContactsManager interface ContactsManager
attribute @@toStringTag attribute @@toStringTag
method constructor method constructor
......
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