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.
5 #include "xwalk/application/common/package/package.h"
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"
17 namespace application {
19 Package::Package(const base::FilePath& source_path,
20 Manifest::Type manifest_type)
21 : source_path_(source_path),
24 name_(source_path_.BaseName().AsUTF8Unsafe()),
25 manifest_type_(manifest_type) {
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();
37 if (source_path.MatchesExtension(FILE_PATH_LITERAL(".wgt"))) {
38 scoped_ptr<Package> package(new WGTPackage(source_path));
39 return package.Pass();
42 LOG(ERROR) << "Invalid package type. Only .xpk/.wgt supported now";
43 return scoped_ptr<Package>();
46 bool Package::ExtractToTemporaryDir(base::FilePath* target_path) {
48 *target_path = temp_dir_.path();
52 if (!CreateTempDirectory()) {
53 LOG(ERROR) << "Can't create a temporary"
54 "directory for extracting the package content.";
58 if (!zip::Unzip(source_path_, temp_dir_.path())) {
59 LOG(ERROR) << "An error occurred during package extraction";
65 *target_path = temp_dir_.path();
69 bool Package::ExtractTo(const base::FilePath& target_path) {
70 if (!DirectoryExists(target_path)) {
71 LOG(ERROR) << "The directory " << target_path.MaybeAsASCII()
75 if (!IsDirectoryEmpty(target_path)) {
76 LOG(ERROR) << "The directory " << target_path.MaybeAsASCII()
80 if (!zip::Unzip(source_path_, target_path)) {
81 LOG(ERROR) << "An error occurred during package extraction";
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() {
93 PathService::Get(base::DIR_TEMP, &tmp);
96 if (!temp_dir_.CreateUniqueTempDirUnderPath(tmp))
101 } // namespace application