This source file includes following definitions.
- diff_size
- ParseError
- TagNameEquals
- GetChildren
- GetAttribute
- XmlErrorFunc
- get
- GetNamespace
- ParseSingleAppTag
- Parse
#include "chrome/common/extensions/update_manifest.h"
#include <algorithm>
#include "base/memory/scoped_ptr.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/version.h"
#include "libxml/tree.h"
#include "third_party/libxml/chromium/libxml_utils.h"
static const char* kExpectedGupdateProtocol = "2.0";
static const char* kExpectedGupdateXmlns =
"http://www.google.com/update2/response";
UpdateManifest::Result::Result()
: size(0),
diff_size(0) {}
UpdateManifest::Result::~Result() {}
UpdateManifest::Results::Results() : daystart_elapsed_seconds(kNoDaystart) {}
UpdateManifest::Results::~Results() {}
UpdateManifest::UpdateManifest() {
}
UpdateManifest::~UpdateManifest() {}
void UpdateManifest::ParseError(const char* details, ...) {
va_list args;
va_start(args, details);
if (errors_.length() > 0) {
errors_ += "\r\n";
}
base::StringAppendV(&errors_, details, args);
va_end(args);
}
static bool TagNameEquals(const xmlNode* node, const char* expected_name,
const xmlNs* expected_namespace) {
if (node->ns != expected_namespace) {
return false;
}
return 0 == strcmp(expected_name, reinterpret_cast<const char*>(node->name));
}
static std::vector<xmlNode*> GetChildren(xmlNode* root, xmlNs* xml_namespace,
const char* name) {
std::vector<xmlNode*> result;
for (xmlNode* child = root->children; child != NULL; child = child->next) {
if (!TagNameEquals(child, name, xml_namespace)) {
continue;
}
result.push_back(child);
}
return result;
}
static std::string GetAttribute(xmlNode* node, const char* attribute_name) {
const xmlChar* name = reinterpret_cast<const xmlChar*>(attribute_name);
for (xmlAttr* attr = node->properties; attr != NULL; attr = attr->next) {
if (!xmlStrcmp(attr->name, name) && attr->children &&
attr->children->content) {
return std::string(reinterpret_cast<const char*>(
attr->children->content));
}
}
return std::string();
}
static void XmlErrorFunc(void *context, const char *message, ...) {
va_list args;
va_start(args, message);
std::string* error = static_cast<std::string*>(context);
base::StringAppendV(error, message, args);
va_end(args);
}
class ScopedXmlDocument {
public:
explicit ScopedXmlDocument(xmlDocPtr document) : document_(document) {}
~ScopedXmlDocument() {
if (document_)
xmlFreeDoc(document_);
}
xmlDocPtr get() {
return document_;
}
private:
xmlDocPtr document_;
};
static xmlNs* GetNamespace(xmlNode* node, const char* expected_href) {
const xmlChar* href = reinterpret_cast<const xmlChar*>(expected_href);
for (xmlNs* ns = node->ns; ns != NULL; ns = ns->next) {
if (ns->href && !xmlStrcmp(ns->href, href)) {
return ns;
}
}
return NULL;
}
static bool ParseSingleAppTag(xmlNode* app_node, xmlNs* xml_namespace,
UpdateManifest::Result* result,
std::string *error_detail) {
result->extension_id = GetAttribute(app_node, "appid");
if (result->extension_id.length() == 0) {
*error_detail = "Missing appid on app node";
return false;
}
std::vector<xmlNode*> updates = GetChildren(app_node, xml_namespace,
"updatecheck");
if (updates.size() > 1) {
*error_detail = "Too many updatecheck tags on app (expecting only 1).";
return false;
}
if (updates.empty()) {
*error_detail = "Missing updatecheck on app.";
return false;
}
xmlNode *updatecheck = updates[0];
if (GetAttribute(updatecheck, "status") == "noupdate") {
return true;
}
result->crx_url = GURL(GetAttribute(updatecheck, "codebase"));
if (!result->crx_url.is_valid()) {
*error_detail = "Invalid codebase url: '";
*error_detail += result->crx_url.possibly_invalid_spec();
*error_detail += "'.";
return false;
}
result->version = GetAttribute(updatecheck, "version");
if (result->version.length() == 0) {
*error_detail = "Missing version for updatecheck.";
return false;
}
Version version(result->version);
if (!version.IsValid()) {
*error_detail = "Invalid version: '";
*error_detail += result->version;
*error_detail += "'.";
return false;
}
result->browser_min_version = GetAttribute(updatecheck, "prodversionmin");
if (result->browser_min_version.length()) {
Version browser_min_version(result->browser_min_version);
if (!browser_min_version.IsValid()) {
*error_detail = "Invalid prodversionmin: '";
*error_detail += result->browser_min_version;
*error_detail += "'.";
return false;
}
}
result->package_hash = GetAttribute(updatecheck, "hash");
int size = 0;
if (base::StringToInt(GetAttribute(updatecheck, "size"), &size)) {
result->size = size;
}
result->package_fingerprint = GetAttribute(updatecheck, "fp");
result->diff_crx_url = GURL(GetAttribute(updatecheck, "codebasediff"));
result->diff_package_hash = GetAttribute(updatecheck, "hashdiff");
int sizediff = 0;
if (base::StringToInt(GetAttribute(updatecheck, "sizediff"), &sizediff)) {
result->diff_size = sizediff;
}
return true;
}
bool UpdateManifest::Parse(const std::string& manifest_xml) {
results_.list.resize(0);
results_.daystart_elapsed_seconds = kNoDaystart;
errors_ = "";
if (manifest_xml.length() < 1) {
ParseError("Empty xml");
return false;
}
std::string xml_errors;
ScopedXmlErrorFunc error_func(&xml_errors, &XmlErrorFunc);
ScopedXmlDocument document(xmlParseDoc(
reinterpret_cast<const xmlChar*>(manifest_xml.c_str())));
if (!document.get()) {
ParseError("%s", xml_errors.c_str());
return false;
}
xmlNode *root = xmlDocGetRootElement(document.get());
if (!root) {
ParseError("Missing root node");
return false;
}
xmlNs* gupdate_ns = GetNamespace(root, kExpectedGupdateXmlns);
if (!gupdate_ns) {
ParseError("Missing or incorrect xmlns on gupdate tag");
return false;
}
if (!TagNameEquals(root, "gupdate", gupdate_ns)) {
ParseError("Missing gupdate tag");
return false;
}
if (GetAttribute(root, "protocol") != kExpectedGupdateProtocol) {
ParseError("Missing/incorrect protocol on gupdate tag "
"(expected '%s')", kExpectedGupdateProtocol);
return false;
}
std::vector<xmlNode*> daystarts = GetChildren(root, gupdate_ns, "daystart");
if (!daystarts.empty()) {
xmlNode* first = daystarts[0];
std::string elapsed_seconds = GetAttribute(first, "elapsed_seconds");
int parsed_elapsed = kNoDaystart;
if (base::StringToInt(elapsed_seconds, &parsed_elapsed)) {
results_.daystart_elapsed_seconds = parsed_elapsed;
}
}
std::vector<xmlNode*> apps = GetChildren(root, gupdate_ns, "app");
for (unsigned int i = 0; i < apps.size(); i++) {
Result current;
std::string error;
if (!ParseSingleAppTag(apps[i], gupdate_ns, ¤t, &error)) {
ParseError("%s", error.c_str());
} else {
results_.list.push_back(current);
}
}
return true;
}