Upstream version 11.39.252.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / package / 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/package.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/path_service.h"
11 #include "third_party/zlib/google/zip.h"
12 #include "xwalk/application/common/id_util.h"
13 #include "xwalk/application/common/package/wgt_package.h"
14 #include "xwalk/application/common/package/xpk_package.h"
15
16 namespace xwalk {
17 namespace application {
18
19 Package::Package(const base::FilePath& source_path,
20     Manifest::Type manifest_type)
21     : source_path_(source_path),
22       is_extracted_(false),
23       is_valid_(false),
24       name_(source_path_.BaseName().AsUTF8Unsafe()),
25       manifest_type_(manifest_type) {
26 }
27
28 Package::~Package() {
29 }
30
31 // static
32 scoped_ptr<Package> Package::Create(const base::FilePath& source_path) {
33   if (source_path.MatchesExtension(FILE_PATH_LITERAL(".xpk"))) {
34     scoped_ptr<Package> package(new XPKPackage(source_path));
35     return package.Pass();
36   }
37   if (source_path.MatchesExtension(FILE_PATH_LITERAL(".wgt"))) {
38     scoped_ptr<Package> package(new WGTPackage(source_path));
39     return package.Pass();
40   }
41
42   LOG(ERROR) << "Invalid package type. Only .xpk/.wgt supported now";
43   return scoped_ptr<Package>();
44 }
45
46 bool Package::ExtractToTemporaryDir(base::FilePath* target_path) {
47   if (is_extracted_) {
48     *target_path = temp_dir_.path();
49     return true;
50   }
51
52   if (!CreateTempDirectory()) {
53     LOG(ERROR) << "Can't create a temporary"
54                   "directory for extracting the package content.";
55     return false;
56   }
57
58   if (!zip::Unzip(source_path_, temp_dir_.path())) {
59     LOG(ERROR) << "An error occurred during package extraction";
60     return false;
61   }
62
63   is_extracted_ = true;
64
65   *target_path = temp_dir_.path();
66   return true;
67 }
68
69 bool Package::ExtractTo(const base::FilePath& target_path) {
70   if (!DirectoryExists(target_path)) {
71     LOG(ERROR) << "The directory " << target_path.MaybeAsASCII()
72                << "does not exist";
73     return false;
74   }
75   if (!IsDirectoryEmpty(target_path)) {
76     LOG(ERROR) << "The directory " << target_path.MaybeAsASCII()
77                << "is not empty.";
78     return false;
79   }
80   if (!zip::Unzip(source_path_, target_path)) {
81     LOG(ERROR) << "An error occurred during package extraction";
82     return false;
83   }
84
85   return true;
86 }
87
88 // Create a temporary directory to decompress the zipped package file.
89 // As the package information might already exists under data_path,
90 // it's safer to extract the XPK/WGT file into a temporary directory first.
91 bool Package::CreateTempDirectory() {
92   base::FilePath tmp;
93   PathService::Get(base::DIR_TEMP, &tmp);
94   if (tmp.empty())
95     return false;
96   if (!temp_dir_.CreateUniqueTempDirUnderPath(tmp))
97     return false;
98   return true;
99 }
100
101 }  // namespace application
102 }  // namespace xwalk