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")
blink_modules_sources("contacts_picker") {
sources = [
"contact_info.cc",
"contact_info.h",
"contacts_manager.cc",
"contacts_manager.h",
"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 @@
// https://github.com/beverloo/contact-api
[
SecureContext,
Exposed=Window,
RuntimeEnabled=ContactsManager
] interface ContactInfo {
readonly attribute FrozenArray<DOMString>? name;
readonly attribute FrozenArray<DOMString>? email;
readonly attribute FrozenArray<DOMString>? tel;
dictionary ContactInfo {
sequence<USVString> name;
sequence<USVString> email;
sequence<USVString> tel;
};
......@@ -26,25 +26,39 @@ struct TypeConverter<blink::ContactInfo*, blink::mojom::blink::ContactInfoPtr> {
blink::ContactInfo*
TypeConverter<blink::ContactInfo*, blink::mojom::blink::ContactInfoPtr>::
Convert(const blink::mojom::blink::ContactInfoPtr& contact) {
Vector<String> names;
Vector<String> emails;
Vector<String> numbers;
blink::ContactInfo* contact_info = blink::ContactInfo::Create();
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);
contact_info->setName(names);
}
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);
contact_info->setEmail(emails);
}
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);
contact_info->setTel(numbers);
}
return blink::MakeGarbageCollected<blink::ContactInfo>(
names.IsEmpty() ? base::Optional<Vector<String>>() : names,
emails.IsEmpty() ? base::Optional<Vector<String>>() : emails,
numbers.IsEmpty() ? base::Optional<Vector<String>>() : numbers);
return contact_info;
}
} // namespace mojo
......@@ -66,39 +80,34 @@ mojom::blink::ContactsManagerPtr& ContactsManager::GetContactsManager(
ScriptPromise ContactsManager::select(ScriptState* script_state,
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);
ScriptPromise promise = resolver->Promise();
if (!options->hasProperties()) {
resolver->Reject(DOMException::Create(
DOMExceptionCode::kAbortError, "Dictionary must contain 'properties'"));
return promise;
}
bool include_names = false;
bool include_emails = false;
bool include_tel = false;
bool names = false;
bool emails = false;
bool tel = false;
for (const String& property : options->properties()) {
if (property == "name")
names = true;
include_names = true;
else if (property == "email")
emails = true;
include_emails = true;
else if (property == "tel")
tel = true;
}
if (!names && !emails && !tel) {
resolver->Reject(
DOMException::Create(DOMExceptionCode::kAbortError,
"'properties' must contain at least one entry"));
return promise;
include_tel = true;
}
// TODO(finnur): Figure out empty-array vs null.
GetContactsManager(script_state)
->Select(options->multiple(), names, emails, tel,
->Select(options->multiple(), include_names, include_emails, include_tel,
WTF::Bind(&ContactsManager::OnContactsSelected,
WrapPersistent(this), WrapPersistent(resolver)));
return promise;
}
......
......@@ -9,5 +9,5 @@
SecureContext,
RuntimeEnabled=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 =
"canvas/imagebitmap/image_bitmap_rendering_context.idl",
"canvas/offscreencanvas2d/offscreen_canvas_rendering_context_2d.idl",
"clipboard/clipboard.idl",
"contacts_picker/contact_info.idl",
"contacts_picker/contacts_manager.idl",
"cookie_store/cookie_change_event.idl",
"cookie_store/cookie_store.idl",
......@@ -504,6 +503,7 @@ modules_dictionary_idl_files =
"canvas/canvas2d/canvas_rendering_context_2d_settings.idl",
"canvas/canvas2d/hit_region_options.idl",
"canvas/htmlcanvas/canvas_context_creation_attributes_module.idl",
"contacts_picker/contact_info.idl",
"contacts_picker/contacts_select_options.idl",
"cookie_store/cookie_change_event_init.idl",
"cookie_store/cookie_list_item.idl",
......
......@@ -5,7 +5,5 @@
test(function() {
assert_true('contacts' in navigator,
'navigator.contacts exists in navigator.');
assert_true('ContactInfo' in window,
'ContactInfo exists in window.');
}, 'navigator.contacts IDL test');
</script>
......@@ -1129,12 +1129,6 @@ interface ConstantSourceNode : AudioScheduledSourceNode
attribute @@toStringTag
getter offset
method constructor
interface ContactInfo
attribute @@toStringTag
getter email
getter name
getter tel
method constructor
interface ContactsManager
attribute @@toStringTag
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