summaryrefslogtreecommitdiffstats
path: root/extensions/browser/updater/update_install_shim.cc
blob: 4b6f58294aab5720e81839c0dc90c9363f591e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/updater/update_install_shim.h"

#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "content/public/browser/browser_thread.h"

namespace extensions {

UpdateInstallShim::UpdateInstallShim(std::string extension_id,
                                     const base::FilePath& extension_root,
                                     const UpdateInstallShimCallback& callback)
    : extension_id_(extension_id),
      extension_root_(extension_root),
      callback_(callback) {}

void UpdateInstallShim::OnUpdateError(int error) {
  VLOG(1) << "OnUpdateError (" << extension_id_ << ") " << error;
}

bool UpdateInstallShim::Install(const base::DictionaryValue& manifest,
                                const base::FilePath& unpack_path) {
  base::ScopedTempDir temp_dir;
  if (!temp_dir.CreateUniqueTempDir())
    return false;

  // The UpdateClient code will delete unpack_path if it still exists after
  // this method is done, so we rename it on top of our temp dir.
  if (!base::DeleteFile(temp_dir.path(), true) ||
      !base::Move(unpack_path, temp_dir.path())) {
    LOG(ERROR) << "Trying to install update for " << extension_id_
               << "and failed to move " << unpack_path.value() << " to  "
               << temp_dir.path().value();
    return false;
  }
  content::BrowserThread::PostTask(
      content::BrowserThread::UI, FROM_HERE,
      base::Bind(&UpdateInstallShim::RunCallbackOnUIThread, this,
                 temp_dir.Take()));
  return true;
}

bool UpdateInstallShim::GetInstalledFile(const std::string& file,
                                         base::FilePath* installed_file) {
  base::FilePath relative_path = base::FilePath::FromUTF8Unsafe(file);
  if (relative_path.IsAbsolute() || relative_path.ReferencesParent())
    return false;
  *installed_file = extension_root_.Append(relative_path);
  if (!extension_root_.IsParent(*installed_file) ||
      !base::PathExists(*installed_file)) {
    VLOG(1) << "GetInstalledFile failed to find " << installed_file->value();
    installed_file->clear();
    return false;
  }
  return true;
}

bool UpdateInstallShim::Uninstall() {
  NOTREACHED();
  return false;
}

UpdateInstallShim::~UpdateInstallShim() {}

void UpdateInstallShim::RunCallbackOnUIThread(const base::FilePath& temp_dir) {
  if (callback_.is_null()) {
    content::BrowserThread::PostBlockingPoolTask(
        FROM_HERE, base::Bind(base::IgnoreResult(&base::DeleteFile), temp_dir,
                              true /*recursive */));
    return;
  }
  callback_.Run(extension_id_, temp_dir);
}

}  // namespace extensions