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.

updatableresource.cpp 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; 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. It may not be distributed
  6. * under any circumstances.
  7. *
  8. * @author Marius Blomli, Michal Zajaczkowski
  9. */
  10. #include "core/pch.h"
  11. #ifdef AUTO_UPDATE_SUPPORT
  12. #include "adjunct/autoupdate/updatableresource.h"
  13. #include "adjunct/autoupdate/updatablefile.h"
  14. #include "adjunct/autoupdate/updatablesetting.h"
  15. #include "modules/xmlutils/xmlfragment.h"
  16. #include "modules/xmlutils/xmlnames.h"
  17. /**
  18. * The string values representing the XML element names that may be found in the XML response.
  19. * The UpdatableResourceFactory::URAttrName[] array *MUST* be in sync with the URAttr enum!
  20. */
  21. /* static */
  22. const uni_char* UpdatableResourceFactory::URAttrName[] =
  23. {
  24. UNI_L("type"),
  25. UNI_L("name"),
  26. UNI_L("folder"),
  27. UNI_L("url"),
  28. UNI_L("urlalt"),
  29. UNI_L("version"),
  30. UNI_L("bnum"),
  31. UNI_L("id"),
  32. UNI_L("size"),
  33. UNI_L("lang"),
  34. UNI_L("timestamp"),
  35. UNI_L("checksum"),
  36. UNI_L("infourl"),
  37. UNI_L("showdialog"),
  38. UNI_L("vendor-name"),
  39. UNI_L("install-params"),
  40. UNI_L("eula-url"),
  41. UNI_L("plugin-name"),
  42. UNI_L("mime-type"),
  43. UNI_L("installs-silently"),
  44. UNI_L("has-installer"),
  45. UNI_L("section"),
  46. UNI_L("key"),
  47. UNI_L("data"),
  48. UNI_L("file")
  49. /***CAREFUL*WHEN*ADDING*VALUES*HERE*SEE*THE*URAttr*ENUM***/
  50. };
  51. /* static */
  52. OP_STATUS UpdatableResourceFactory::GetResourceFromXML(const OpStringC& resource_type, XMLFragment& fragment, UpdatableResource** resource)
  53. {
  54. OpString name_string, value_string;
  55. OpAutoVector<URNameValuePair> pairs;
  56. while(fragment.EnterAnyElement())
  57. {
  58. XMLCompleteName element_name;
  59. element_name = fragment.GetElementName();
  60. RETURN_IF_ERROR(name_string.Set(element_name.GetLocalPart()));
  61. RETURN_IF_ERROR(value_string.Set(fragment.GetText()));
  62. URAttr attr = ConvertNameToAttr(name_string);
  63. if (URA_LAST == attr)
  64. return OpStatus::ERR;
  65. OpAutoPtr<URNameValuePair> new_pair(OP_NEW(URNameValuePair, ()));
  66. RETURN_OOM_IF_NULL(new_pair.get());
  67. new_pair->SetAttrName(attr);
  68. RETURN_IF_ERROR(new_pair->SetValue(value_string));
  69. RETURN_IF_ERROR(pairs.Add(new_pair.get()));
  70. new_pair.release();
  71. fragment.LeaveElement();
  72. }
  73. // Ugly, we get the type with files, but we don't get one with settings.
  74. OpString type_string;
  75. if (resource_type.CompareI(UNI_L("setting")) == 0)
  76. RETURN_IF_ERROR(type_string.Set(resource_type));
  77. else
  78. {
  79. OP_ASSERT(resource_type.CompareI(UNI_L("file")) == 0);
  80. RETURN_IF_ERROR(UpdatableResource::GetValueByAttr(pairs, URA_TYPE, type_string));
  81. }
  82. *resource = GetEmptyResourceByType(type_string);
  83. RETURN_OOM_IF_NULL(*resource);
  84. OpAutoPtr<UpdatableResource> resource_keeper(*resource);
  85. for (UINT32 i=0; i<pairs.GetCount(); i++)
  86. {
  87. URNameValuePair* pair = pairs.Get(i);
  88. OP_ASSERT(pair);
  89. RETURN_IF_ERROR((*resource)->AddNameValuePair(pair));
  90. }
  91. if (FALSE == (*resource)->VerifyAttributes())
  92. return OpStatus::ERR;
  93. resource_keeper.release();
  94. return OpStatus::OK;
  95. }
  96. /* static */
  97. UpdatableResource* UpdatableResourceFactory::GetEmptyResourceByType(const OpStringC& type)
  98. {
  99. UpdatableResource* new_resource = NULL;
  100. if (type.CompareI(UNI_L("full")) == 0)
  101. new_resource = OP_NEW(UpdatablePackage, ());
  102. else if (type.CompareI(UNI_L("spoof")) == 0)
  103. new_resource = OP_NEW(UpdatableSpoof, ());
  104. else if (type.CompareI(UNI_L("browserjs")) == 0)
  105. new_resource = OP_NEW(UpdatableBrowserJS, ());
  106. else if (type.CompareI(UNI_L("dictionary")) == 0)
  107. new_resource = OP_NEW(UpdatableDictionary, ());
  108. else if (type.CompareI(UNI_L("dictionaries")) == 0)
  109. new_resource = OP_NEW(UpdatableDictionaryXML, ());
  110. #ifdef PLUGIN_AUTO_INSTALL
  111. else if (type.CompareI(UNI_L("plugin")) == 0)
  112. new_resource = OP_NEW(UpdatablePlugin, ());
  113. #endif // PLUGIN_AUTO_INSTALL
  114. else if (type.CompareI(UNI_L("hardwareblocklist")) == 0)
  115. new_resource = OP_NEW(UpdatableHardwareBlocklist, ());
  116. else if (type.CompareI(UNI_L("setting")) == 0)
  117. new_resource = OP_NEW(UpdatableSetting, ());
  118. else if (type.CompareI(UNI_L("handlersignore")) == 0)
  119. new_resource = OP_NEW(UpdatableHandlersIgnore, ());
  120. else
  121. // Add a new type above if you are implementing a new resource type
  122. OP_ASSERT(!"Resource type string not recognized!");
  123. return new_resource;
  124. }
  125. /* static */
  126. URAttr UpdatableResourceFactory::ConvertNameToAttr(const OpStringC& name)
  127. {
  128. if (name.IsEmpty())
  129. return URA_LAST;
  130. int count = ARRAY_SIZE(URAttrName);
  131. // There a mess between the URAttr and UpdatableResourceFactory::URAttrName[] if this assert triggers!
  132. OP_ASSERT(URA_LAST == count);
  133. for (int i=0; i<count; i++)
  134. {
  135. if (name.CompareI(URAttrName[i]) == 0)
  136. return (URAttr)i;
  137. }
  138. // Add the correct elements to URAttr and UpdatableResourceFactory::URAttrName[] if you believe this should not trigger.
  139. OP_ASSERT(!"Unknown string attribute name received in the response XML!");
  140. return URA_LAST;
  141. }
  142. /********************
  143. * UpdatableResource *
  144. *********************/
  145. UpdatableResource::UpdatableResource()
  146. {
  147. }
  148. OP_STATUS UpdatableResource::AddNameValuePair(URNameValuePair* pair)
  149. {
  150. OP_ASSERT(pair);
  151. if (m_pairs.GetCount() > UpdatableResourceFactory::URA_MAX_PROPERTY_COUNT)
  152. {
  153. OP_ASSERT(!"Property count limit reached!");
  154. return OpStatus::ERR;
  155. }
  156. OpAutoPtr<URNameValuePair> new_pair(OP_NEW(URNameValuePair, ()));
  157. RETURN_OOM_IF_NULL(new_pair.get());
  158. new_pair->SetAttrName(pair->GetAttrName());
  159. OpString value;
  160. RETURN_IF_ERROR(pair->GetAttrValue(value));
  161. RETURN_IF_ERROR(new_pair->SetValue(value));
  162. RETURN_IF_ERROR(m_pairs.Add(new_pair.get()));
  163. new_pair.release();
  164. return OpStatus::OK;
  165. }
  166. /* static */
  167. OP_STATUS UpdatableResource::GetValueByAttr(OpAutoVector<URNameValuePair>& pairs, URAttr attr, OpString& value)
  168. {
  169. for (UINT32 i=0; i<pairs.GetCount(); i++)
  170. {
  171. URNameValuePair* pair = pairs.Get(i);
  172. OP_ASSERT(pair);
  173. if (pair->IsAttr(attr))
  174. return pair->GetAttrValue(value);
  175. }
  176. return OpStatus::ERR_FILE_NOT_FOUND;
  177. }
  178. OP_STATUS UpdatableResource::GetAttrValue(URAttr attr, OpString& value)
  179. {
  180. return GetValueByAttr(m_pairs, attr, value);
  181. }
  182. OP_STATUS UpdatableResource::GetAttrValue(URAttr attr, int& value)
  183. {
  184. OpString value_str;
  185. RETURN_IF_ERROR(GetValueByAttr(m_pairs, attr, value_str));
  186. if (value_str.IsEmpty())
  187. return OpStatus::ERR;
  188. value = uni_atoi(value_str.CStr());
  189. return OpStatus::OK;
  190. }
  191. OP_STATUS UpdatableResource::GetAttrValue(URAttr attr, bool& value)
  192. {
  193. OpString value_str;
  194. RETURN_IF_ERROR(GetValueByAttr(m_pairs, attr, value_str));
  195. if (value_str.IsEmpty())
  196. return OpStatus::ERR;
  197. if (value_str.CompareI("1") == 0 || value_str.CompareI("yes") == 0 || value_str.CompareI("true") == 0)
  198. {
  199. value = true;
  200. return OpStatus::OK;
  201. }
  202. if (value_str.CompareI("0") == 0 || value_str.CompareI("no") == 0 || value_str.CompareI("false") == 0)
  203. {
  204. value = false;
  205. return OpStatus::OK;
  206. }
  207. return OpStatus::ERR;
  208. }
  209. BOOL UpdatableResource::HasAttrWithContent(URAttr attr)
  210. {
  211. OpString value;
  212. RETURN_VALUE_IF_ERROR(GetAttrValue(attr, value), FALSE);
  213. if (value.IsEmpty())
  214. return FALSE;
  215. return TRUE;
  216. }
  217. #endif // AUTO_UPDATE_SUPPORT