Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / component_updater / default_component_installer.h
blobf76bc837ebe24b78c976c77cc752c24b02bfb5e4
1 // Copyright 2014 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.
5 #ifndef COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_
6 #define COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/threading/thread_checker.h"
18 #include "base/values.h"
19 #include "base/version.h"
20 #include "components/update_client/update_client.h"
22 namespace base {
23 class FilePath;
24 class SequencedTaskRunner;
25 class SingleThreadTaskRunner;
26 } // namespace base
28 namespace component_updater {
30 class ComponentUpdateService;
32 // Components should use a DefaultComponentInstaller by defining a class that
33 // implements the members of ComponentInstallerTraits, and then registering a
34 // DefaultComponentInstaller that has been constructed with an instance of that
35 // class.
36 class ComponentInstallerTraits {
37 public:
38 virtual ~ComponentInstallerTraits();
40 // Verifies that a working installation resides within the directory specified
41 // by |install_dir|. |install_dir| is of the form <base directory>/<version>.
42 // |manifest| should have been read from the manifest file in |install_dir|.
43 // Called only from a thread belonging to a blocking thread pool.
44 // The implementation of this function must be efficient since the function
45 // can be called when Chrome starts.
46 virtual bool VerifyInstallation(const base::DictionaryValue& manifest,
47 const base::FilePath& install_dir) const = 0;
49 // Returns true if the component can be automatically updated. Called once
50 // during component registration from the UI thread.
51 virtual bool CanAutoUpdate() const = 0;
53 // OnCustomInstall is called during the installation process. Components that
54 // require custom installation operations should implement them here.
55 // Returns false if a custom operation failed, and true otherwise.
56 // Called only from a thread belonging to a blocking thread pool.
57 virtual bool OnCustomInstall(const base::DictionaryValue& manifest,
58 const base::FilePath& install_dir) = 0;
60 // ComponentReady is called in two cases:
61 // 1) After an installation is successfully completed.
62 // 2) During component registration if the component is already installed.
63 // In both cases the install is verified before this is called. This method
64 // is guaranteed to be called before any observers of the component are
65 // notified of a successful install, and is meant to support follow-on work
66 // such as updating paths elsewhere in Chrome. Called on the UI thread.
67 // |version| is the version of the component.
68 // |install_dir| is the path to the install directory for this version.
69 // |manifest| is the manifest for this version of the component.
70 virtual void ComponentReady(const base::Version& version,
71 const base::FilePath& install_dir,
72 scoped_ptr<base::DictionaryValue> manifest) = 0;
74 // Returns the directory that the installer will place versioned installs of
75 // the component into.
76 virtual base::FilePath GetBaseDirectory() const = 0;
78 // Returns the component's SHA2 hash as raw bytes.
79 virtual void GetHash(std::vector<uint8_t>* hash) const = 0;
81 // Returns the human-readable name of the component.
82 virtual std::string GetName() const = 0;
85 // A DefaultComponentInstaller is intended to be final, and not derived from.
86 // Customization must be provided by passing a ComponentInstallerTraits object
87 // to the constructor.
88 class DefaultComponentInstaller : public update_client::CrxInstaller {
89 public:
90 DefaultComponentInstaller(
91 scoped_ptr<ComponentInstallerTraits> installer_traits);
93 // Registers the component for update checks and installs.
94 // The passed |callback| will be called once the initial check for installed
95 // versions is done and the component has been registered.
96 void Register(ComponentUpdateService* cus, const base::Closure& callback);
98 // Overridden from ComponentInstaller:
99 void OnUpdateError(int error) override;
100 bool Install(const base::DictionaryValue& manifest,
101 const base::FilePath& unpack_path) override;
102 bool GetInstalledFile(const std::string& file,
103 base::FilePath* installed_file) override;
104 bool Uninstall() override;
106 private:
107 ~DefaultComponentInstaller() override;
109 base::FilePath GetInstallDirectory();
110 bool InstallHelper(const base::DictionaryValue& manifest,
111 const base::FilePath& unpack_path,
112 const base::FilePath& install_path);
113 void StartRegistration(ComponentUpdateService* cus);
114 void FinishRegistration(ComponentUpdateService* cus,
115 const base::Closure& callback);
116 void ComponentReady(scoped_ptr<base::DictionaryValue> manifest);
117 void UninstallOnTaskRunner();
119 base::Version current_version_;
120 std::string current_fingerprint_;
121 scoped_ptr<base::DictionaryValue> current_manifest_;
122 scoped_ptr<ComponentInstallerTraits> installer_traits_;
123 scoped_refptr<base::SequencedTaskRunner> task_runner_;
125 // Used to post responses back to the main thread. Initialized on the main
126 // loop but accessed from the task runner.
127 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
129 base::ThreadChecker thread_checker_;
131 DISALLOW_COPY_AND_ASSIGN(DefaultComponentInstaller);
134 } // namespace component_updater
136 #endif // COMPONENTS_COMPONENT_UPDATER_DEFAULT_COMPONENT_INSTALLER_H_