Bug 1523562 [wpt PR 15079] - Pass the full path to the flake8 config files, a=testonly
[gecko.git] / uriloader / prefetch / OfflineCacheUpdateParent.cpp
blobd50d2aba0fc4e6b5aa9ef9cd9e26a7f478036006
1 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "OfflineCacheUpdateParent.h"
8 #include "BackgroundUtils.h"
9 #include "mozilla/BasePrincipal.h"
10 #include "mozilla/dom/Element.h"
11 #include "mozilla/dom/TabParent.h"
12 #include "mozilla/ipc/URIUtils.h"
13 #include "mozilla/Unused.h"
14 #include "nsContentUtils.h"
15 #include "nsOfflineCacheUpdate.h"
16 #include "nsIApplicationCache.h"
17 #include "nsIScriptSecurityManager.h"
18 #include "nsNetUtil.h"
20 using namespace mozilla::ipc;
21 using mozilla::BasePrincipal;
22 using mozilla::OriginAttributes;
23 using mozilla::dom::TabParent;
26 // To enable logging (see mozilla/Logging.h for full details):
28 // set MOZ_LOG=nsOfflineCacheUpdate:5
29 // set MOZ_LOG_FILE=offlineupdate.log
31 // this enables LogLevel::Debug level information and places all output in
32 // the file offlineupdate.log
34 extern mozilla::LazyLogModule gOfflineCacheUpdateLog;
36 #undef LOG
37 #define LOG(args) \
38 MOZ_LOG(gOfflineCacheUpdateLog, mozilla::LogLevel::Debug, args)
40 #undef LOG_ENABLED
41 #define LOG_ENABLED() \
42 MOZ_LOG_TEST(gOfflineCacheUpdateLog, mozilla::LogLevel::Debug)
44 namespace mozilla {
45 namespace docshell {
47 //-----------------------------------------------------------------------------
48 // OfflineCacheUpdateParent::nsISupports
49 //-----------------------------------------------------------------------------
51 NS_IMPL_ISUPPORTS(OfflineCacheUpdateParent, nsIOfflineCacheUpdateObserver,
52 nsILoadContext)
54 //-----------------------------------------------------------------------------
55 // OfflineCacheUpdateParent <public>
56 //-----------------------------------------------------------------------------
58 OfflineCacheUpdateParent::OfflineCacheUpdateParent() : mIPCClosed(false) {
59 // Make sure the service has been initialized
60 nsOfflineCacheUpdateService::EnsureService();
62 LOG(("OfflineCacheUpdateParent::OfflineCacheUpdateParent [%p]", this));
65 OfflineCacheUpdateParent::~OfflineCacheUpdateParent() {
66 LOG(("OfflineCacheUpdateParent::~OfflineCacheUpdateParent [%p]", this));
69 void OfflineCacheUpdateParent::ActorDestroy(ActorDestroyReason why) {
70 mIPCClosed = true;
73 nsresult OfflineCacheUpdateParent::Schedule(
74 const URIParams& aManifestURI, const URIParams& aDocumentURI,
75 const PrincipalInfo& aLoadingPrincipalInfo, const bool& stickDocument) {
76 LOG(("OfflineCacheUpdateParent::RecvSchedule [%p]", this));
78 nsresult rv;
80 RefPtr<nsOfflineCacheUpdate> update;
81 nsCOMPtr<nsIURI> manifestURI = DeserializeURI(aManifestURI);
82 if (!manifestURI) return NS_ERROR_FAILURE;
84 mLoadingPrincipal = PrincipalInfoToPrincipal(aLoadingPrincipalInfo, &rv);
85 NS_ENSURE_SUCCESS(rv, rv);
87 nsOfflineCacheUpdateService* service =
88 nsOfflineCacheUpdateService::EnsureService();
89 if (!service) return NS_ERROR_FAILURE;
91 bool offlinePermissionAllowed = false;
93 rv = service->OfflineAppAllowed(mLoadingPrincipal, nullptr,
94 &offlinePermissionAllowed);
95 NS_ENSURE_SUCCESS(rv, rv);
97 if (!offlinePermissionAllowed) return NS_ERROR_DOM_SECURITY_ERR;
99 nsCOMPtr<nsIURI> documentURI = DeserializeURI(aDocumentURI);
100 if (!documentURI) return NS_ERROR_FAILURE;
102 if (!NS_SecurityCompareURIs(manifestURI, documentURI, false))
103 return NS_ERROR_DOM_SECURITY_ERR;
105 nsAutoCString originSuffix;
106 rv = mLoadingPrincipal->GetOriginSuffix(originSuffix);
107 NS_ENSURE_SUCCESS(rv, rv);
109 service->FindUpdate(manifestURI, originSuffix, nullptr,
110 getter_AddRefs(update));
111 if (!update) {
112 update = new nsOfflineCacheUpdate();
114 // Leave aDocument argument null. Only glues and children keep
115 // document instances.
116 rv = update->Init(manifestURI, documentURI, mLoadingPrincipal, nullptr,
117 nullptr);
118 NS_ENSURE_SUCCESS(rv, rv);
120 // Must add before Schedule() call otherwise we would miss
121 // oncheck event notification.
122 update->AddObserver(this, false);
124 rv = update->Schedule();
125 NS_ENSURE_SUCCESS(rv, rv);
126 } else {
127 update->AddObserver(this, false);
130 if (stickDocument) {
131 update->StickDocument(documentURI);
134 return NS_OK;
137 NS_IMETHODIMP
138 OfflineCacheUpdateParent::UpdateStateChanged(nsIOfflineCacheUpdate* aUpdate,
139 uint32_t state) {
140 if (mIPCClosed) return NS_ERROR_UNEXPECTED;
142 LOG(("OfflineCacheUpdateParent::StateEvent [%p]", this));
144 uint64_t byteProgress;
145 aUpdate->GetByteProgress(&byteProgress);
146 Unused << SendNotifyStateEvent(state, byteProgress);
148 if (state == nsIOfflineCacheUpdateObserver::STATE_FINISHED) {
149 // Tell the child the particulars after the update has finished.
150 // Sending the Finish event will release the child side of the protocol
151 // and notify "offline-cache-update-completed" on the child process.
152 bool isUpgrade;
153 aUpdate->GetIsUpgrade(&isUpgrade);
154 bool succeeded;
155 aUpdate->GetSucceeded(&succeeded);
157 Unused << SendFinish(succeeded, isUpgrade);
160 return NS_OK;
163 NS_IMETHODIMP
164 OfflineCacheUpdateParent::ApplicationCacheAvailable(
165 nsIApplicationCache* aApplicationCache) {
166 if (mIPCClosed) return NS_ERROR_UNEXPECTED;
168 NS_ENSURE_ARG(aApplicationCache);
170 nsCString cacheClientId;
171 aApplicationCache->GetClientID(cacheClientId);
172 nsCString cacheGroupId;
173 aApplicationCache->GetGroupID(cacheGroupId);
175 Unused << SendAssociateDocuments(cacheGroupId, cacheClientId);
176 return NS_OK;
179 //-----------------------------------------------------------------------------
180 // OfflineCacheUpdateParent::nsILoadContext
181 //-----------------------------------------------------------------------------
183 NS_IMETHODIMP
184 OfflineCacheUpdateParent::GetAssociatedWindow(
185 mozIDOMWindowProxy** aAssociatedWindow) {
186 return NS_ERROR_NOT_IMPLEMENTED;
189 NS_IMETHODIMP
190 OfflineCacheUpdateParent::GetTopWindow(mozIDOMWindowProxy** aTopWindow) {
191 return NS_ERROR_NOT_IMPLEMENTED;
194 NS_IMETHODIMP
195 OfflineCacheUpdateParent::GetTopFrameElement(dom::Element** aElement) {
196 return NS_ERROR_NOT_IMPLEMENTED;
199 NS_IMETHODIMP
200 OfflineCacheUpdateParent::GetNestedFrameId(uint64_t* aId) {
201 return NS_ERROR_NOT_IMPLEMENTED;
204 NS_IMETHODIMP
205 OfflineCacheUpdateParent::GetIsContent(bool* aIsContent) {
206 return NS_ERROR_NOT_IMPLEMENTED;
209 NS_IMETHODIMP
210 OfflineCacheUpdateParent::GetUsePrivateBrowsing(bool* aUsePrivateBrowsing) {
211 return NS_ERROR_NOT_IMPLEMENTED;
213 NS_IMETHODIMP
214 OfflineCacheUpdateParent::SetUsePrivateBrowsing(bool aUsePrivateBrowsing) {
215 return NS_ERROR_NOT_IMPLEMENTED;
218 NS_IMETHODIMP
219 OfflineCacheUpdateParent::SetPrivateBrowsing(bool aUsePrivateBrowsing) {
220 return NS_ERROR_NOT_IMPLEMENTED;
223 NS_IMETHODIMP
224 OfflineCacheUpdateParent::GetUseRemoteTabs(bool* aUseRemoteTabs) {
225 return NS_ERROR_NOT_IMPLEMENTED;
228 NS_IMETHODIMP
229 OfflineCacheUpdateParent::SetRemoteTabs(bool aUseRemoteTabs) {
230 return NS_ERROR_NOT_IMPLEMENTED;
233 NS_IMETHODIMP
234 OfflineCacheUpdateParent::GetIsInIsolatedMozBrowserElement(
235 bool* aIsInIsolatedMozBrowserElement) {
236 NS_ENSURE_TRUE(mLoadingPrincipal, NS_ERROR_UNEXPECTED);
237 return mLoadingPrincipal->GetIsInIsolatedMozBrowserElement(
238 aIsInIsolatedMozBrowserElement);
241 NS_IMETHODIMP
242 OfflineCacheUpdateParent::GetScriptableOriginAttributes(
243 JSContext* aCx, JS::MutableHandleValue aAttrs) {
244 NS_ENSURE_TRUE(mLoadingPrincipal, NS_ERROR_UNEXPECTED);
246 nsresult rv = mLoadingPrincipal->GetOriginAttributes(aCx, aAttrs);
247 NS_ENSURE_SUCCESS(rv, rv);
249 return NS_OK;
252 NS_IMETHODIMP_(void)
253 OfflineCacheUpdateParent::GetOriginAttributes(
254 mozilla::OriginAttributes& aAttrs) {
255 if (mLoadingPrincipal) {
256 aAttrs = mLoadingPrincipal->OriginAttributesRef();
260 NS_IMETHODIMP
261 OfflineCacheUpdateParent::GetUseTrackingProtection(
262 bool* aUseTrackingProtection) {
263 return NS_ERROR_NOT_IMPLEMENTED;
266 NS_IMETHODIMP
267 OfflineCacheUpdateParent::SetUseTrackingProtection(
268 bool aUseTrackingProtection) {
269 return NS_ERROR_NOT_IMPLEMENTED;
272 } // namespace docshell
273 } // namespace mozilla