• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. 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 "chrome/browser/component_updater/test/test_installer.h"
6 
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/values.h"
10 
TestInstaller()11 TestInstaller::TestInstaller()
12     : error_(0), install_count_(0) {
13 }
14 
OnUpdateError(int error)15 void TestInstaller::OnUpdateError(int error) {
16   error_ = error;
17 }
18 
Install(const base::DictionaryValue & manifest,const base::FilePath & unpack_path)19 bool TestInstaller::Install(const base::DictionaryValue& manifest,
20                             const base::FilePath& unpack_path) {
21   ++install_count_;
22   return base::DeleteFile(unpack_path, true);
23 }
24 
GetInstalledFile(const std::string & file,base::FilePath * installed_file)25 bool TestInstaller::GetInstalledFile(const std::string& file,
26                                      base::FilePath* installed_file) {
27   return false;
28 }
29 
error() const30 int TestInstaller::error() const { return error_; }
31 
install_count() const32 int TestInstaller::install_count() const { return install_count_; }
33 
34 
ReadOnlyTestInstaller(const base::FilePath & install_dir)35 ReadOnlyTestInstaller::ReadOnlyTestInstaller(const base::FilePath& install_dir)
36     : install_directory_(install_dir) {
37 }
38 
~ReadOnlyTestInstaller()39 ReadOnlyTestInstaller::~ReadOnlyTestInstaller() {
40 }
41 
GetInstalledFile(const std::string & file,base::FilePath * installed_file)42 bool ReadOnlyTestInstaller::GetInstalledFile(const std::string& file,
43                                              base::FilePath* installed_file) {
44   *installed_file = install_directory_.AppendASCII(file);
45   return true;
46 }
47 
48 
VersionedTestInstaller()49 VersionedTestInstaller::VersionedTestInstaller() {
50   base::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &install_directory_);
51 }
52 
~VersionedTestInstaller()53 VersionedTestInstaller::~VersionedTestInstaller() {
54   base::DeleteFile(install_directory_, true);
55 }
56 
57 
Install(const base::DictionaryValue & manifest,const base::FilePath & unpack_path)58 bool VersionedTestInstaller::Install(const base::DictionaryValue& manifest,
59                                      const base::FilePath& unpack_path) {
60   std::string version_string;
61   manifest.GetStringASCII("version", &version_string);
62   Version version(version_string.c_str());
63 
64   base::FilePath path;
65   path = install_directory_.AppendASCII(version.GetString());
66   base::CreateDirectory(path.DirName());
67   if (!base::Move(unpack_path, path))
68     return false;
69   current_version_ = version;
70   ++install_count_;
71   return true;
72 }
73 
GetInstalledFile(const std::string & file,base::FilePath * installed_file)74 bool VersionedTestInstaller::GetInstalledFile(const std::string& file,
75                                               base::FilePath* installed_file) {
76   base::FilePath path;
77   path = install_directory_.AppendASCII(current_version_.GetString());
78   *installed_file = path.Append(base::FilePath::FromUTF8Unsafe(file));
79   return true;
80 }
81