Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

version_checker.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "core/pch.h"
  2. #ifdef AUTO_UPDATE_SUPPORT
  3. #ifdef AUTOUPDATE_PACKAGE_INSTALLATION
  4. #include "adjunct/autoupdate/version_checker.h"
  5. #include "adjunct/autoupdate/updater/audatafile_reader.h"
  6. #include "adjunct/autoupdate/updatablefile.h"
  7. VersionChecker::VersionChecker():
  8. m_status(VCSNotInitialized),
  9. m_autoupdate_xml(NULL),
  10. m_status_xml_downloader(NULL),
  11. m_listener(NULL),
  12. m_autoupdate_server_url(NULL)
  13. {
  14. }
  15. VersionChecker::~VersionChecker()
  16. {
  17. OP_DELETE(m_status_xml_downloader);
  18. OP_DELETE(m_autoupdate_server_url);
  19. OP_DELETE(m_autoupdate_xml);
  20. }
  21. OP_STATUS VersionChecker::Init(VersionCheckerListener* listener)
  22. {
  23. if (!listener)
  24. return OpStatus::ERR;
  25. if (m_status != VCSNotInitialized)
  26. return OpStatus::ERR;
  27. m_listener = listener;
  28. m_status = VCSInitFailed;
  29. OpAutoPtr<AutoUpdateXML> autoupdate_xml_guard(OP_NEW(AutoUpdateXML, ()));
  30. OpAutoPtr<StatusXMLDownloader> status_xml_downloader_guard(OP_NEW(StatusXMLDownloader, ()));
  31. OpAutoPtr<AutoUpdateServerURL> autoupdate_server_url_guard(OP_NEW(AutoUpdateServerURL, ()));
  32. RETURN_OOM_IF_NULL(autoupdate_xml_guard.get());
  33. RETURN_OOM_IF_NULL(status_xml_downloader_guard.get());
  34. RETURN_OOM_IF_NULL(autoupdate_server_url_guard.get());
  35. RETURN_IF_ERROR(autoupdate_xml_guard->Init());
  36. RETURN_IF_ERROR(status_xml_downloader_guard->Init(StatusXMLDownloader::CheckTypeOther, this));
  37. RETURN_IF_ERROR(autoupdate_server_url_guard->Init());
  38. RETURN_IF_ERROR(m_data_file_reader.Init());
  39. RETURN_IF_ERROR(m_data_file_reader.Load());
  40. m_autoupdate_xml = autoupdate_xml_guard.release();
  41. m_status_xml_downloader = status_xml_downloader_guard.release();
  42. m_autoupdate_server_url = autoupdate_server_url_guard.release();
  43. if (!CheckIsNeeded())
  44. m_status = VCSCheckNotNeeded;
  45. else
  46. {
  47. RETURN_IF_ERROR(ReadVersionOfExistingUpdate());
  48. m_status = VCSCheckNotPerformed;
  49. }
  50. return OpStatus::OK;
  51. }
  52. OP_STATUS VersionChecker::GetDataFileLastModificationTime(time_t& result)
  53. {
  54. uni_char* updateFile = m_data_file_reader.GetUpdateFile();
  55. RETURN_VALUE_IF_NULL(updateFile, OpStatus::ERR);
  56. OP_STATUS retcode = OpStatus::OK;
  57. OpFile opFile;
  58. retcode = opFile.Construct(updateFile);
  59. if (OpStatus::IsSuccess(retcode))
  60. retcode = opFile.GetLastModified(result);
  61. // AUDataFileReader uses a plain old "new []" to allocate the strings
  62. delete [] updateFile;
  63. return retcode;;
  64. }
  65. bool VersionChecker::CheckIsNeeded()
  66. {
  67. time_t fileModificationTime = 0;
  68. RETURN_VALUE_IF_ERROR(GetDataFileLastModificationTime(fileModificationTime), true);
  69. time_t currentTime = static_cast<UINT32>(g_op_time_info->GetTimeUTC() / 1000.0);
  70. // It can happen when user modified date in the system
  71. if (currentTime < fileModificationTime)
  72. return true;
  73. time_t updateAgeInDays = (currentTime - fileModificationTime) / (60 * 60 * 24);
  74. if (static_cast<unsigned int>(updateAgeInDays) < MaxDownloadedPackageAgeDays)
  75. return false;
  76. return true;
  77. }
  78. OP_STATUS VersionChecker::ReadVersionOfExistingUpdate()
  79. {
  80. uni_char* versionString = m_data_file_reader.GetVersion();
  81. uni_char* buildNumberString = m_data_file_reader.GetBuildNum();
  82. // OperaVersion::Set() will return error if any of the pointers passed to the method is NULL
  83. OP_STATUS result = m_downloaded_version.Set(versionString, buildNumberString);
  84. // AUDataFileReader uses a plain old "new []" to allocate the strings.
  85. delete [] versionString;
  86. delete [] buildNumberString;
  87. return result;
  88. }
  89. OP_STATUS VersionChecker::CheckRemoteVersion()
  90. {
  91. if (m_status != VCSCheckNotPerformed)
  92. return OpStatus::ERR;
  93. OP_ASSERT(m_autoupdate_server_url);
  94. OP_ASSERT(m_autoupdate_xml);
  95. OP_ASSERT(m_status_xml_downloader);
  96. m_status = VCSCheckVersionFailed;
  97. OpString url_string;
  98. OpString8 xml_string;
  99. RETURN_IF_ERROR(m_autoupdate_server_url->GetCurrentURL(url_string));
  100. m_autoupdate_xml->ClearRequestItems();
  101. // RT_Other ensures that this request is not identified as the "main" autoupdate, see DSK-344690
  102. RETURN_IF_ERROR(m_autoupdate_xml->GetRequestXML(xml_string, AutoUpdateXML::UpdateLevelUpgradeCheck, AutoUpdateXML::RT_Other));
  103. RETURN_IF_ERROR(m_status_xml_downloader->StartXMLRequest(url_string, xml_string));
  104. m_status = VCSCheckInProgress;
  105. return OpStatus::OK;
  106. }
  107. void VersionChecker::StatusXMLDownloaded(StatusXMLDownloader* downloader)
  108. {
  109. OP_ASSERT(m_listener);
  110. OP_ASSERT(downloader);
  111. OP_ASSERT(downloader == m_status_xml_downloader);
  112. OP_ASSERT(m_status == VCSCheckInProgress);
  113. UpdatablePackage* package = NULL;
  114. UpdatableResource* resource = downloader->GetFirstResource();
  115. while (resource)
  116. {
  117. if (resource->GetType() == UpdatableResource::RTPackage)
  118. {
  119. package = static_cast<UpdatablePackage*>(resource);
  120. break;
  121. }
  122. resource = downloader->GetNextResource();
  123. }
  124. if (package)
  125. {
  126. OperaVersion package_version;
  127. if (OpStatus::IsError(package->GetPackageVersion(package_version)))
  128. m_status = VCSCheckVersionFailed;
  129. else
  130. {
  131. if (package_version < m_downloaded_version)
  132. m_status = VCSOlderUpdateAvailable;
  133. else if (package_version > m_downloaded_version)
  134. m_status = VCSNewerUpdateAvailable;
  135. else if (package_version == m_downloaded_version)
  136. m_status = VCSSameUpdateAvailable;
  137. else
  138. m_status = VCSCheckVersionFailed;
  139. }
  140. }
  141. else
  142. m_status = VCSNoUpdateAvailable;
  143. m_listener->VersionCheckFinished();
  144. }
  145. void VersionChecker::StatusXMLDownloadFailed(StatusXMLDownloader* downloader, StatusXMLDownloader::DownloadStatus)
  146. {
  147. OP_ASSERT(m_listener);
  148. OP_ASSERT(downloader);
  149. OP_ASSERT(downloader == m_status_xml_downloader);
  150. OP_ASSERT(m_status == VCSCheckInProgress);
  151. m_status = VCSCheckVersionFailed;
  152. m_listener->VersionCheckFinished();
  153. }
  154. #endif // AUTOUPDATE_PACKAGE_INSTALLATION
  155. #endif // AUTO_UPDATE_SUPPORT