Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / update_manifest.cc
blob85947caabb10b8b7804f138430bdc11e4fc42623
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/extensions/update_manifest.h"
7 #include <algorithm>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util.h"
11 #include "base/string_util.h"
12 #include "base/string_number_conversions.h"
13 #include "base/stringprintf.h"
14 #include "base/version.h"
15 #include "libxml/tree.h"
16 #include "third_party/libxml/chromium/libxml_utils.h"
18 static const char* kExpectedGupdateProtocol = "2.0";
19 static const char* kExpectedGupdateXmlns =
20 "http://www.google.com/update2/response";
22 UpdateManifest::Result::Result() {}
24 UpdateManifest::Result::~Result() {}
26 UpdateManifest::Results::Results() : daystart_elapsed_seconds(kNoDaystart) {}
28 UpdateManifest::Results::~Results() {}
30 UpdateManifest::UpdateManifest() {
33 UpdateManifest::~UpdateManifest() {}
35 void UpdateManifest::ParseError(const char* details, ...) {
36 va_list args;
37 va_start(args, details);
39 if (errors_.length() > 0) {
40 // TODO(asargent) make a platform abstracted newline?
41 errors_ += "\r\n";
43 base::StringAppendV(&errors_, details, args);
44 va_end(args);
47 // Checks whether a given node's name matches |expected_name| and
48 // |expected_namespace|.
49 static bool TagNameEquals(const xmlNode* node, const char* expected_name,
50 const xmlNs* expected_namespace) {
51 if (node->ns != expected_namespace) {
52 return false;
54 return 0 == strcmp(expected_name, reinterpret_cast<const char*>(node->name));
57 // Returns child nodes of |root| with name |name| in namespace |xml_namespace|.
58 static std::vector<xmlNode*> GetChildren(xmlNode* root, xmlNs* xml_namespace,
59 const char* name) {
60 std::vector<xmlNode*> result;
61 for (xmlNode* child = root->children; child != NULL; child = child->next) {
62 if (!TagNameEquals(child, name, xml_namespace)) {
63 continue;
65 result.push_back(child);
67 return result;
70 // Returns the value of a named attribute, or the empty string.
71 static std::string GetAttribute(xmlNode* node, const char* attribute_name) {
72 const xmlChar* name = reinterpret_cast<const xmlChar*>(attribute_name);
73 for (xmlAttr* attr = node->properties; attr != NULL; attr = attr->next) {
74 if (!xmlStrcmp(attr->name, name) && attr->children &&
75 attr->children->content) {
76 return std::string(reinterpret_cast<const char*>(
77 attr->children->content));
80 return std::string();
83 // This is used for the xml parser to report errors. This assumes the context
84 // is a pointer to a std::string where the error message should be appended.
85 static void XmlErrorFunc(void *context, const char *message, ...) {
86 va_list args;
87 va_start(args, message);
88 std::string* error = static_cast<std::string*>(context);
89 base::StringAppendV(error, message, args);
90 va_end(args);
93 // Utility class for cleaning up the xml document when leaving a scope.
94 class ScopedXmlDocument {
95 public:
96 explicit ScopedXmlDocument(xmlDocPtr document) : document_(document) {}
97 ~ScopedXmlDocument() {
98 if (document_)
99 xmlFreeDoc(document_);
102 xmlDocPtr get() {
103 return document_;
106 private:
107 xmlDocPtr document_;
110 // Returns a pointer to the xmlNs on |node| with the |expected_href|, or
111 // NULL if there isn't one with that href.
112 static xmlNs* GetNamespace(xmlNode* node, const char* expected_href) {
113 const xmlChar* href = reinterpret_cast<const xmlChar*>(expected_href);
114 for (xmlNs* ns = node->ns; ns != NULL; ns = ns->next) {
115 if (ns->href && !xmlStrcmp(ns->href, href)) {
116 return ns;
119 return NULL;
123 // Helper function that reads in values for a single <app> tag. It returns a
124 // boolean indicating success or failure. On failure, it writes a error message
125 // into |error_detail|.
126 static bool ParseSingleAppTag(xmlNode* app_node, xmlNs* xml_namespace,
127 UpdateManifest::Result* result,
128 std::string *error_detail) {
129 // Read the extension id.
130 result->extension_id = GetAttribute(app_node, "appid");
131 if (result->extension_id.length() == 0) {
132 *error_detail = "Missing appid on app node";
133 return false;
136 // Get the updatecheck node.
137 std::vector<xmlNode*> updates = GetChildren(app_node, xml_namespace,
138 "updatecheck");
139 if (updates.size() > 1) {
140 *error_detail = "Too many updatecheck tags on app (expecting only 1).";
141 return false;
143 if (updates.empty()) {
144 *error_detail = "Missing updatecheck on app.";
145 return false;
147 xmlNode *updatecheck = updates[0];
149 if (GetAttribute(updatecheck, "status") == "noupdate") {
150 return true;
153 // Find the url to the crx file.
154 result->crx_url = GURL(GetAttribute(updatecheck, "codebase"));
155 if (!result->crx_url.is_valid()) {
156 *error_detail = "Invalid codebase url: '";
157 *error_detail += GetAttribute(updatecheck, "codebase");
158 *error_detail += "'.";
159 return false;
162 // Get the version.
163 result->version = GetAttribute(updatecheck, "version");
164 if (result->version.length() == 0) {
165 *error_detail = "Missing version for updatecheck.";
166 return false;
168 Version version(result->version);
169 if (!version.IsValid()) {
170 *error_detail = "Invalid version: '";
171 *error_detail += result->version;
172 *error_detail += "'.";
173 return false;
176 // Get the minimum browser version (not required).
177 result->browser_min_version = GetAttribute(updatecheck, "prodversionmin");
178 if (result->browser_min_version.length()) {
179 Version browser_min_version(result->browser_min_version);
180 if (!browser_min_version.IsValid()) {
181 *error_detail = "Invalid prodversionmin: '";
182 *error_detail += result->browser_min_version;
183 *error_detail += "'.";
184 return false;
188 // package_hash is optional. It is only required for blacklist. It is a
189 // sha256 hash of the package in hex format.
190 result->package_hash = GetAttribute(updatecheck, "hash");
192 return true;
196 bool UpdateManifest::Parse(const std::string& manifest_xml) {
197 results_.list.resize(0);
198 results_.daystart_elapsed_seconds = kNoDaystart;
199 errors_ = "";
201 if (manifest_xml.length() < 1) {
202 ParseError("Empty xml");
203 return false;
206 std::string xml_errors;
207 ScopedXmlErrorFunc error_func(&xml_errors, &XmlErrorFunc);
209 // Start up the xml parser with the manifest_xml contents.
210 ScopedXmlDocument document(xmlParseDoc(
211 reinterpret_cast<const xmlChar*>(manifest_xml.c_str())));
212 if (!document.get()) {
213 ParseError("%s", xml_errors.c_str());
214 return false;
217 xmlNode *root = xmlDocGetRootElement(document.get());
218 if (!root) {
219 ParseError("Missing root node");
220 return false;
223 // Look for the required namespace declaration.
224 xmlNs* gupdate_ns = GetNamespace(root, kExpectedGupdateXmlns);
225 if (!gupdate_ns) {
226 ParseError("Missing or incorrect xmlns on gupdate tag");
227 return false;
230 if (!TagNameEquals(root, "gupdate", gupdate_ns)) {
231 ParseError("Missing gupdate tag");
232 return false;
235 // Check for the gupdate "protocol" attribute.
236 if (GetAttribute(root, "protocol") != kExpectedGupdateProtocol) {
237 ParseError("Missing/incorrect protocol on gupdate tag "
238 "(expected '%s')", kExpectedGupdateProtocol);
239 return false;
242 // Parse the first <daystart> if it's present.
243 std::vector<xmlNode*> daystarts = GetChildren(root, gupdate_ns, "daystart");
244 if (!daystarts.empty()) {
245 xmlNode* first = daystarts[0];
246 std::string elapsed_seconds = GetAttribute(first, "elapsed_seconds");
247 int parsed_elapsed = kNoDaystart;
248 if (base::StringToInt(elapsed_seconds, &parsed_elapsed)) {
249 results_.daystart_elapsed_seconds = parsed_elapsed;
253 // Parse each of the <app> tags.
254 std::vector<xmlNode*> apps = GetChildren(root, gupdate_ns, "app");
255 for (unsigned int i = 0; i < apps.size(); i++) {
256 Result current;
257 std::string error;
258 if (!ParseSingleAppTag(apps[i], gupdate_ns, &current, &error)) {
259 ParseError("%s", error.c_str());
260 } else {
261 results_.list.push_back(current);
265 return true;