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