084b167c22f2972dfd15b2748f71cc88ca392a96
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / installer / wgt_package.cc
1 // Copyright (c) 2013 Intel Corporation. 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.
4
5 #include "xwalk/application/common/installer/wgt_package.h"
6
7 #include "base/file_util.h"
8 #include "base/files/scoped_file.h"
9 #include "third_party/libxml/chromium/libxml_utils.h"
10 #include "xwalk/application/common/id_util.h"
11 #include "xwalk/application/common/installer/tizen/signature_validator.h"
12
13 namespace xwalk {
14 namespace application {
15 namespace {
16 #if defined(OS_TIZEN)
17 const char kIdNodeName[] = "application";
18 #else
19 const char kIdNodeName[] = "widget";
20 #endif
21 }
22
23 WGTPackage::~WGTPackage() {
24 }
25
26 WGTPackage::WGTPackage(const base::FilePath& path)
27   : Package(path) {
28   if (!base::PathExists(path))
29     return;
30   type_ = WGT;
31   base::FilePath extracted_path;
32   if (!Extract(&extracted_path))
33     return;
34
35   XmlReader xml;
36   if (!xml.LoadFile(extracted_path.Append(FILE_PATH_LITERAL("config.xml"))
37                     .MaybeAsASCII())) {
38     LOG(ERROR) << "Unable to load WGT package config.xml file.";
39     return;
40   }
41
42   while (!xml.SkipToElement()) {
43     if (!xml.Read()) {
44       LOG(ERROR) << "Unable to read WGT package config.xml file.";
45       return;
46     }
47   }
48
49   std::string value;
50   while (xml.Read()) {
51     std::string node_name = xml.NodeName();
52     if (node_name == kIdNodeName) {
53       xml.NodeAttribute("id", &value);
54       break;
55     }
56   }
57
58   if (!value.empty()) {
59 #if defined(OS_TIZEN)
60     id_ = value;
61     is_valid_ =
62       SignatureValidator::Check(extracted_path) != SignatureValidator::INVALID;
63 #else
64     id_ = GenerateId(value);
65     is_valid_ = true;
66 #endif
67   }
68   scoped_ptr<base::ScopedFILE> file(
69         new base::ScopedFILE(base::OpenFile(path, "rb")));
70
71   file_ = file.Pass();
72 }
73 }  // namespace application
74 }  // namespace xwalk