OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/webshare/NavigatorShare.h" |
| 6 |
| 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ExceptionCode.h" |
| 10 #include "core/frame/LocalFrame.h" |
| 11 #include "core/frame/Navigator.h" |
| 12 #include "platform/mojo/MojoHelper.h" |
| 13 #include "public/platform/Platform.h" |
| 14 #include "public/platform/ServiceRegistry.h" |
| 15 |
| 16 namespace blink { |
| 17 |
| 18 class NavigatorShare::ShareClientImpl : public GarbageCollectedFinalized<ShareCl
ientImpl> { |
| 19 public: |
| 20 ShareClientImpl(NavigatorShare*, ScriptPromiseResolver*); |
| 21 |
| 22 void callback(const String& error); |
| 23 |
| 24 DEFINE_INLINE_TRACE() |
| 25 { |
| 26 visitor->trace(m_navigator); |
| 27 visitor->trace(m_resolver); |
| 28 } |
| 29 |
| 30 private: |
| 31 WeakMember<NavigatorShare> m_navigator; |
| 32 Member<ScriptPromiseResolver> m_resolver; |
| 33 }; |
| 34 |
| 35 NavigatorShare::ShareClientImpl::ShareClientImpl(NavigatorShare* navigator_share
, ScriptPromiseResolver* resolver) |
| 36 : m_navigator(navigator_share) |
| 37 , m_resolver(resolver) |
| 38 { |
| 39 } |
| 40 |
| 41 void NavigatorShare::ShareClientImpl::callback(const String& error) |
| 42 { |
| 43 if (m_navigator) |
| 44 m_navigator->m_clients.remove(this); |
| 45 |
| 46 if (error.isNull()) { |
| 47 m_resolver->resolve(); |
| 48 } else { |
| 49 // TODO(mgiuca): Work out which error type to use. |
| 50 m_resolver->reject(DOMException::create(AbortError, error)); |
| 51 } |
| 52 } |
| 53 |
| 54 NavigatorShare::~NavigatorShare() = default; |
| 55 |
| 56 NavigatorShare& NavigatorShare::from(Navigator& navigator) |
| 57 { |
| 58 NavigatorShare* supplement = static_cast<NavigatorShare*>(Supplement<Navigat
or>::from(navigator, supplementName())); |
| 59 if (!supplement) { |
| 60 supplement = new NavigatorShare(); |
| 61 provideTo(navigator, supplementName(), supplement); |
| 62 } |
| 63 return *supplement; |
| 64 } |
| 65 |
| 66 DEFINE_TRACE(NavigatorShare) |
| 67 { |
| 68 visitor->trace(m_clients); |
| 69 Supplement<Navigator>::trace(visitor); |
| 70 } |
| 71 |
| 72 NavigatorShare::NavigatorShare() |
| 73 { |
| 74 } |
| 75 |
| 76 const char* NavigatorShare::supplementName() |
| 77 { |
| 78 return "NavigatorShare"; |
| 79 } |
| 80 |
| 81 ScriptPromise NavigatorShare::share(ScriptState* scriptState, const String& titl
e, const String& text) |
| 82 { |
| 83 if (!m_service) { |
| 84 Document* doc = toDocument(scriptState->getExecutionContext()); |
| 85 DCHECK(doc); |
| 86 LocalFrame* frame = doc->frame(); |
| 87 DCHECK(frame); |
| 88 frame->serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_servi
ce)); |
| 89 DCHECK(m_service); |
| 90 } |
| 91 |
| 92 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 93 ShareClientImpl* client = new ShareClientImpl(this, resolver); |
| 94 m_clients.add(client); |
| 95 ScriptPromise promise = resolver->promise(); |
| 96 |
| 97 m_service->Share(title, text, convertToBaseCallback(WTF::bind(&ShareClientIm
pl::callback, wrapPersistent(client)))); |
| 98 |
| 99 return promise; |
| 100 } |
| 101 |
| 102 ScriptPromise NavigatorShare::share(ScriptState* scriptState, Navigator& navigat
or, const String& title, const String& text) |
| 103 { |
| 104 return from(navigator).share(scriptState, title, text); |
| 105 } |
| 106 |
| 107 } // namespace blink |
OLD | NEW |