1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Netscape Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/NPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is Mozilla Communicator client code,
14
* released March 31, 1998.
16
* The Initial Developer of the Original Code is Netscape Communications
17
* Corporation. Portions created by Netscape are
18
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
22
* Doug Turner <dougt@netscape.com>
23
* Pierre Phaneuf <pp@ludusdesign.com>
27
#include "nsUpdateNotification.h"
29
#include "nsISupports.h"
30
#include "nsIServiceManager.h"
32
#include "nsIComponentManager.h"
33
#include "nsIGenericFactory.h"
34
#include "nsIRDFContainer.h"
35
#include "nsIRDFDataSource.h"
36
#include "nsIRDFService.h"
37
#include "nsIRDFRemoteDataSource.h"
39
#include "nsIRDFXMLSink.h"
41
#include "nsIPrefBranch.h"
42
#include "nsIPrefService.h"
43
#include "nsISoftwareUpdate.h"
45
#define NC_RDF_NAME "http://home.netscape.com/NC-rdf#name"
46
#define NC_RDF_SOURCE "http://home.netscape.com/NC-rdf#source"
47
#define NC_RDF_URL "http://home.netscape.com/NC-rdf#url"
48
#define NC_RDF_CHILD "http://home.netscape.com/NC-rdf#child"
50
#define NC_RDF_NOTIFICATION_ROOT "http://home.netscape.com/NC-rdf#SoftwareNotificationRoot"
51
#define NC_XPI_SOURCES "http://home.netscape.com/NC-rdf#SoftwareUpdateDataSources"
52
#define NC_XPI_PACKAGES "http://home.netscape.com/NC-rdf#SoftwarePackages"
54
#define NC_XPI_TITLE "http://home.netscape.com/NC-rdf#title"
55
#define NC_XPI_REGKEY "http://home.netscape.com/NC-rdf#registryKey"
56
#define NC_XPI_VERSION "http://home.netscape.com/NC-rdf#version"
57
#define NC_XPI_DESCRIPTION "http://home.netscape.com/NC-rdf#description"
59
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
60
static NS_DEFINE_CID(kRDFContainerCID, NS_RDFCONTAINER_CID);
62
nsIRDFResource* nsXPINotifierImpl::kXPI_NotifierSources = nsnull;
63
nsIRDFResource* nsXPINotifierImpl::kXPI_NotifierPackages = nsnull;
64
nsIRDFResource* nsXPINotifierImpl::kXPI_NotifierPackage_Title = nsnull;
65
nsIRDFResource* nsXPINotifierImpl::kXPI_NotifierPackage_Version = nsnull;
66
nsIRDFResource* nsXPINotifierImpl::kXPI_NotifierPackage_Description = nsnull;
67
nsIRDFResource* nsXPINotifierImpl::kXPI_NotifierPackage_RegKey = nsnull;
69
nsIRDFResource* nsXPINotifierImpl::kNC_NotificationRoot = nsnull;
70
nsIRDFResource* nsXPINotifierImpl::kNC_Source = nsnull;
71
nsIRDFResource* nsXPINotifierImpl::kNC_Name = nsnull;
72
nsIRDFResource* nsXPINotifierImpl::kNC_URL = nsnull;
73
nsIRDFResource* nsXPINotifierImpl::kNC_Child = nsnull;
76
nsXPINotifierImpl::nsXPINotifierImpl()
79
mPendingRefreshes = 0;
81
static NS_DEFINE_CID(kRDFInMemoryDataSourceCID, NS_RDFINMEMORYDATASOURCE_CID);
83
nsComponentManager::CreateInstance(kRDFInMemoryDataSourceCID,
85
NS_GET_IID(nsISupports),
86
getter_AddRefs(mNotifications));
90
nsXPINotifierImpl::~nsXPINotifierImpl()
94
nsServiceManager::ReleaseService(kRDFServiceCID, mRDF);
98
NS_IF_RELEASE(kXPI_NotifierSources);
99
NS_IF_RELEASE(kXPI_NotifierPackages);
100
NS_IF_RELEASE(kXPI_NotifierPackage_Title);
101
NS_IF_RELEASE(kXPI_NotifierPackage_Version);
102
NS_IF_RELEASE(kXPI_NotifierPackage_Description);
103
NS_IF_RELEASE(kXPI_NotifierPackage_RegKey);
105
NS_IF_RELEASE(kNC_NotificationRoot);
106
NS_IF_RELEASE(kNC_Source);
107
NS_IF_RELEASE(kNC_Name);
108
NS_IF_RELEASE(kNC_URL);
109
NS_IF_RELEASE(kNC_Child);
113
NS_IMPL_ISUPPORTS2(nsXPINotifierImpl, nsIRDFXMLSinkObserver, nsIUpdateNotification)
117
nsXPINotifierImpl::NotificationEnabled(PRBool* aReturn)
121
nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID);
126
// check to see if we are on.
127
nsresult rv = prefBranch->GetBoolPref( (const char*) XPINSTALL_NOTIFICATIONS_ENABLE, &value);
129
if (NS_SUCCEEDED(rv) && value)
131
// check to see the last time we did anything. Since flash does not have a persistant
132
// way to do poll invervals longer than a session, we will implemented that here by using the
135
PRInt32 intervalHours = 0;
140
PRInt32 lastTime = 0;
142
rv = prefBranch->GetIntPref(XPINSTALL_NOTIFICATIONS_INTERVAL, &intervalHours);
146
intervalHours = 7*24; // default at once a week
147
rv = prefBranch->SetIntPref(XPINSTALL_NOTIFICATIONS_INTERVAL, intervalHours);
150
rv = prefBranch->GetIntPref(XPINSTALL_NOTIFICATIONS_LASTDATE, &lastTime);
154
// nowSec = now / 1000000
155
LL_DIV(nowSec, now, 1000000);
157
if (NS_FAILED(rv) || lastTime == 0)
159
rv = prefBranch->SetIntPref(XPINSTALL_NOTIFICATIONS_LASTDATE, nowSec);
163
if ((lastTime + (intervalHours*60*24)) <= nowSec)
174
nsXPINotifierImpl::Init()
178
NotificationEnabled(&enabled);
181
return NS_ERROR_FAILURE;
183
if (mNotifications == nsnull)
184
return NS_ERROR_FAILURE;
187
nsCOMPtr<nsIRDFDataSource> distributors;
188
nsCOMPtr<nsIRDFContainer> distributorsContainer;
189
nsCOMPtr <nsISimpleEnumerator> distributorEnumerator;
192
// Read the distributor registry
193
rv = nsServiceManager::GetService(kRDFServiceCID, NS_GET_IID(nsIRDFService), (nsISupports**) &mRDF);
194
if (NS_FAILED(rv)) return rv;
196
if (! kXPI_NotifierSources)
198
mRDF->GetResource(NC_XPI_SOURCES, &kXPI_NotifierSources);
199
mRDF->GetResource(NC_XPI_PACKAGES, &kXPI_NotifierPackages);
200
mRDF->GetResource(NC_XPI_TITLE, &kXPI_NotifierPackage_Title);
201
mRDF->GetResource(NC_XPI_VERSION, &kXPI_NotifierPackage_Version);
202
mRDF->GetResource(NC_XPI_DESCRIPTION, &kXPI_NotifierPackage_Description);
203
mRDF->GetResource(NC_XPI_REGKEY, &kXPI_NotifierPackage_RegKey);
205
mRDF->GetResource(NC_RDF_NOTIFICATION_ROOT, &kNC_NotificationRoot);
207
mRDF->GetResource(NC_RDF_SOURCE, &kNC_Source);
208
mRDF->GetResource(NC_RDF_NAME, &kNC_Name);
209
mRDF->GetResource(NC_RDF_URL, &kNC_URL);
210
mRDF->GetResource(NC_RDF_CHILD, &kNC_Child);
214
rv = OpenRemoteDataSource(BASE_DATASOURCE_URL, PR_TRUE, getter_AddRefs(distributors));
215
if (NS_FAILED(rv)) return rv;
217
rv = nsComponentManager::CreateInstance(kRDFContainerCID,
219
NS_GET_IID(nsIRDFContainer),
220
getter_AddRefs(distributorsContainer));
222
if (NS_SUCCEEDED(rv))
224
rv = distributorsContainer->Init(distributors, kXPI_NotifierSources);
226
if (NS_SUCCEEDED(rv))
228
rv = distributorsContainer->GetElements(getter_AddRefs(distributorEnumerator));
230
if (NS_SUCCEEDED(rv))
232
distributorEnumerator->HasMoreElements(&moreElements);
235
nsCOMPtr<nsISupports> i;
236
rv = distributorEnumerator->GetNext(getter_AddRefs(i));
237
if (NS_FAILED(rv)) break;
239
nsCOMPtr<nsIRDFResource> aDistributor(do_QueryInterface(i, &rv));
240
if (NS_FAILED(rv)) break;
243
nsCOMPtr<nsIRDFDataSource> remoteDatasource;
244
aDistributor->GetValue(&uri);
246
rv = OpenRemoteDataSource(uri, PR_FALSE, getter_AddRefs(remoteDatasource));
248
if (NS_FAILED(rv)) break;
250
distributorEnumerator->HasMoreElements(&moreElements);
260
nsXPINotifierImpl::IsNewerOrUninstalled(const char* regKey, const char* versionString)
262
PRBool needJar = PR_FALSE;
264
REGERR status = VR_ValidateComponent( (char*) regKey );
266
if ( status == REGERR_NOFIND || status == REGERR_NOFILE )
268
// either component is not in the registry or it's a file
269
// node and the physical file is missing
276
status = VR_GetVersion( (char*)regKey, &oldVersion );
278
if ( status != REGERR_OK )
286
StringToVersionNumbers(versionString, &(newVersion).major, &(newVersion).minor, &(newVersion).release, &(newVersion).build);
288
if ( CompareVersions(&oldVersion, &newVersion) < 0 )
297
nsXPINotifierImpl::CompareVersions(VERSION *oldversion, VERSION *newVersion)
301
if ( oldversion->major == newVersion->major )
303
if ( oldversion->minor == newVersion->minor )
305
if ( oldversion->release == newVersion->release )
307
if ( oldversion->build == newVersion->build )
309
else if ( oldversion->build > newVersion->build )
314
else if ( oldversion->release > newVersion->release )
319
else if ( oldversion->minor > newVersion->minor )
324
else if ( oldversion->major > newVersion->major )
334
nsXPINotifierImpl::StringToVersionNumbers(const nsString& version, int32 *aMajor, int32 *aMinor, int32 *aRelease, int32 *aBuild)
338
int dot = version.FindChar('.', 0);
342
*aMajor = version.ToInteger(&errorCode);
347
version.Mid(majorStr, 0, dot);
348
*aMajor = majorStr.ToInteger(&errorCode);
351
dot = version.FindChar('.',prev);
355
version.Mid(minorStr, prev, version.Length() - prev);
356
*aMinor = minorStr.ToInteger(&errorCode);
361
version.Mid(minorStr, prev, dot - prev);
362
*aMinor = minorStr.ToInteger(&errorCode);
365
dot = version.FindChar('.',prev);
369
version.Mid(releaseStr, prev, version.Length() - prev);
370
*aRelease = releaseStr.ToInteger(&errorCode);
375
version.Mid(releaseStr, prev, dot - prev);
376
*aRelease = releaseStr.ToInteger(&errorCode);
379
if ( version.Length() > dot )
382
version.Mid(buildStr, prev, version.Length() - prev);
383
*aBuild = buildStr.ToInteger(&errorCode);
391
nsXPINotifierImpl::OpenRemoteDataSource(const char* aURL, PRBool blocking, nsIRDFDataSource** aResult)
393
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
396
nsCOMPtr<nsIRDFRemoteDataSource> remote;
397
rv = nsComponentManager::CreateInstance(kRDFXMLDataSourceCID,
399
NS_GET_IID(nsIRDFRemoteDataSource),
400
getter_AddRefs(remote));
401
if (NS_FAILED(rv)) return rv;
403
rv = remote->Init(aURL);
404
if (NS_SUCCEEDED(rv))
408
nsCOMPtr<nsIRDFXMLSink> sink = do_QueryInterface(remote, &rv);
409
if (NS_FAILED(rv)) return rv;
411
rv = sink->AddXMLSinkObserver(this);
412
if (NS_FAILED(rv)) return rv;
415
rv = remote->Refresh(blocking);
416
if (NS_FAILED(rv)) return rv;
418
nsCOMPtr<nsIRDFDataSource> result = do_QueryInterface(remote, &rv);
420
NS_IF_ADDREF(*aResult);
425
// we've already loaded this datasource. use cached copy
426
return mRDF->GetDataSource(aURL, aResult);
432
nsXPINotifierImpl::New(nsISupports* aOuter, REFNSIID aIID, void** aResult)
434
NS_PRECONDITION(aOuter == nsnull, "no aggregation");
436
return NS_ERROR_NO_AGGREGATION;
438
nsXPINotifierImpl* result = new nsXPINotifierImpl();
440
return NS_ERROR_OUT_OF_MEMORY;
442
NS_ADDREF(result); // stabilize
446
if (NS_SUCCEEDED(rv)) {
447
rv = result->QueryInterface(aIID, aResult);
456
nsXPINotifierImpl::OnBeginLoad(nsIRDFXMLSink *aSink)
462
nsXPINotifierImpl::OnInterrupt(nsIRDFXMLSink *aSink)
467
nsXPINotifierImpl::OnResume(nsIRDFXMLSink *aSink)
473
nsXPINotifierImpl::OnEndLoad(nsIRDFXMLSink *aSink)
477
(void) aSink->RemoveXMLSinkObserver(this);
479
nsCOMPtr<nsIRDFDataSource> distributorDataSource = do_QueryInterface(aSink, &rv);
480
if (NS_FAILED(rv)) return rv;
482
nsCOMPtr<nsIRDFContainer> distributorContainer;
483
nsCOMPtr <nsISimpleEnumerator> packageEnumerator;
486
rv = nsComponentManager::CreateInstance(kRDFContainerCID,
488
NS_GET_IID(nsIRDFContainer),
489
getter_AddRefs(distributorContainer));
490
if (NS_SUCCEEDED(rv))
492
rv = distributorContainer->Init(distributorDataSource, kXPI_NotifierPackages);
493
if (NS_SUCCEEDED(rv))
495
rv = distributorContainer->GetElements(getter_AddRefs(packageEnumerator));
496
if (NS_SUCCEEDED(rv))
498
packageEnumerator->HasMoreElements(&moreElements);
501
nsCOMPtr<nsISupports> i;
503
rv = packageEnumerator->GetNext(getter_AddRefs(i));
504
if (NS_FAILED(rv)) break;
506
nsCOMPtr<nsIRDFResource> aPackage(do_QueryInterface(i, &rv));
507
if (NS_FAILED(rv)) break;
510
// Get the version information
511
nsCOMPtr<nsIRDFNode> versionNode;
512
distributorDataSource->GetTarget(aPackage,
513
kXPI_NotifierPackage_Version,
515
getter_AddRefs(versionNode));
517
nsCOMPtr<nsIRDFLiteral> version(do_QueryInterface(versionNode, &rv));
518
if (NS_FAILED(rv)) break;
520
// Get the regkey information
521
nsCOMPtr<nsIRDFNode> regkeyNode;
522
distributorDataSource->GetTarget(aPackage,
523
kXPI_NotifierPackage_RegKey,
525
getter_AddRefs(regkeyNode));
527
nsCOMPtr<nsIRDFLiteral> regkey(do_QueryInterface(regkeyNode, &rv));
528
if (NS_FAILED(rv)) break;
530
// convert them into workable nsAutoStrings
531
PRUnichar* regkeyCString;
532
regkey->GetValue(®keyCString);
533
nsString regKeyString(regkeyCString);
535
PRUnichar* versionCString;
536
version->GetValue(&versionCString);
537
nsString versionString(versionCString);
538
nsMemory::Free(versionCString);
539
nsMemory::Free(regkeyCString);
541
// check to see if this software title should be "flashed"
542
if (IsNewerOrUninstalled(NS_ConvertUCS2toUTF8(regKeyString).get(), NS_ConvertUCS2toUTF8(versionString).get()))
546
nsCOMPtr<nsIRDFNode> urlNode;
547
distributorDataSource->GetTarget(kXPI_NotifierPackages,
550
getter_AddRefs(urlNode));
552
nsCOMPtr<nsIRDFLiteral> url(do_QueryInterface(urlNode, &rv));
553
if (NS_FAILED(rv)) break;
556
nsCOMPtr<nsIRDFNode> titleNode;
557
distributorDataSource->GetTarget(kXPI_NotifierPackages,
558
kXPI_NotifierPackage_Title,
560
getter_AddRefs(titleNode));
562
nsCOMPtr<nsIRDFLiteral> title(do_QueryInterface(titleNode, &rv));
563
if (NS_FAILED(rv)) break;
565
nsCOMPtr<nsIRDFDataSource> ds = do_QueryInterface(mNotifications);
567
ds->Assert(aPackage, kNC_Name, title, PR_TRUE);
568
ds->Assert(aPackage, kNC_URL, url, PR_TRUE);
570
ds->Assert(kNC_NotificationRoot, kNC_Child, aPackage, PR_TRUE);
584
nsXPINotifierImpl::OnError(nsIRDFXMLSink *aSink, nsresult aResult, const PRUnichar* aErrorMsg)
586
(void) aSink->RemoveXMLSinkObserver(this);
592
nsXPINotifierImpl::DisplayUpdateDialog(void)
595
nsCOMPtr <nsISimpleEnumerator> packages;
598
nsCOMPtr<nsIRDFDataSource> ds = do_QueryInterface(mNotifications, &rv);
600
if (NS_SUCCEEDED(rv))
602
rv = ds->GetAllResources(getter_AddRefs(packages));
603
if (NS_SUCCEEDED(rv))
605
packages->HasMoreElements(&moreElements);
608
nsCOMPtr<nsISupports> i;
610
rv = packages->GetNext(getter_AddRefs(i));
611
if (NS_FAILED(rv)) break;
613
nsCOMPtr<nsIRDFResource> aPackage(do_QueryInterface(i, &rv));
614
if (NS_FAILED(rv)) break;
617
// Get the version information
618
nsCOMPtr<nsIRDFNode> name;
619
ds->GetTarget(aPackage,
620
nsXPINotifierImpl::kNC_Name,
622
getter_AddRefs(name));
624
nsCOMPtr<nsIRDFLiteral> nameLiteral = do_QueryInterface(name, &rv);
625
if (NS_FAILED(rv)) break;