summaryrefslogtreecommitdiffstats
path: root/extensions/browser/updater/update_install_shim.h
blob: 518866a6062c2fd316621bde2bc2d30a03f3fb02 (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
// 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.

#ifndef EXTENSIONS_BROWSER_UPDATER_UPDATE_INSTALL_SHIM_H_
#define EXTENSIONS_BROWSER_UPDATER_UPDATE_INSTALL_SHIM_H_

#include <string>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "components/update_client/update_client.h"

namespace base {
class DictionaryValue;
}

namespace extensions {

// A callback to implement the install of a new version of the extension.
// Takes ownership of the directory at |temp_dir|.
using UpdateInstallShimCallback =
    base::Callback<void(const std::string& extension_id,
                        const base::FilePath& temp_dir)>;

// This class is used as a shim between the components::update_client and
// extensions code, to help the generic update_client code prepare and then
// install an updated version of an extension. Because the update_client code
// doesn't have the notion of extension ids, we use instances of this class to
// map an install request back to the original update check for a given
// extension.
class UpdateInstallShim : public update_client::CrxInstaller {
 public:
  // This method takes the id and root directory for an extension we're doing
  // an update check for, as well as a callback to call if we get a new version
  // of it to install.
  UpdateInstallShim(std::string extension_id,
                    const base::FilePath& extension_root,
                    const UpdateInstallShimCallback& callback);

  // Called when an update attempt failed.
  void OnUpdateError(int error) override;

  // This is called when a new version of an extension is unpacked at
  // |unpack_path| and is ready for install.
  bool Install(const base::DictionaryValue& manifest,
               const base::FilePath& unpack_path) override;

  // This is called by the generic differential update code in the
  // update_client to provide the path to an existing file in the current
  // version of the extension, so that it can be copied (or serve as the input
  // to diff-patching) with output going to the directory with the new version
  // being staged on disk for install.
  bool GetInstalledFile(const std::string& file,
                        base::FilePath* installed_file) override;

  // This method is not relevant to extension updating.
  bool Uninstall() override;

 private:
  friend class base::RefCountedThreadSafe<UpdateInstallShim>;
  ~UpdateInstallShim() override;

  // Takes ownership of the directory at path |temp_dir|.
  void RunCallbackOnUIThread(const base::FilePath& temp_dir);

  std::string extension_id_;
  base::FilePath extension_root_;
  UpdateInstallShimCallback callback_;

  DISALLOW_COPY_AND_ASSIGN(UpdateInstallShim);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_UPDATER_UPDATE_INSTALL_SHIM_H_