Bug 627938: Fix nsGlobalChromeWindow cleanup. (r=smaug, a=jst)
[mozilla-central.git] / uriloader / prefetch / OfflineCacheUpdateParent.cpp
blobc8dbd4320a37a49785eb207a95e9d3039ae27606
1 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Mozilla Corporation
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Honza Bambas <honzab@firemni.cz>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "OfflineCacheUpdateParent.h"
40 #include "nsOfflineCacheUpdate.h"
41 #include "nsIApplicationCache.h"
43 static nsOfflineCacheUpdateService *gOfflineCacheUpdateService = nsnull;
45 #if defined(PR_LOGGING)
47 // To enable logging (see prlog.h for full details):
49 // set NSPR_LOG_MODULES=nsOfflineCacheUpdate:5
50 // set NSPR_LOG_FILE=offlineupdate.log
52 // this enables PR_LOG_ALWAYS level information and places all output in
53 // the file offlineupdate.log
55 extern PRLogModuleInfo *gOfflineCacheUpdateLog;
56 #endif
57 #define LOG(args) PR_LOG(gOfflineCacheUpdateLog, 4, args)
58 #define LOG_ENABLED() PR_LOG_TEST(gOfflineCacheUpdateLog, 4)
60 namespace mozilla {
61 namespace docshell {
63 //-----------------------------------------------------------------------------
64 // OfflineCacheUpdateParent::nsISupports
65 //-----------------------------------------------------------------------------
67 NS_IMPL_ISUPPORTS1(OfflineCacheUpdateParent,
68 nsIOfflineCacheUpdateObserver)
70 //-----------------------------------------------------------------------------
71 // OfflineCacheUpdateParent <public>
72 //-----------------------------------------------------------------------------
74 OfflineCacheUpdateParent::OfflineCacheUpdateParent()
75 : mIPCClosed(false)
77 // Make sure the service has been initialized
78 nsOfflineCacheUpdateService* service =
79 nsOfflineCacheUpdateService::EnsureService();
80 if (!service)
81 return;
83 LOG(("OfflineCacheUpdateParent::OfflineCacheUpdateParent [%p]", this));
86 OfflineCacheUpdateParent::~OfflineCacheUpdateParent()
88 LOG(("OfflineCacheUpdateParent::~OfflineCacheUpdateParent [%p]", this));
91 void
92 OfflineCacheUpdateParent::ActorDestroy(ActorDestroyReason why)
94 mIPCClosed = true;
97 nsresult
98 OfflineCacheUpdateParent::Schedule(const URI& aManifestURI,
99 const URI& aDocumentURI,
100 const nsCString& aClientID,
101 const bool& stickDocument)
103 LOG(("OfflineCacheUpdateParent::RecvSchedule [%p]", this));
105 nsRefPtr<nsOfflineCacheUpdate> update;
106 nsCOMPtr<nsIURI> manifestURI(aManifestURI);
107 nsCOMPtr<nsIURI> documentURI(aDocumentURI);
109 nsOfflineCacheUpdateService* service =
110 nsOfflineCacheUpdateService::EnsureService();
111 if (!service)
112 return NS_ERROR_FAILURE;
114 service->FindUpdate(manifestURI, documentURI, getter_AddRefs(update));
115 if (!update) {
116 update = new nsOfflineCacheUpdate();
118 nsresult rv;
119 // Leave aDocument argument null. Only glues and children keep
120 // document instances.
121 rv = update->Init(manifestURI, documentURI, nsnull);
122 NS_ENSURE_SUCCESS(rv, rv);
124 rv = update->Schedule();
125 NS_ENSURE_SUCCESS(rv, rv);
128 update->AddObserver(this, PR_FALSE);
130 if (stickDocument) {
131 nsCOMPtr<nsIURI> stickURI;
132 documentURI->Clone(getter_AddRefs(stickURI));
133 update->StickDocument(stickURI);
136 return NS_OK;
139 NS_IMETHODIMP
140 OfflineCacheUpdateParent::UpdateStateChanged(nsIOfflineCacheUpdate *aUpdate, PRUint32 state)
142 if (mIPCClosed)
143 return NS_ERROR_UNEXPECTED;
145 LOG(("OfflineCacheUpdateParent::StateEvent [%p]", this));
147 SendNotifyStateEvent(state);
149 if (state == nsIOfflineCacheUpdateObserver::STATE_FINISHED) {
150 // Tell the child the particulars after the update has finished.
151 // Sending the Finish event will release the child side of the protocol
152 // and notify "offline-cache-update-completed" on the child process.
153 PRBool isUpgrade;
154 aUpdate->GetIsUpgrade(&isUpgrade);
155 PRBool succeeded;
156 aUpdate->GetSucceeded(&succeeded);
158 SendFinish(succeeded, isUpgrade);
161 return NS_OK;
164 NS_IMETHODIMP
165 OfflineCacheUpdateParent::ApplicationCacheAvailable(nsIApplicationCache *aApplicationCache)
167 if (mIPCClosed)
168 return NS_ERROR_UNEXPECTED;
170 NS_ENSURE_ARG(aApplicationCache);
172 nsCString cacheClientId;
173 aApplicationCache->GetClientID(cacheClientId);
174 nsCString cacheGroupId;
175 aApplicationCache->GetGroupID(cacheGroupId);
177 SendAssociateDocuments(cacheGroupId, cacheClientId);
178 return NS_OK;
181 } // docshell
182 } // mozilla