Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / installer / 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/installer/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/installer/wgt_package.h"
14 #include "xwalk/application/common/installer/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 }
24
25 Package::~Package() {
26 }
27
28 // static
29 scoped_ptr<Package> Package::Create(const base::FilePath& source_path) {
30   if (source_path.MatchesExtension(FILE_PATH_LITERAL(".xpk"))) {
31       scoped_ptr<Package> package(new XPKPackage(source_path));
32       if (!package->IsValid())
33         LOG(ERROR) << "Package not valid";
34       return package.Pass();
35   } else 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::Extract(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 // Create a temporary directory to decompress the zipped package file.
68 // As the package information might already exists under data_path,
69 // it's safer to extract the XPK/WGT file into a temporary directory first.
70 bool Package::CreateTempDirectory() {
71   base::FilePath tmp;
72   PathService::Get(base::DIR_TEMP, &tmp);
73   if (tmp.empty())
74     return false;
75   if (!temp_dir_.CreateUniqueTempDirUnderPath(tmp))
76     return false;
77   return true;
78 }
79
80 }  // namespace application
81 }  // namespace xwalk