Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/serviceworkers/ServiceWorkerInterceptController.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ServiceWorkerInterceptController.h"
8
9
#include "mozilla/BasePrincipal.h"
10
#include "nsContentUtils.h"
11
#include "nsIChannel.h"
12
#include "ServiceWorkerManager.h"
13
14
namespace mozilla {
15
namespace dom {
16
17
NS_IMPL_ISUPPORTS(ServiceWorkerInterceptController, nsINetworkInterceptController)
18
19
NS_IMETHODIMP
20
ServiceWorkerInterceptController::ShouldPrepareForIntercept(nsIURI* aURI,
21
                                                            nsIChannel* aChannel,
22
                                                            bool* aShouldIntercept)
23
0
{
24
0
  *aShouldIntercept = false;
25
0
26
0
  nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
27
0
  if (!loadInfo) {
28
0
    return NS_OK;
29
0
  }
30
0
31
0
  // For subresource requests we base our decision solely on the client's
32
0
  // controller value.  Any settings that would have blocked service worker
33
0
  // access should have been set before the initial navigation created the
34
0
  // window.
35
0
  if (!nsContentUtils::IsNonSubresourceRequest(aChannel)) {
36
0
    const Maybe<ServiceWorkerDescriptor>& controller = loadInfo->GetController();
37
0
    *aShouldIntercept = controller.isSome();
38
0
    return NS_OK;
39
0
  }
40
0
41
0
  if (nsContentUtils::StorageAllowedForChannel(aChannel) !=
42
0
      nsContentUtils::StorageAccess::eAllow) {
43
0
    return NS_OK;
44
0
  }
45
0
46
0
  nsCOMPtr<nsIPrincipal> principal =
47
0
    BasePrincipal::CreateCodebasePrincipal(aURI,
48
0
                                           loadInfo->GetOriginAttributes());
49
0
50
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
51
0
  if (!swm) {
52
0
    return NS_OK;
53
0
  }
54
0
55
0
  // We're allowed to control a window, so check with the ServiceWorkerManager
56
0
  // for a matching service worker.
57
0
  *aShouldIntercept = swm->IsAvailable(principal, aURI);
58
0
  return NS_OK;
59
0
}
60
61
NS_IMETHODIMP
62
ServiceWorkerInterceptController::ChannelIntercepted(nsIInterceptedChannel* aChannel)
63
0
{
64
0
  // Note, do not cancel the interception here.  The caller will try to
65
0
  // ResetInterception() on error.
66
0
67
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
68
0
  if (!swm) {
69
0
    return NS_ERROR_FAILURE;
70
0
  }
71
0
72
0
  ErrorResult error;
73
0
  swm->DispatchFetchEvent(aChannel, error);
74
0
  if (NS_WARN_IF(error.Failed())) {
75
0
    return error.StealNSResult();
76
0
  }
77
0
78
0
  return NS_OK;
79
0
}
80
81
} // namespace dom
82
} // namespace mozilla