Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / component_updater / recovery_component_installer.cc
blobafebd6ff53e13664a358d835a56f344937e11fe7
1 // Copyright (c) 2012 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 #include "chrome/browser/component_updater/recovery_component_installer.h"
7 #include <string>
9 #include "base/base_paths.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/path_service.h"
17 #include "base/prefs/pref_registry_simple.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/process/launch.h"
20 #include "base/strings/string_util.h"
21 #include "base/values.h"
22 #include "components/component_updater/component_updater_paths.h"
23 #include "components/component_updater/component_updater_service.h"
24 #include "components/component_updater/pref_names.h"
25 #include "content/public/browser/browser_thread.h"
27 using content::BrowserThread;
29 namespace component_updater {
31 namespace {
33 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
34 const uint8 kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c,
35 0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c,
36 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c,
37 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7};
39 // File name of the recovery binary on different platforms.
40 const base::FilePath::CharType kRecoveryFileName[] =
41 #if defined(OS_WIN)
42 FILE_PATH_LITERAL("ChromeRecovery.exe");
43 #else // OS_LINUX, OS_MACOSX, etc.
44 FILE_PATH_LITERAL("ChromeRecovery");
45 #endif
47 const char kRecoveryManifestName[] = "ChromeRecovery";
49 } // namespace
51 class RecoveryComponentInstaller : public ComponentInstaller {
52 public:
53 explicit RecoveryComponentInstaller(const Version& version,
54 PrefService* prefs);
56 virtual ~RecoveryComponentInstaller() {}
58 virtual void OnUpdateError(int error) OVERRIDE;
60 virtual bool Install(const base::DictionaryValue& manifest,
61 const base::FilePath& unpack_path) OVERRIDE;
63 virtual bool GetInstalledFile(const std::string& file,
64 base::FilePath* installed_file) OVERRIDE;
66 private:
67 Version current_version_;
68 PrefService* prefs_;
71 void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
73 Version version(prefs->GetString(prefs::kRecoveryComponentVersion));
74 if (!version.IsValid()) {
75 NOTREACHED();
76 return;
79 CrxComponent recovery;
80 recovery.name = "recovery";
81 recovery.installer = new RecoveryComponentInstaller(version, prefs);
82 recovery.version = version;
83 recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
84 if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) {
85 NOTREACHED() << "Recovery component registration failed.";
89 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
91 prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString());
94 RecoveryComponentInstaller::RecoveryComponentInstaller(const Version& version,
95 PrefService* prefs)
96 : current_version_(version), prefs_(prefs) {
97 DCHECK(version.IsValid());
100 void RecoveryComponentInstaller::OnUpdateError(int error) {
101 NOTREACHED() << "Recovery component update error: " << error;
104 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest,
105 const base::FilePath& unpack_path) {
106 std::string name;
107 manifest.GetStringASCII("name", &name);
108 if (name != kRecoveryManifestName)
109 return false;
110 std::string proposed_version;
111 manifest.GetStringASCII("version", &proposed_version);
112 Version version(proposed_version.c_str());
113 if (!version.IsValid())
114 return false;
115 if (current_version_.CompareTo(version) >= 0)
116 return false;
118 // Passed the basic tests. Copy the installation to a permanent directory.
119 base::FilePath path;
120 if (!PathService::Get(DIR_RECOVERY_BASE, &path))
121 return false;
122 path = path.AppendASCII(version.GetString());
123 if (base::PathExists(path) && !base::DeleteFile(path, true))
124 return false;
125 if (!base::Move(unpack_path, path)) {
126 DVLOG(1) << "Recovery component move failed.";
127 return false;
130 base::FilePath main_file = path.Append(kRecoveryFileName);
131 if (!base::PathExists(main_file))
132 return false;
133 // Run the recovery component.
134 CommandLine cmdline(main_file);
135 std::string arguments;
136 if (manifest.GetStringASCII("x-recovery-args", &arguments))
137 cmdline.AppendArg(arguments);
138 std::string add_version;
139 if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) {
140 if (add_version == "yes")
141 cmdline.AppendSwitchASCII("version", current_version_.GetString());
143 current_version_ = version;
144 if (prefs_) {
145 BrowserThread::PostTask(
146 BrowserThread::UI,
147 FROM_HERE,
148 base::Bind(&RecoveryUpdateVersionHelper, version, prefs_));
150 return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL);
153 bool RecoveryComponentInstaller::GetInstalledFile(
154 const std::string& file,
155 base::FilePath* installed_file) {
156 return false;
159 void RegisterRecoveryComponent(ComponentUpdateService* cus,
160 PrefService* prefs) {
161 #if !defined(OS_CHROMEOS)
162 // We delay execute the registration because we are not required in
163 // the critical path during browser startup.
164 BrowserThread::PostDelayedTask(
165 BrowserThread::UI,
166 FROM_HERE,
167 base::Bind(&RecoveryRegisterHelper, cus, prefs),
168 base::TimeDelta::FromSeconds(6));
169 #endif
172 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) {
173 registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0");
176 } // namespace component_updater