Upstream version 9.38.204.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / package / 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/package/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
12 #if defined(OS_TIZEN)
13 #include "xwalk/application/common/tizen/signature_validator.h"
14 #endif
15
16 namespace xwalk {
17 namespace application {
18 namespace {
19 #if defined(OS_TIZEN)
20 const char kIdNodeName[] = "application";
21 #else
22 const char kIdNodeName[] = "widget";
23 #endif
24 }
25
26 WGTPackage::~WGTPackage() {
27 }
28
29 WGTPackage::WGTPackage(const base::FilePath& path)
30   : Package(path) {
31   if (!base::PathExists(path))
32     return;
33   type_ = WGT;
34   base::FilePath extracted_path;
35   // FIXME : we should not call 'extract' here!
36   if (!ExtractToTemporaryDir(&extracted_path))
37     return;
38
39   XmlReader xml;
40   if (!xml.LoadFile(extracted_path.Append(FILE_PATH_LITERAL("config.xml"))
41                     .MaybeAsASCII())) {
42     LOG(ERROR) << "Unable to load WGT package config.xml file.";
43     return;
44   }
45
46   while (!xml.SkipToElement()) {
47     if (!xml.Read()) {
48       LOG(ERROR) << "Unable to read WGT package config.xml file.";
49       return;
50     }
51   }
52
53   std::string value;
54   while (xml.Read()) {
55     std::string node_name = xml.NodeName();
56     if (node_name == kIdNodeName) {
57       xml.NodeAttribute("id", &value);
58       break;
59     }
60   }
61
62   if (!value.empty()) {
63 #if defined(OS_TIZEN)
64     id_ = value;
65     is_valid_ =
66       SignatureValidator::Check(extracted_path) != SignatureValidator::INVALID;
67 #else
68     id_ = GenerateId(value);
69     is_valid_ = true;
70 #endif
71   }
72   scoped_ptr<base::ScopedFILE> file(
73         new base::ScopedFILE(base::OpenFile(path, "rb")));
74
75   file_ = file.Pass();
76 }
77 }  // namespace application
78 }  // namespace xwalk