Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(704)

Unified Diff: third_party/WebKit/Source/modules/webshare/NavigatorShare.cpp

Issue 1806253002: Added Web Share (navigator.share) experimental web API (stub). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/webshare/NavigatorShare.cpp
diff --git a/third_party/WebKit/Source/modules/webshare/NavigatorShare.cpp b/third_party/WebKit/Source/modules/webshare/NavigatorShare.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..ec369ed4a63aa1894dc9e65b449d8efed9ce8b94
--- /dev/null
+++ b/third_party/WebKit/Source/modules/webshare/NavigatorShare.cpp
@@ -0,0 +1,107 @@
+// Copyright 2016 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 "modules/webshare/NavigatorShare.h"
+
+#include "core/dom/DOMException.h"
+#include "core/dom/Document.h"
+#include "core/dom/ExceptionCode.h"
+#include "core/frame/LocalFrame.h"
+#include "core/frame/Navigator.h"
+#include "platform/mojo/MojoHelper.h"
+#include "public/platform/Platform.h"
+#include "public/platform/ServiceRegistry.h"
+
+namespace blink {
+
+class NavigatorShare::ShareClientImpl : public GarbageCollectedFinalized<ShareClientImpl> {
+public:
+ ShareClientImpl(NavigatorShare*, ScriptPromiseResolver*);
+
+ void callback(const String& error);
+
+ DEFINE_INLINE_TRACE()
+ {
+ visitor->trace(m_navigator);
+ visitor->trace(m_resolver);
+ }
+
+private:
+ WeakMember<NavigatorShare> m_navigator;
+ Member<ScriptPromiseResolver> m_resolver;
+};
+
+NavigatorShare::ShareClientImpl::ShareClientImpl(NavigatorShare* navigator_share, ScriptPromiseResolver* resolver)
+ : m_navigator(navigator_share)
+ , m_resolver(resolver)
+{
+}
+
+void NavigatorShare::ShareClientImpl::callback(const String& error)
+{
+ if (m_navigator)
+ m_navigator->m_clients.remove(this);
+
+ if (error.isNull()) {
+ m_resolver->resolve();
+ } else {
+ // TODO(mgiuca): Work out which error type to use.
+ m_resolver->reject(DOMException::create(AbortError, error));
+ }
+}
+
+NavigatorShare::~NavigatorShare() = default;
+
+NavigatorShare& NavigatorShare::from(Navigator& navigator)
+{
+ NavigatorShare* supplement = static_cast<NavigatorShare*>(Supplement<Navigator>::from(navigator, supplementName()));
+ if (!supplement) {
+ supplement = new NavigatorShare();
+ provideTo(navigator, supplementName(), supplement);
+ }
+ return *supplement;
+}
+
+DEFINE_TRACE(NavigatorShare)
+{
+ visitor->trace(m_clients);
+ Supplement<Navigator>::trace(visitor);
+}
+
+NavigatorShare::NavigatorShare()
+{
+}
+
+const char* NavigatorShare::supplementName()
+{
+ return "NavigatorShare";
+}
+
+ScriptPromise NavigatorShare::share(ScriptState* scriptState, const String& title, const String& text)
+{
+ if (!m_service) {
+ Document* doc = toDocument(scriptState->getExecutionContext());
+ DCHECK(doc);
+ LocalFrame* frame = doc->frame();
+ DCHECK(frame);
+ frame->serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_service));
+ DCHECK(m_service);
+ }
+
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
+ ShareClientImpl* client = new ShareClientImpl(this, resolver);
+ m_clients.add(client);
+ ScriptPromise promise = resolver->promise();
+
+ m_service->Share(title, text, createBaseCallback(bind<const String&>(&ShareClientImpl::callback, client)));
+
+ return promise;
+}
+
+ScriptPromise NavigatorShare::share(ScriptState* scriptState, Navigator& navigator, const String& title, const String& text)
+{
+ return from(navigator).share(scriptState, title, text);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698