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