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 : source_path_(source_path),
23 name_(source_path_.BaseName().AsUTF8Unsafe()) {
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();
35 if (source_path.MatchesExtension(FILE_PATH_LITERAL(".wgt"))) {
36 scoped_ptr<Package> package(new WGTPackage(source_path));
37 return package.Pass();
40 LOG(ERROR) << "Invalid package type. Only .xpk/.wgt supported now";
41 return scoped_ptr<Package>();
44 bool Package::ExtractToTemporaryDir(base::FilePath* target_path) {
46 *target_path = temp_dir_.path();
50 if (!CreateTempDirectory()) {
51 LOG(ERROR) << "Can't create a temporary"
52 "directory for extracting the package content.";
56 if (!zip::Unzip(source_path_, temp_dir_.path())) {
57 LOG(ERROR) << "An error occurred during package extraction";
63 *target_path = temp_dir_.path();
67 bool Package::ExtractTo(const base::FilePath& target_path) {
68 if (!DirectoryExists(target_path)) {
69 LOG(ERROR) << "The directory " << target_path.MaybeAsASCII()
73 if (!IsDirectoryEmpty(target_path)) {
74 LOG(ERROR) << "The directory " << target_path.MaybeAsASCII()
78 if (!zip::Unzip(source_path_, target_path)) {
79 LOG(ERROR) << "An error occurred during package extraction";
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() {
91 PathService::Get(base::DIR_TEMP, &tmp);
94 if (!temp_dir_.CreateUniqueTempDirUnderPath(tmp))
99 } // namespace application