summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/modules/beacon/NavigatorBeacon.cpp
blob: f08108c481715dd4ef536f1041195155e0fb1ffa (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 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 "config.h"
#include "modules/beacon/NavigatorBeacon.h"

#include "bindings/core/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
#include "core/fileapi/Blob.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Settings.h"
#include "core/frame/UseCounter.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "core/html/DOMFormData.h"
#include "core/loader/BeaconLoader.h"
#include "wtf/ArrayBufferView.h"

namespace blink {

NavigatorBeacon::NavigatorBeacon(Navigator& navigator)
    : m_transmittedBytes(0)
    , m_navigator(navigator)
{
}

const char* NavigatorBeacon::supplementName()
{
    return "NavigatorBeacon";
}

NavigatorBeacon& NavigatorBeacon::from(Navigator& navigator)
{
    NavigatorBeacon* supplement = static_cast<NavigatorBeacon*>(WillBeHeapSupplement<Navigator>::from(navigator, supplementName()));
    if (!supplement) {
        supplement = new NavigatorBeacon(navigator);
        provideTo(navigator, supplementName(), adoptPtrWillBeNoop(supplement));
    }
    return *supplement;
}

bool NavigatorBeacon::canSendBeacon(ExecutionContext* context, const KURL& url, ExceptionState& exceptionState)
{
    if (!url.isValid()) {
        exceptionState.throwDOMException(SyntaxError, "The URL argument is ill-formed or unsupported.");
        return false;
    }
    // For now, only support HTTP and related.
    if (!url.protocolIsInHTTPFamily()) {
        exceptionState.throwDOMException(SyntaxError, "Beacons are only supported over HTTP(S).");
        return false;
    }
    // FIXME: CSP is not enforced on redirects, crbug.com/372197
    if (!ContentSecurityPolicy::shouldBypassMainWorld(context) && !context->contentSecurityPolicy()->allowConnectToSource(url)) {
        // We can safely expose the URL to JavaScript, as these checks happen synchronously before redirection. JavaScript receives no new information.
        exceptionState.throwSecurityError("Refused to send beacon to '" + url.elidedString() + "' because it violates the document's Content Security Policy.");
        return false;
    }

    // Do not allow sending Beacons over a Navigator that is detached.
    if (!m_navigator.frame() || !m_navigator.frame()->client())
        return false;

    return true;
}

int NavigatorBeacon::maxAllowance() const
{
    const Settings* settings = m_navigator.frame()->settings();
    if (settings) {
        int maxAllowed = settings->maxBeaconTransmission();
        if (maxAllowed < m_transmittedBytes)
            return 0;
        return maxAllowed - m_transmittedBytes;
    }
    return m_transmittedBytes;
}

bool NavigatorBeacon::beaconResult(ExecutionContext* context, bool allowed, int sentBytes)
{
    if (allowed) {
        ASSERT(sentBytes >= 0);
        m_transmittedBytes += sentBytes;
    } else {
        UseCounter::count(context, UseCounter::SendBeaconQuotaExceeded);
    }
    return allowed;
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, Navigator& navigator, const String& urlstring, const String& data, ExceptionState& exceptionState)
{
    return NavigatorBeacon::from(navigator).sendBeacon(context, urlstring, data, exceptionState);
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, const String& urlstring, const String& data, ExceptionState& exceptionState)
{
    KURL url = context->completeURL(urlstring);
    if (!canSendBeacon(context, url, exceptionState))
        return false;

    int bytes = 0;
    bool result = BeaconLoader::sendBeacon(m_navigator.frame(), maxAllowance(), url, data, bytes);
    return beaconResult(context, result, bytes);
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, Navigator& navigator, const String& urlstring, PassRefPtr<ArrayBufferView> data, ExceptionState& exceptionState)
{
    return NavigatorBeacon::from(navigator).sendBeacon(context, urlstring, data, exceptionState);
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, const String& urlstring, PassRefPtr<ArrayBufferView> data, ExceptionState& exceptionState)
{
    KURL url = context->completeURL(urlstring);
    if (!canSendBeacon(context, url, exceptionState))
        return false;

    int bytes = 0;
    bool result = BeaconLoader::sendBeacon(m_navigator.frame(), maxAllowance(), url, data, bytes);
    return beaconResult(context, result, bytes);
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, Navigator& navigator, const String& urlstring, Blob* data, ExceptionState& exceptionState)
{
    return NavigatorBeacon::from(navigator).sendBeacon(context, urlstring, data, exceptionState);
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, const String& urlstring, Blob* data, ExceptionState& exceptionState)
{
    KURL url = context->completeURL(urlstring);
    if (!canSendBeacon(context, url, exceptionState))
        return false;

    int bytes = 0;
    bool result = BeaconLoader::sendBeacon(m_navigator.frame(), maxAllowance(), url, data, bytes);
    return beaconResult(context, result, bytes);
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, Navigator& navigator, const String& urlstring, PassRefPtrWillBeRawPtr<DOMFormData> data, ExceptionState& exceptionState)
{
    return NavigatorBeacon::from(navigator).sendBeacon(context, urlstring, data, exceptionState);
}

bool NavigatorBeacon::sendBeacon(ExecutionContext* context, const String& urlstring, PassRefPtrWillBeRawPtr<DOMFormData> data, ExceptionState& exceptionState)
{
    KURL url = context->completeURL(urlstring);
    if (!canSendBeacon(context, url, exceptionState))
        return false;

    int bytes = 0;
    bool result = BeaconLoader::sendBeacon(m_navigator.frame(), maxAllowance(), url, data, bytes);
    return beaconResult(context, result, bytes);
}

} // namespace blink