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 "ClientInfo.h"
8 
9 #include "mozilla/dom/ClientIPCTypes.h"
10 #include "mozilla/ipc/BackgroundUtils.h"
11 
12 namespace mozilla::dom {
13 
14 using mozilla::ipc::PrincipalInfo;
15 using mozilla::ipc::PrincipalInfoToPrincipal;
16 
ClientInfo(const nsID & aId,ClientType aType,const mozilla::ipc::PrincipalInfo & aPrincipalInfo,const TimeStamp & aCreationTime)17 ClientInfo::ClientInfo(const nsID& aId, ClientType aType,
18                        const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
19                        const TimeStamp& aCreationTime)
20     : mData(MakeUnique<IPCClientInfo>(aId, mozilla::Nothing(), aType,
21                                       aPrincipalInfo, aCreationTime, ""_ns,
22                                       mozilla::dom::FrameType::None,
23                                       mozilla::Nothing(), mozilla::Nothing())) {
24 }
25 
ClientInfo(const IPCClientInfo & aData)26 ClientInfo::ClientInfo(const IPCClientInfo& aData)
27     : mData(MakeUnique<IPCClientInfo>(aData)) {}
28 
ClientInfo(const ClientInfo & aRight)29 ClientInfo::ClientInfo(const ClientInfo& aRight) { operator=(aRight); }
30 
operator =(const ClientInfo & aRight)31 ClientInfo& ClientInfo::operator=(const ClientInfo& aRight) {
32   mData.reset();
33   mData = MakeUnique<IPCClientInfo>(*aRight.mData);
34   return *this;
35 }
36 
ClientInfo(ClientInfo && aRight)37 ClientInfo::ClientInfo(ClientInfo&& aRight) : mData(std::move(aRight.mData)) {}
38 
operator =(ClientInfo && aRight)39 ClientInfo& ClientInfo::operator=(ClientInfo&& aRight) {
40   mData.reset();
41   mData = std::move(aRight.mData);
42   return *this;
43 }
44 
45 ClientInfo::~ClientInfo() = default;
46 
operator ==(const ClientInfo & aRight) const47 bool ClientInfo::operator==(const ClientInfo& aRight) const {
48   return *mData == *aRight.mData;
49 }
50 
operator !=(const ClientInfo & aRight) const51 bool ClientInfo::operator!=(const ClientInfo& aRight) const {
52   return *mData != *aRight.mData;
53 }
54 
Id() const55 const nsID& ClientInfo::Id() const { return mData->id(); }
56 
SetAgentClusterId(const nsID & aId)57 void ClientInfo::SetAgentClusterId(const nsID& aId) {
58   MOZ_ASSERT(mData->agentClusterId().isNothing() ||
59              mData->agentClusterId().ref().Equals(aId));
60   mData->agentClusterId() = Some(aId);
61 }
62 
AgentClusterId() const63 const Maybe<nsID>& ClientInfo::AgentClusterId() const {
64   return mData->agentClusterId();
65 }
66 
Type() const67 ClientType ClientInfo::Type() const { return mData->type(); }
68 
PrincipalInfo() const69 const mozilla::ipc::PrincipalInfo& ClientInfo::PrincipalInfo() const {
70   return mData->principalInfo();
71 }
72 
CreationTime() const73 const TimeStamp& ClientInfo::CreationTime() const {
74   return mData->creationTime();
75 }
76 
URL() const77 const nsCString& ClientInfo::URL() const { return mData->url(); }
78 
SetURL(const nsACString & aURL)79 void ClientInfo::SetURL(const nsACString& aURL) { mData->url() = aURL; }
80 
FrameType() const81 FrameType ClientInfo::FrameType() const { return mData->frameType(); }
82 
SetFrameType(mozilla::dom::FrameType aFrameType)83 void ClientInfo::SetFrameType(mozilla::dom::FrameType aFrameType) {
84   mData->frameType() = aFrameType;
85 }
86 
ToIPC() const87 const IPCClientInfo& ClientInfo::ToIPC() const { return *mData; }
88 
IsPrivateBrowsing() const89 bool ClientInfo::IsPrivateBrowsing() const {
90   switch (PrincipalInfo().type()) {
91     case PrincipalInfo::TContentPrincipalInfo: {
92       auto& p = PrincipalInfo().get_ContentPrincipalInfo();
93       return p.attrs().mPrivateBrowsingId != 0;
94     }
95     case PrincipalInfo::TSystemPrincipalInfo: {
96       return false;
97     }
98     case PrincipalInfo::TNullPrincipalInfo: {
99       auto& p = PrincipalInfo().get_NullPrincipalInfo();
100       return p.attrs().mPrivateBrowsingId != 0;
101     }
102     default: {
103       // clients should never be expanded principals
104       MOZ_CRASH("unexpected principal type!");
105     }
106   }
107 }
108 
GetPrincipal() const109 Result<nsCOMPtr<nsIPrincipal>, nsresult> ClientInfo::GetPrincipal() const {
110   MOZ_ASSERT(NS_IsMainThread());
111   return PrincipalInfoToPrincipal(PrincipalInfo());
112 }
113 
GetCspInfo() const114 const Maybe<mozilla::ipc::CSPInfo>& ClientInfo::GetCspInfo() const {
115   return mData->cspInfo();
116 }
117 
SetCspInfo(const mozilla::ipc::CSPInfo & aCSPInfo)118 void ClientInfo::SetCspInfo(const mozilla::ipc::CSPInfo& aCSPInfo) {
119   mData->cspInfo() = Some(aCSPInfo);
120 }
121 
GetPreloadCspInfo() const122 const Maybe<mozilla::ipc::CSPInfo>& ClientInfo::GetPreloadCspInfo() const {
123   return mData->preloadCspInfo();
124 }
125 
SetPreloadCspInfo(const mozilla::ipc::CSPInfo & aPreloadCSPInfo)126 void ClientInfo::SetPreloadCspInfo(
127     const mozilla::ipc::CSPInfo& aPreloadCSPInfo) {
128   mData->preloadCspInfo() = Some(aPreloadCSPInfo);
129 }
130 
131 }  // namespace mozilla::dom
132