- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / recovery_component_installer.cc
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.
4
5 #include "chrome/browser/component_updater/recovery_component_installer.h"
6
7 #include "base/base_paths.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/compiler_specific.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/path_service.h"
15 #include "base/prefs/pref_registry_simple.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/process/launch.h"
18 #include "base/strings/string_util.h"
19 #include "base/values.h"
20 #include "chrome/browser/component_updater/component_updater_service.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/browser_thread.h"
24
25 using content::BrowserThread;
26
27 namespace {
28
29 // CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
30 const uint8 kSha2Hash[] = {0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c,
31                            0xbc, 0xc3, 0x4b, 0x29, 0x12, 0xf3, 0x9e, 0x2c,
32                            0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c, 0x80, 0x1c,
33                            0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7};
34
35 // File name of the recovery binary on different platforms.
36 const base::FilePath::CharType kRecoveryFileName[] =
37 #if defined(OS_WIN)
38     FILE_PATH_LITERAL("ChromeRecovery.exe");
39 #else  // OS_LINUX, OS_MACOSX, etc.
40     FILE_PATH_LITERAL("ChromeRecovery");
41 #endif
42
43 const char kRecoveryManifestName[] = "ChromeRecovery";
44
45 }  // namespace
46
47 class RecoveryComponentInstaller : public ComponentInstaller {
48  public:
49   explicit RecoveryComponentInstaller(const Version& version,
50                                       PrefService* prefs);
51
52   virtual ~RecoveryComponentInstaller() {}
53
54   virtual void OnUpdateError(int error) OVERRIDE;
55
56   virtual bool Install(const base::DictionaryValue& manifest,
57                        const base::FilePath& unpack_path) OVERRIDE;
58
59   virtual bool GetInstalledFile(const std::string& file,
60                                 base::FilePath* installed_file) OVERRIDE;
61
62  private:
63   Version current_version_;
64   PrefService* prefs_;
65 };
66
67 void RecoveryRegisterHelper(ComponentUpdateService* cus,
68                             PrefService* prefs) {
69   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
70   Version version(prefs->GetString(prefs::kRecoveryComponentVersion));
71   if (!version.IsValid()) {
72     NOTREACHED();
73     return;
74   }
75
76   CrxComponent recovery;
77   recovery.name = "recovery";
78   recovery.installer = new RecoveryComponentInstaller(version, prefs);
79   recovery.version = version;
80   recovery.pk_hash.assign(kSha2Hash, &kSha2Hash[sizeof(kSha2Hash)]);
81   if (cus->RegisterComponent(recovery) != ComponentUpdateService::kOk) {
82     NOTREACHED() << "Recovery component registration failed.";
83   }
84 }
85
86 void RecoveryUpdateVersionHelper(const Version& version, PrefService* prefs) {
87   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
88   prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString());
89 }
90
91 RecoveryComponentInstaller::RecoveryComponentInstaller(
92       const Version& version, PrefService* prefs)
93     : current_version_(version), prefs_(prefs){
94   DCHECK(version.IsValid());
95 }
96
97 void RecoveryComponentInstaller::OnUpdateError(int error) {
98   NOTREACHED() << "Recovery component update error: " << error;
99 }
100
101 bool RecoveryComponentInstaller::Install(const base::DictionaryValue& manifest,
102                                          const base::FilePath& unpack_path) {
103   std::string name;
104   manifest.GetStringASCII("name", &name);
105   if (name != kRecoveryManifestName)
106     return false;
107   std::string proposed_version;
108   manifest.GetStringASCII("version", &proposed_version);
109   Version version(proposed_version.c_str());
110   if (!version.IsValid())
111     return false;
112   if (current_version_.CompareTo(version) >= 0)
113     return false;
114   base::FilePath main_file = unpack_path.Append(kRecoveryFileName);
115   if (!base::PathExists(main_file))
116     return false;
117   // Passed the basic tests. The installation continues with the
118   // recovery component itself running from the temp directory.
119   CommandLine cmdline(main_file);
120   std::string arguments;
121   if (manifest.GetStringASCII("x-recovery-args", &arguments))
122     cmdline.AppendArg(arguments);
123   std::string add_version;
124   if (manifest.GetStringASCII("x-recovery-add-version", &add_version)) {
125     if (add_version == "yes")
126       cmdline.AppendSwitchASCII("version", current_version_.GetString());
127   }
128   current_version_ = version;
129   if (prefs_) {
130     BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
131         base::Bind(&RecoveryUpdateVersionHelper, version, prefs_));
132   }
133   return base::LaunchProcess(cmdline, base::LaunchOptions(), NULL);
134 }
135
136 bool RecoveryComponentInstaller::GetInstalledFile(
137     const std::string& file, base::FilePath* installed_file) {
138   return false;
139 }
140
141 void RegisterRecoveryComponent(ComponentUpdateService* cus,
142                                PrefService* prefs) {
143 #if !defined(OS_CHROMEOS)
144   // We delay execute the registration because we are not required in
145   // the critical path during browser startup.
146   BrowserThread::PostDelayedTask(
147       BrowserThread::UI,
148       FROM_HERE,
149       base::Bind(&RecoveryRegisterHelper, cus, prefs),
150       base::TimeDelta::FromSeconds(6));
151 #endif
152 }
153
154 void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) {
155   registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0");
156 }