Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/fetch/ChannelInfo.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 "mozilla/dom/ChannelInfo.h"
8
#include "nsCOMPtr.h"
9
#include "nsContentUtils.h"
10
#include "nsIChannel.h"
11
#include "nsIDocument.h"
12
#include "nsIHttpChannel.h"
13
#include "nsSerializationHelper.h"
14
#include "mozilla/net/HttpBaseChannel.h"
15
#include "mozilla/ipc/ChannelInfo.h"
16
#include "nsNetUtil.h"
17
18
using namespace mozilla;
19
using namespace mozilla::dom;
20
21
void
22
ChannelInfo::InitFromDocument(nsIDocument* aDoc)
23
0
{
24
0
  MOZ_ASSERT(NS_IsMainThread());
25
0
  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
26
0
27
0
  nsCOMPtr<nsISupports> securityInfo = aDoc->GetSecurityInfo();
28
0
  if (securityInfo) {
29
0
    SetSecurityInfo(securityInfo);
30
0
  }
31
0
32
0
  mInited = true;
33
0
}
34
35
void
36
ChannelInfo::InitFromChannel(nsIChannel* aChannel)
37
0
{
38
0
  MOZ_ASSERT(NS_IsMainThread());
39
0
  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
40
0
41
0
  nsCOMPtr<nsISupports> securityInfo;
42
0
  aChannel->GetSecurityInfo(getter_AddRefs(securityInfo));
43
0
  if (securityInfo) {
44
0
    SetSecurityInfo(securityInfo);
45
0
  }
46
0
47
0
  mInited = true;
48
0
}
49
50
void
51
ChannelInfo::InitFromChromeGlobal(nsIGlobalObject* aGlobal)
52
0
{
53
0
  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
54
0
  MOZ_ASSERT(aGlobal);
55
0
56
0
  MOZ_RELEASE_ASSERT(
57
0
    nsContentUtils::IsSystemPrincipal(aGlobal->PrincipalOrNull()));
58
0
59
0
  mSecurityInfo.Truncate();
60
0
  mInited = true;
61
0
}
62
63
void
64
ChannelInfo::InitFromIPCChannelInfo(const mozilla::ipc::IPCChannelInfo& aChannelInfo)
65
0
{
66
0
  MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
67
0
68
0
  mSecurityInfo = aChannelInfo.securityInfo();
69
0
70
0
  mInited = true;
71
0
}
72
73
void
74
ChannelInfo::SetSecurityInfo(nsISupports* aSecurityInfo)
75
0
{
76
0
  MOZ_ASSERT(mSecurityInfo.IsEmpty(), "security info should only be set once");
77
0
  nsCOMPtr<nsISerializable> serializable = do_QueryInterface(aSecurityInfo);
78
0
  if (!serializable) {
79
0
    NS_WARNING("A non-serializable object was passed to InternalResponse::SetSecurityInfo");
80
0
    return;
81
0
  }
82
0
  NS_SerializeToString(serializable, mSecurityInfo);
83
0
}
84
85
nsresult
86
ChannelInfo::ResurrectInfoOnChannel(nsIChannel* aChannel)
87
0
{
88
0
  MOZ_ASSERT(NS_IsMainThread());
89
0
  MOZ_ASSERT(mInited);
90
0
91
0
  if (!mSecurityInfo.IsEmpty()) {
92
0
    nsCOMPtr<nsISupports> infoObj;
93
0
    nsresult rv = NS_DeserializeObject(mSecurityInfo, getter_AddRefs(infoObj));
94
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
95
0
      return rv;
96
0
    }
97
0
    nsCOMPtr<nsIHttpChannel> httpChannel =
98
0
      do_QueryInterface(aChannel);
99
0
    MOZ_ASSERT(httpChannel);
100
0
    net::HttpBaseChannel* httpBaseChannel =
101
0
      static_cast<net::HttpBaseChannel*>(httpChannel.get());
102
0
    rv = httpBaseChannel->OverrideSecurityInfo(infoObj);
103
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
104
0
      return rv;
105
0
    }
106
0
  }
107
0
108
0
  return NS_OK;
109
0
}
110
111
mozilla::ipc::IPCChannelInfo
112
ChannelInfo::AsIPCChannelInfo() const
113
0
{
114
0
  // This may be called when mInited is false, for example if we try to store
115
0
  // a synthesized Response object into the Cache.  Uninitialized and empty
116
0
  // ChannelInfo objects are indistinguishable at the IPC level, so this is
117
0
  // fine.
118
0
119
0
  IPCChannelInfo ipcInfo;
120
0
121
0
  ipcInfo.securityInfo() = mSecurityInfo;
122
0
123
0
  return ipcInfo;
124
0
}