ClientInfo.cpp (4167B)
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 17 ClientInfo::ClientInfo(const nsID& aId, const Maybe<nsID>& aAgentClusterId, 18 ClientType aType, 19 const mozilla::ipc::PrincipalInfo& aPrincipalInfo, 20 const TimeStamp& aCreationTime, const nsCString& aURL, 21 mozilla::dom::FrameType aFrameType) 22 : mData(MakeUnique<IPCClientInfo>( 23 aId, aAgentClusterId, aType, aPrincipalInfo, aCreationTime, aURL, 24 aFrameType, mozilla::Nothing(), mozilla::Nothing())) {} 25 26 ClientInfo::ClientInfo(const IPCClientInfo& aData) 27 : mData(MakeUnique<IPCClientInfo>(aData)) {} 28 29 ClientInfo::ClientInfo(const ClientInfo& aRight) { operator=(aRight); } 30 31 ClientInfo& ClientInfo::operator=(const ClientInfo& aRight) { 32 mData.reset(); 33 mData = MakeUnique<IPCClientInfo>(*aRight.mData); 34 return *this; 35 } 36 37 ClientInfo::ClientInfo(ClientInfo&& aRight) noexcept 38 : mData(std::move(aRight.mData)) {} 39 40 ClientInfo& ClientInfo::operator=(ClientInfo&& aRight) noexcept { 41 mData.reset(); 42 mData = std::move(aRight.mData); 43 return *this; 44 } 45 46 ClientInfo::~ClientInfo() = default; 47 48 bool ClientInfo::operator==(const ClientInfo& aRight) const { 49 return *mData == *aRight.mData; 50 } 51 52 bool ClientInfo::operator!=(const ClientInfo& aRight) const { 53 return *mData != *aRight.mData; 54 } 55 56 const nsID& ClientInfo::Id() const { return mData->id(); } 57 58 void ClientInfo::SetAgentClusterId(const nsID& aId) { 59 MOZ_ASSERT(mData->agentClusterId().isNothing() || 60 mData->agentClusterId().ref().Equals(aId)); 61 mData->agentClusterId() = Some(aId); 62 } 63 64 const Maybe<nsID>& ClientInfo::AgentClusterId() const { 65 return mData->agentClusterId(); 66 } 67 68 ClientType ClientInfo::Type() const { return mData->type(); } 69 70 const mozilla::ipc::PrincipalInfo& ClientInfo::PrincipalInfo() const { 71 return mData->principalInfo(); 72 } 73 74 const TimeStamp& ClientInfo::CreationTime() const { 75 return mData->creationTime(); 76 } 77 78 const nsCString& ClientInfo::URL() const { return mData->url(); } 79 80 void ClientInfo::SetURL(const nsACString& aURL) { mData->url() = aURL; } 81 82 FrameType ClientInfo::FrameType() const { return mData->frameType(); } 83 84 void ClientInfo::SetFrameType(mozilla::dom::FrameType aFrameType) { 85 mData->frameType() = aFrameType; 86 } 87 88 const IPCClientInfo& ClientInfo::ToIPC() const { return *mData; } 89 90 bool ClientInfo::IsPrivateBrowsing() const { 91 switch (PrincipalInfo().type()) { 92 case PrincipalInfo::TContentPrincipalInfo: { 93 const auto& p = PrincipalInfo().get_ContentPrincipalInfo(); 94 return p.attrs().IsPrivateBrowsing(); 95 } 96 case PrincipalInfo::TSystemPrincipalInfo: { 97 return false; 98 } 99 case PrincipalInfo::TNullPrincipalInfo: { 100 const auto& p = PrincipalInfo().get_NullPrincipalInfo(); 101 return p.attrs().IsPrivateBrowsing(); 102 } 103 default: { 104 // clients should never be expanded principals 105 MOZ_CRASH("unexpected principal type!"); 106 } 107 } 108 } 109 110 Result<nsCOMPtr<nsIPrincipal>, nsresult> ClientInfo::GetPrincipal() const { 111 return PrincipalInfoToPrincipal(PrincipalInfo()); 112 } 113 114 const Maybe<mozilla::ipc::PolicyContainerArgs>& 115 ClientInfo::GetPolicyContainerArgs() const { 116 return mData->policyContainerArgs(); 117 } 118 119 void ClientInfo::SetPolicyContainerArgs( 120 const mozilla::ipc::PolicyContainerArgs& aPolicyContainer) { 121 mData->policyContainerArgs() = Some(aPolicyContainer); 122 } 123 124 const Maybe<mozilla::ipc::CSPInfo>& ClientInfo::GetPreloadCspInfo() const { 125 return mData->preloadCspInfo(); 126 } 127 128 void ClientInfo::SetPreloadCspInfo( 129 const mozilla::ipc::CSPInfo& aPreloadCSPInfo) { 130 mData->preloadCspInfo() = Some(aPreloadCSPInfo); 131 } 132 133 } // namespace mozilla::dom