Commit 8872fd61 authored by Hitoshi Yoshida's avatar Hitoshi Yoshida Committed by Commit Bot

Refactor CheckForTypeError in NavigatorShare

CheckForTypeError returned a String, and it is non-empty if TypeError
is expected.  This style is not obvious.
This CL makes it to return what canShare() returns, and do not process
URL if |data.url| is not present.


Bug: 839389
Change-Id: Ie7eede5e33b16947aeffcb5c144f95940bb5d82a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2237213
Commit-Queue: Hitoshi Yoshida <peria@chromium.org>
Commit-Queue: Kentaro Hara <haraken@chromium.org>
Auto-Submit: Hitoshi Yoshida <peria@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#776894}
parent 947b1951
...@@ -49,35 +49,46 @@ String ErrorToString(mojom::blink::ShareError error) { ...@@ -49,35 +49,46 @@ String ErrorToString(mojom::blink::ShareError error) {
return String(); return String();
} }
bool HasFiles(const ShareData& share_data) { bool HasFiles(const ShareData& data) {
if (!RuntimeEnabledFeatures::WebShareV2Enabled() || !share_data.hasFiles()) if (!RuntimeEnabledFeatures::WebShareV2Enabled() || !data.hasFiles())
return false; return false;
const HeapVector<Member<File>>& files = share_data.files(); return !data.files().IsEmpty();
return !files.IsEmpty();
} }
// Returns a message for a TypeError if share(share_data) would reject with // Returns true unless |share(data)| would reject with TypeError.
// TypeError. https://w3c.github.io/web-share/level-2/#canshare-method // Populates |url| with the result of running the URL parser on |data.url|.
// Otherwise returns an empty string. // If the return value is false and |exception_state| is non null, throws
// Populates full_url with the result of running the URL parser on // TypeError.
// share_data.url //
String CheckForTypeError(const LocalDOMWindow& window, // https://w3c.github.io/web-share/level-2/#canshare-method
const ShareData& share_data, // https://w3c.github.io/web-share/level-2/#share-method
KURL* full_url) { bool CanShareInternal(const LocalDOMWindow& window,
if (!share_data.hasTitle() && !share_data.hasText() && !share_data.hasUrl() && const ShareData& data,
!HasFiles(share_data)) { KURL& url,
return "No known share data fields supplied. If using only new fields " ExceptionState* exception_state) {
"(other than title, text and url), you must feature-detect " if (!data.hasTitle() && !data.hasText() && !data.hasUrl() &&
"them first."; !HasFiles(data)) {
if (exception_state) {
exception_state->ThrowTypeError(
"No known share data fields supplied. If using only new fields "
"(other than title, text and url), you must feature-detect "
"them first.");
}
return false;
} }
*full_url = window.CompleteURL(share_data.url()); if (data.hasUrl()) {
if (!full_url->IsNull() && !full_url->IsValid()) { url = window.CompleteURL(data.url());
return "Invalid URL"; if (!url.IsValid()) {
if (exception_state) {
exception_state->ThrowTypeError("Invalid URL");
}
return false;
}
} }
return g_empty_string; return true;
} }
} // namespace } // namespace
...@@ -172,22 +183,22 @@ NavigatorShare::NavigatorShare() ...@@ -172,22 +183,22 @@ NavigatorShare::NavigatorShare()
const char NavigatorShare::kSupplementName[] = "NavigatorShare"; const char NavigatorShare::kSupplementName[] = "NavigatorShare";
bool NavigatorShare::canShare(ScriptState* script_state, bool NavigatorShare::canShare(ScriptState* script_state,
const ShareData* share_data) { const ShareData* data) {
if (!script_state->ContextIsValid()) if (!script_state->ContextIsValid())
return false; return false;
LocalDOMWindow* window = LocalDOMWindow::From(script_state); LocalDOMWindow* window = LocalDOMWindow::From(script_state);
KURL full_url; KURL unused_url;
return CheckForTypeError(*window, *share_data, &full_url).IsEmpty(); return CanShareInternal(*window, *data, unused_url, nullptr);
} }
bool NavigatorShare::canShare(ScriptState* script_state, bool NavigatorShare::canShare(ScriptState* script_state,
Navigator& navigator, Navigator& navigator,
const ShareData* share_data) { const ShareData* data) {
return From(navigator).canShare(script_state, share_data); return From(navigator).canShare(script_state, data);
} }
ScriptPromise NavigatorShare::share(ScriptState* script_state, ScriptPromise NavigatorShare::share(ScriptState* script_state,
const ShareData* share_data, const ShareData* data,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!script_state->ContextIsValid()) { if (!script_state->ContextIsValid()) {
exception_state.ThrowDOMException( exception_state.ThrowDOMException(
...@@ -198,10 +209,9 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state, ...@@ -198,10 +209,9 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state,
} }
LocalDOMWindow* window = LocalDOMWindow::From(script_state); LocalDOMWindow* window = LocalDOMWindow::From(script_state);
KURL full_url; KURL url;
String error_message = CheckForTypeError(*window, *share_data, &full_url); if (!CanShareInternal(*window, *data, url, &exception_state)) {
if (!error_message.IsEmpty()) { DCHECK(exception_state.HadException());
exception_state.ThrowTypeError(error_message);
return ScriptPromise(); return ScriptPromise();
} }
...@@ -222,7 +232,7 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state, ...@@ -222,7 +232,7 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state,
DCHECK(service_remote_.is_bound()); DCHECK(service_remote_.is_bound());
} }
bool has_files = HasFiles(*share_data); bool has_files = HasFiles(*data);
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state); auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ShareClientImpl* client = ShareClientImpl* client =
MakeGarbageCollected<ShareClientImpl>(this, has_files, resolver); MakeGarbageCollected<ShareClientImpl>(this, has_files, resolver);
...@@ -232,8 +242,8 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state, ...@@ -232,8 +242,8 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state,
WTF::Vector<mojom::blink::SharedFilePtr> files; WTF::Vector<mojom::blink::SharedFilePtr> files;
uint64_t total_bytes = 0; uint64_t total_bytes = 0;
if (has_files) { if (has_files) {
files.ReserveInitialCapacity(share_data->files().size()); files.ReserveInitialCapacity(data->files().size());
for (const blink::Member<blink::File>& file : share_data->files()) { for (const blink::Member<blink::File>& file : data->files()) {
total_bytes += file->size(); total_bytes += file->size();
files.push_back(mojom::blink::SharedFile::New(file->name(), files.push_back(mojom::blink::SharedFile::New(file->name(),
file->GetBlobDataHandle())); file->GetBlobDataHandle()));
...@@ -248,9 +258,8 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state, ...@@ -248,9 +258,8 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state,
} }
service_remote_->Share( service_remote_->Share(
share_data->hasTitle() ? share_data->title() : g_empty_string, data->hasTitle() ? data->title() : g_empty_string,
share_data->hasText() ? share_data->text() : g_empty_string, full_url, data->hasText() ? data->text() : g_empty_string, url, std::move(files),
std::move(files),
WTF::Bind(&ShareClientImpl::Callback, WrapPersistent(client))); WTF::Bind(&ShareClientImpl::Callback, WrapPersistent(client)));
return promise; return promise;
...@@ -258,9 +267,9 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state, ...@@ -258,9 +267,9 @@ ScriptPromise NavigatorShare::share(ScriptState* script_state,
ScriptPromise NavigatorShare::share(ScriptState* script_state, ScriptPromise NavigatorShare::share(ScriptState* script_state,
Navigator& navigator, Navigator& navigator,
const ShareData* share_data, const ShareData* data,
ExceptionState& exception_state) { ExceptionState& exception_state) {
return From(navigator).share(script_state, share_data, exception_state); return From(navigator).share(script_state, data, exception_state);
} }
void NavigatorShare::OnConnectionError() { void NavigatorShare::OnConnectionError() {
......
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