1eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Copyright 2013 The Chromium Authors. All rights reserved.
2eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// found in the LICENSE file.
4eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/component_updater/test/test_installer.h"
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
7010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include <string>
8010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
9eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/files/file_path.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/values.h"
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace component_updater {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
15010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)TestInstaller::TestInstaller() : error_(0), install_count_(0) {
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
17eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
18eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid TestInstaller::OnUpdateError(int error) {
19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  error_ = error;
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool TestInstaller::Install(const base::DictionaryValue& manifest,
23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                            const base::FilePath& unpack_path) {
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ++install_count_;
257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  return base::DeleteFile(unpack_path, true);
26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
27eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool TestInstaller::GetInstalledFile(const std::string& file,
29eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                     base::FilePath* installed_file) {
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return false;
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)int TestInstaller::error() const {
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return error_;
35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
37010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)int TestInstaller::install_count() const {
38010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return install_count_;
39010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochReadOnlyTestInstaller::ReadOnlyTestInstaller(const base::FilePath& install_dir)
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    : install_directory_(install_dir) {
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochReadOnlyTestInstaller::~ReadOnlyTestInstaller() {
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool ReadOnlyTestInstaller::GetInstalledFile(const std::string& file,
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                             base::FilePath* installed_file) {
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  *installed_file = install_directory_.AppendASCII(file);
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
53eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
54eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochVersionedTestInstaller::VersionedTestInstaller() {
55a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &install_directory_);
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
58eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochVersionedTestInstaller::~VersionedTestInstaller() {
597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  base::DeleteFile(install_directory_, true);
60eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
61eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
62eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool VersionedTestInstaller::Install(const base::DictionaryValue& manifest,
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                     const base::FilePath& unpack_path) {
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::string version_string;
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  manifest.GetStringASCII("version", &version_string);
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  Version version(version_string.c_str());
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::FilePath path;
69eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  path = install_directory_.AppendASCII(version.GetString());
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::CreateDirectory(path.DirName());
71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!base::Move(unpack_path, path))
72eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  current_version_ = version;
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ++install_count_;
75eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
76eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
77eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
78eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool VersionedTestInstaller::GetInstalledFile(const std::string& file,
79eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                                              base::FilePath* installed_file) {
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::FilePath path;
81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  path = install_directory_.AppendASCII(current_version_.GetString());
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  *installed_file = path.Append(base::FilePath::FromUTF8Unsafe(file));
83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return true;
84eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace component_updater
87