Opera 12.15 Source Code
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

country_checker.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* -*- Mode: c++; tab-width: 4; c-basic-offset: 4 -*-
  2. *
  3. * Copyright (C) 1995-2011 Opera Software AS. All rights reserved.
  4. *
  5. * This file is part of the Opera web browser.
  6. * It may not be distributed under any circumstances.
  7. */
  8. #include "core/pch.h"
  9. #ifdef AUTO_UPDATE_SUPPORT
  10. #include "adjunct/autoupdate/country_checker.h"
  11. #include "adjunct/autoupdate/updatablesetting.h"
  12. CountryChecker::CountryChecker()
  13. : m_status(NotInitialized)
  14. , m_autoupdate_xml(NULL)
  15. , m_status_xml_downloader(NULL)
  16. , m_listener(NULL)
  17. , m_autoupdate_server_url(NULL)
  18. {
  19. m_timer.SetTimerListener(this);
  20. }
  21. CountryChecker::~CountryChecker()
  22. {
  23. OP_DELETE(m_status_xml_downloader);
  24. OP_DELETE(m_autoupdate_server_url);
  25. OP_DELETE(m_autoupdate_xml);
  26. }
  27. OP_STATUS CountryChecker::Init(CountryCheckerListener* listener)
  28. {
  29. if (m_status != NotInitialized)
  30. return OpStatus::ERR;
  31. m_listener = listener;
  32. m_status = InitFailed;
  33. OpAutoPtr<AutoUpdateXML> autoupdate_xml_guard(OP_NEW(AutoUpdateXML, ()));
  34. OpAutoPtr<StatusXMLDownloader> status_xml_downloader_guard(OP_NEW(StatusXMLDownloader, ()));
  35. OpAutoPtr<AutoUpdateServerURL> autoupdate_server_url_guard(OP_NEW(AutoUpdateServerURL, ()));
  36. RETURN_OOM_IF_NULL(autoupdate_xml_guard.get());
  37. RETURN_OOM_IF_NULL(status_xml_downloader_guard.get());
  38. RETURN_OOM_IF_NULL(autoupdate_server_url_guard.get());
  39. RETURN_IF_ERROR(autoupdate_xml_guard->Init());
  40. RETURN_IF_ERROR(status_xml_downloader_guard->Init(StatusXMLDownloader::CheckTypeOther, this));
  41. RETURN_IF_ERROR(autoupdate_server_url_guard->Init());
  42. m_autoupdate_xml = autoupdate_xml_guard.release();
  43. m_status_xml_downloader = status_xml_downloader_guard.release();
  44. m_autoupdate_server_url = autoupdate_server_url_guard.release();
  45. m_status = CheckNotPerformed;
  46. return OpStatus::OK;
  47. }
  48. OP_STATUS CountryChecker::CheckCountryCode(UINT32 timeout)
  49. {
  50. if (m_status != CheckNotPerformed)
  51. return OpStatus::ERR;
  52. OP_ASSERT(m_autoupdate_server_url);
  53. OP_ASSERT(m_autoupdate_xml);
  54. OP_ASSERT(m_status_xml_downloader);
  55. m_country_code.Empty();
  56. m_status = CheckFailed;
  57. OpString url_string;
  58. RETURN_IF_ERROR(GetServerAddress(url_string));
  59. OpString8 xml_string;
  60. m_autoupdate_xml->ClearRequestItems();
  61. // RT_Other ensures that this request is not identified as the "main" autoupdate, see DSK-344690
  62. RETURN_IF_ERROR(m_autoupdate_xml->GetRequestXML(xml_string, AutoUpdateXML::UpdateLevelCountryCheck, AutoUpdateXML::RT_Other));
  63. RETURN_IF_ERROR(m_status_xml_downloader->StartXMLRequest(url_string, xml_string));
  64. if (timeout)
  65. {
  66. m_timer.Start(timeout);
  67. }
  68. else
  69. {
  70. m_timer.Stop();
  71. }
  72. m_status = CheckInProgress;
  73. return OpStatus::OK;
  74. }
  75. OP_STATUS CountryChecker::GetServerAddress(OpString& address)
  76. {
  77. OpString current_url;
  78. RETURN_IF_ERROR(m_autoupdate_server_url->GetCurrentURL(current_url));
  79. // Country check should be as fast as possible, so http is preferred over https. However
  80. // we switch to http only if current_url matches one of the default addresses. (DSK-353772)
  81. if (current_url.Compare("https:", 6) == 0)
  82. {
  83. OpString default_urls;
  84. RETURN_IF_ERROR(default_urls.Set(g_pcui->GetDefaultStringPref(PrefsCollectionUI::AutoUpdateServer)));
  85. int length = current_url.Length();
  86. bool trailing_slash = current_url[length-1] == '/';
  87. if (trailing_slash)
  88. {
  89. current_url.Delete(length-1);
  90. --length;
  91. }
  92. int pos = default_urls.Find(current_url);
  93. while (pos != KNotFound)
  94. {
  95. // check that current_url matched whole address at position pos in the list
  96. if (pos == 0 || default_urls[pos-1] == ' ')
  97. {
  98. int end_pos = pos + length;
  99. if (default_urls[end_pos] == '\0' || default_urls[end_pos] == ' ' ||
  100. (default_urls[end_pos] == '/' && (default_urls[end_pos+1] == '\0' || default_urls[end_pos+1] == ' ')))
  101. {
  102. current_url.Delete(4, 1); // remove 's' from 'https'
  103. break;
  104. }
  105. }
  106. pos = default_urls.Find(current_url, pos+length);
  107. }
  108. if (trailing_slash)
  109. {
  110. RETURN_IF_ERROR(current_url.Append("/"));
  111. }
  112. }
  113. return address.TakeOver(current_url);
  114. }
  115. void CountryChecker::StatusXMLDownloaded(StatusXMLDownloader* downloader)
  116. {
  117. OP_ASSERT(downloader);
  118. OP_ASSERT(downloader == m_status_xml_downloader);
  119. OP_ASSERT(m_status == CheckInProgress);
  120. m_timer.Stop();
  121. UpdatableResource* resource = downloader->GetFirstResource();
  122. while (resource)
  123. {
  124. if (resource->GetType() == UpdatableResource::RTSetting)
  125. {
  126. UpdatableSetting* s = static_cast<UpdatableSetting*>(resource);
  127. OpString section, key;
  128. if (OpStatus::IsError(s->GetAttrValue(URA_SECTION, section)) ||
  129. OpStatus::IsError(s->GetAttrValue(URA_KEY, key)))
  130. {
  131. break;
  132. }
  133. if (section == "Auto Update" && key == "Country Code")
  134. {
  135. OpString data;
  136. if (OpStatus::IsSuccess(s->GetAttrValue(URA_DATA, data)) && data.Length() == 2 &&
  137. data != "A6") // A6 means "Unknown Country" - see discussion in BINT-76
  138. {
  139. m_country_code.TakeOver(data);
  140. }
  141. break;
  142. }
  143. }
  144. resource = downloader->GetNextResource();
  145. }
  146. m_status = m_country_code.HasContent() ? CheckSucceded : CheckFailed;
  147. if (m_listener)
  148. {
  149. m_listener->CountryCheckFinished();
  150. }
  151. }
  152. void CountryChecker::StatusXMLDownloadFailed(StatusXMLDownloader* downloader, StatusXMLDownloader::DownloadStatus)
  153. {
  154. OP_ASSERT(downloader);
  155. OP_ASSERT(downloader == m_status_xml_downloader);
  156. OP_ASSERT(m_status == CheckInProgress);
  157. m_timer.Stop();
  158. m_status = CheckFailed;
  159. if (m_listener)
  160. {
  161. m_listener->CountryCheckFinished();
  162. }
  163. }
  164. void CountryChecker::OnTimeOut(OpTimer* timer)
  165. {
  166. OP_ASSERT(timer == &m_timer);
  167. OP_ASSERT(m_status == CheckInProgress);
  168. m_status = CheckTimedOut;
  169. if (m_status_xml_downloader)
  170. {
  171. OpStatus::Ignore(m_status_xml_downloader->StopRequest());
  172. }
  173. if (m_listener)
  174. {
  175. m_listener->CountryCheckFinished();
  176. }
  177. }
  178. #endif // AUTO_UPDATE_SUPPORT