File: NavigatorShare.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (142 lines) | stat: -rw-r--r-- 4,506 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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 "modules/webshare/ShareData.h"
#include "platform/UserGestureIndicator.h"
#include "platform/mojo/MojoHelper.h"
#include "public/platform/InterfaceProvider.h"
#include "public/platform/Platform.h"

namespace blink {

class NavigatorShare::ShareClientImpl final
    : public GarbageCollected<ShareClientImpl> {
 public:
  ShareClientImpl(NavigatorShare*, ScriptPromiseResolver*);

  void callback(const String& error);

  void onConnectionError();

  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));
  }
}

void NavigatorShare::ShareClientImpl::onConnectionError() {
  m_resolver->reject(
      DOMException::create(SecurityError, "WebShare is disabled."));
}

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 ShareData& shareData) {
  String errorMessage;
  if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) {
    DOMException* error = DOMException::create(SecurityError, errorMessage);
    return ScriptPromise::rejectWithDOMException(scriptState, error);
  }
  if (!UserGestureIndicator::utilizeUserGesture()) {
    DOMException* error = DOMException::create(
        SecurityError,
        "Must be handling a user gesture to perform a share request.");
    return ScriptPromise::rejectWithDOMException(scriptState, error);
  }

  Document* doc = toDocument(scriptState->getExecutionContext());
  DCHECK(doc);
  if (!m_service) {
    LocalFrame* frame = doc->frame();
    DCHECK(frame);
    frame->interfaceProvider()->getInterface(mojo::MakeRequest(&m_service));
    m_service.set_connection_error_handler(convertToBaseCallback(WTF::bind(
        &NavigatorShare::onConnectionError, wrapWeakPersistent(this))));
    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(shareData.hasTitle() ? shareData.title() : emptyString(),
                   shareData.hasText() ? shareData.text() : emptyString(),
                   doc->completeURL(shareData.url()),
                   convertToBaseCallback(WTF::bind(&ShareClientImpl::callback,
                                                   wrapPersistent(client))));

  return promise;
}

ScriptPromise NavigatorShare::share(ScriptState* scriptState,
                                    Navigator& navigator,
                                    const ShareData& shareData) {
  return from(navigator).share(scriptState, shareData);
}

void NavigatorShare::onConnectionError() {
  if (!Platform::current()) {
    // TODO(rockot): Clean this up once renderer shutdown sequence is fixed.
    return;
  }

  for (auto& client : m_clients) {
    client->onConnectionError();
  }
  m_clients.clear();
  m_service.reset();
}

}  // namespace blink