1447ca3ef6f9c34636da8314fedbba2bc9561778
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / default_component_installer.cc
1 // Copyright 2013 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 "base/bind.h"
6 #include "base/file_util.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_path.h"
9 #include "base/values.h"
10 #include "base/version.h"
11 // TODO(ddorwin): Find a better place for ReadManifest.
12 #include "chrome/browser/component_updater/component_unpacker.h"
13 #include "chrome/browser/component_updater/default_component_installer.h"
14 #include "content/public/browser/browser_thread.h"
15
16 namespace component_updater {
17
18 namespace {
19 // Version "0" corresponds to no installed version. By the server's conventions,
20 // we represent it as a dotted quad.
21 const char kNullVersion[] = "0.0.0.0";
22 }  // namespace
23
24 ComponentInstallerTraits::~ComponentInstallerTraits() {
25 }
26
27 DefaultComponentInstaller::DefaultComponentInstaller(
28     scoped_ptr<ComponentInstallerTraits> installer_traits)
29     : current_version_(kNullVersion) {
30   installer_traits_ = installer_traits.Pass();
31 }
32
33 DefaultComponentInstaller::~DefaultComponentInstaller() {
34 }
35
36 void DefaultComponentInstaller::Register(ComponentUpdateService* cus) {
37   if (!installer_traits_) {
38     NOTREACHED() << "A DefaultComponentInstaller has been created but "
39                  << "has no installer traits.";
40     return;
41   }
42   content::BrowserThread::PostBlockingPoolTask(
43       FROM_HERE,
44       base::Bind(&DefaultComponentInstaller::StartRegistration,
45                  base::Unretained(this),
46                  cus));
47 }
48
49 void DefaultComponentInstaller::OnUpdateError(int error) {
50   NOTREACHED() << "Component update error: " << error;
51 }
52
53 bool DefaultComponentInstaller::InstallHelper(
54     const base::DictionaryValue& manifest,
55     const base::FilePath& unpack_path,
56     const base::FilePath& install_path) {
57   if (!base::Move(unpack_path, install_path))
58     return false;
59   if (!installer_traits_->OnCustomInstall(manifest, install_path))
60     return false;
61   if (!installer_traits_->VerifyInstallation(install_path))
62     return false;
63   return true;
64 }
65
66 bool DefaultComponentInstaller::Install(const base::DictionaryValue& manifest,
67                                         const base::FilePath& unpack_path) {
68   std::string manifest_version;
69   manifest.GetStringASCII("version", &manifest_version);
70   base::Version version(manifest_version.c_str());
71   if (!version.IsValid())
72     return false;
73   if (current_version_.CompareTo(version) > 0)
74     return false;
75   base::FilePath install_path =
76       installer_traits_->GetBaseDirectory().AppendASCII(version.GetString());
77   if (base::PathExists(install_path)) {
78     if (!base::DeleteFile(install_path, true))
79       return false;
80   }
81   if (!InstallHelper(manifest, unpack_path, install_path)) {
82     base::DeleteFile(install_path, true);
83     return false;
84   }
85   current_version_ = version;
86   // TODO(ddorwin): Change the parameter to scoped_ptr<base::DictionaryValue>
87   // so we can avoid this DeepCopy.
88   current_manifest_.reset(manifest.DeepCopy());
89   scoped_ptr<base::DictionaryValue> manifest_copy(
90       current_manifest_->DeepCopy());
91   content::BrowserThread::PostTask(
92       content::BrowserThread::UI, FROM_HERE,
93       base::Bind(&ComponentInstallerTraits::ComponentReady,
94                  base::Unretained(installer_traits_.get()),
95                  current_version_,
96                  GetInstallDirectory(),
97                  base::Passed(&manifest_copy)));
98   return true;
99 }
100
101 bool DefaultComponentInstaller::GetInstalledFile(
102     const std::string& file,
103     base::FilePath* installed_file) {
104   if (current_version_.Equals(base::Version(kNullVersion)))
105     return false;  // No component has been installed yet.
106
107   *installed_file =
108       installer_traits_->GetBaseDirectory()
109           .AppendASCII(current_version_.GetString()).AppendASCII(file);
110   return true;
111 }
112
113 void DefaultComponentInstaller::StartRegistration(
114     ComponentUpdateService* cus) {
115   base::FilePath base_dir = installer_traits_->GetBaseDirectory();
116   if (!base::PathExists(base_dir) &&
117       !base::CreateDirectory(base_dir)) {
118     NOTREACHED() << "Could not create the base directory for "
119                  << installer_traits_->GetName() << " ("
120                  << base_dir.MaybeAsASCII() << ").";
121     return;
122   }
123
124   base::FilePath latest_dir;
125   base::Version latest_version(kNullVersion);
126   std::vector<base::FilePath> older_dirs;
127   bool found = false;
128   base::FileEnumerator file_enumerator(
129       base_dir, false, base::FileEnumerator::DIRECTORIES);
130   for (base::FilePath path = file_enumerator.Next();
131        !path.value().empty();
132        path = file_enumerator.Next()) {
133     base::Version version(path.BaseName().MaybeAsASCII());
134     // Ignore folders that don't have valid version names. These folders are not
135     // managed by component installer so do not try to remove them.
136     if (!version.IsValid())
137       continue;
138     if (!installer_traits_->VerifyInstallation(path)) {
139       older_dirs.push_back(path);
140       continue;
141     }
142     if (found) {
143       if (version.CompareTo(latest_version) > 0) {
144         older_dirs.push_back(latest_dir);
145         latest_dir = path;
146         latest_version = version;
147       } else {
148         older_dirs.push_back(path);
149       }
150     } else {
151       latest_dir = path;
152       latest_version = version;
153       found = true;
154     }
155   }
156
157   if (found) {
158     current_version_ = latest_version;
159     // TODO(ddorwin): Remove these members and pass them directly to
160     // FinishRegistration().
161     base::ReadFileToString(latest_dir.AppendASCII("manifest.fingerprint"),
162                            &current_fingerprint_);
163     current_manifest_= ReadManifest(latest_dir);
164     if (!current_manifest_) {
165       DLOG(ERROR) << "Failed to read manifest for "
166                   << installer_traits_->GetName() << " ("
167                   << base_dir.MaybeAsASCII() << ").";
168       return;
169     }
170   }
171
172   // Remove older versions of the component. None should be in use during
173   // browser startup.
174   for (std::vector<base::FilePath>::iterator iter = older_dirs.begin();
175        iter != older_dirs.end(); ++iter) {
176    base::DeleteFile(*iter, true);
177   }
178
179   content::BrowserThread::PostTask(
180       content::BrowserThread::UI, FROM_HERE,
181       base::Bind(&DefaultComponentInstaller::FinishRegistration,
182                  base::Unretained(this),
183                  cus));
184 }
185
186 base::FilePath DefaultComponentInstaller::GetInstallDirectory() {
187   return installer_traits_->GetBaseDirectory()
188       .AppendASCII(current_version_.GetString());
189 }
190
191 void DefaultComponentInstaller::FinishRegistration(
192     ComponentUpdateService* cus) {
193   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
194   if (installer_traits_->CanAutoUpdate()) {
195     CrxComponent crx;
196     crx.name = installer_traits_->GetName();
197     crx.installer = this;
198     crx.version = current_version_;
199     crx.fingerprint = current_fingerprint_;
200     installer_traits_->GetHash(&crx.pk_hash);
201     ComponentUpdateService::Status status = cus->RegisterComponent(crx);
202     if (status != ComponentUpdateService::kOk &&
203         status != ComponentUpdateService::kReplaced) {
204       NOTREACHED() << "Component registration failed for "
205                    << installer_traits_->GetName();
206       return;
207     }
208   }
209
210   if (current_version_.CompareTo(base::Version(kNullVersion)) > 0) {
211     scoped_ptr<base::DictionaryValue> manifest_copy(
212         current_manifest_->DeepCopy());
213     installer_traits_->ComponentReady(
214         current_version_,
215         GetInstallDirectory(),
216         manifest_copy.Pass());
217   }
218 }
219
220 }  // namespace component_updater
221