Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(609)

Unified Diff: chrome/nacl/pnacl_component_installer.cc

Issue 8348026: Add a component installer for Portable NaCl. Registration of installer is (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/nacl/pnacl_component_installer.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/nacl/pnacl_component_installer.cc
diff --git a/chrome/nacl/pnacl_component_installer.cc b/chrome/nacl/pnacl_component_installer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ada7976a68677bb1e339834ce685a7560979ce3e
--- /dev/null
+++ b/chrome/nacl/pnacl_component_installer.cc
@@ -0,0 +1,239 @@
+// Copyright (c) 2011 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.
+
+#include "chrome/nacl/pnacl_component_installer.h"
+
+#include <string.h>
+
+#include "base/base_paths.h"
+#include "base/compiler_specific.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/path_service.h"
+#include "base/string_util.h"
+#include "base/stringprintf.h"
+#include "base/values.h"
+#include "base/version.h"
+#include "build/build_config.h"
+#include "chrome/browser/component_updater/component_updater_service.h"
+#include "chrome/browser/plugin_prefs.h"
+#include "chrome/common/chrome_paths.h"
+#include "content/browser/browser_thread.h"
+
+namespace {
+
+// CRX hash. This made up extension id is: haoikncodblmdapadglhmehplknoaddl
+// TODO(jvoung): update the ID and hash when we go into production.
+// See tools/crx_id/crx_id.py for gathering this information.
+const uint8 sha256_hash[] =
+ {0x70, 0xe8, 0xad, 0x2e, 0x31, 0xbc, 0x30, 0xf0, 0x36, 0xb7,
+ 0xc4, 0x7f, 0xba, 0xde, 0x03, 0x3b, 0x48, 0xd2, 0xe8, 0x58,
+ 0x04, 0x45, 0xa1, 0x6c, 0xc8, 0xac, 0x86, 0x9e, 0xf1, 0x9b,
+ 0xf6, 0xea};
+
+// One of the PNaCl component files, for checking that expected files exist.
+// TODO(jvoung): perhaps replace this with a file that contains a list of
+// the expected files. Use that to check that everything is unpacked.
+// However, that would make startup detection even slower (need to check for
+// more than one file!).
+const FilePath::CharType kPNaClCompilerFileName[] =
+ FILE_PATH_LITERAL("llc");
+
+// Name of the PNaCl component specified in the manifest.
+const char kPNaClManifestName[] = "PNaCl";
+
+// Name of the PNaCl architecture in the component manifest.
+// NOTE: this can be independent of the Omaha query parameter.
+// TODO(jvoung): will this pre-processor define work with 64-bit Windows?
+// NaCl's sel_ldr is 64-bit, but the browser process is 32-bit.
+// Will the Omaha query do the right thing as well?
+const char kPNaClArch[] =
+#if defined(ARCH_CPU_X86)
+ "x86";
+#elif defined(ARCH_CPU_X86_64)
+ "x64";
+#elif defined(ARCH_CPU_ARMEL)
+ "arm";
+#else
+#error "Unknown Architecture in PNaCl Component Installer."
+#endif
+
+// The PNaCl components are in a directory with this name.
+const FilePath::CharType kPNaClBaseDirectory[] =
+ FILE_PATH_LITERAL("PNaCl");
+
+// If we don't have PNaCl installed, this is the version we claim.
+// TODO(jvoung): Is there a way to trick the configurator to ping the server
+// earlier if there are components that are not yet installed (version 0.0.0.0),
jvoung - send to chromium... 2011/11/11 22:57:34 Would such a change make sense?
cpu_(ooo_6.6-7.5) 2011/11/23 01:39:12 Sounds like an interesting possibility.
+// So that they will be available ASAP? Be careful not to hurt startup speed.
+// Make kNullVersion part of ComponentUpdater in that case, to avoid skew?
+const char kNullVersion[] = "0.0.0.0";
+
+// The base directory on Windows looks like:
+// <profile>\AppData\Local\Google\Chrome\User Data\PNaCl\.
+FilePath GetPNaClBaseDirectory() {
+ FilePath result;
+ PathService::Get(chrome::DIR_USER_DATA, &result);
+ return result.Append(kPNaClBaseDirectory);
+}
+
+// PNaCl components have the version encoded in the path itself
+// so we need to enumerate the directories to find the full path.
+// On success it returns something like:
+// <profile>\AppData\Local\Google\Chrome\User Data\PNaCl\0.1.2.3\.
+//
+// TODO(jvoung): Does it garbage collect old versions when a new version is
+// installed? Do we need the architecture in the path too? That is for handling
+// cases when you share a profile but switch between machine-types.
+bool GetLatestPNaClDirectory(FilePath* result, Version* latest) {
+ *result = GetPNaClBaseDirectory();
+ bool found = false;
+ file_util::FileEnumerator
+ file_enumerator(*result, false, file_util::FileEnumerator::DIRECTORIES);
+ for (FilePath path = file_enumerator.Next(); !path.value().empty();
+ path = file_enumerator.Next()) {
+ Version version(path.BaseName().MaybeAsASCII());
+ if (!version.IsValid())
+ continue;
+ if (version.CompareTo(*latest) > 0) {
+ *latest = version;
+ *result = path;
+ found = true;
+ }
+ }
+ return found;
+}
+
+} // namespace
+
+class PNaClComponentInstaller : public ComponentInstaller {
+ public:
+ explicit PNaClComponentInstaller(const Version& version);
+
+ virtual ~PNaClComponentInstaller() {}
+
+ virtual void OnUpdateError(int error) OVERRIDE;
+
+ virtual bool Install(base::DictionaryValue* manifest,
+ const FilePath& unpack_path) OVERRIDE;
+
+ private:
+ Version current_version_;
+};
+
+PNaClComponentInstaller::PNaClComponentInstaller(
+ const Version& version) : current_version_(version) {
+ DCHECK(version.IsValid());
+}
+
+void PNaClComponentInstaller::OnUpdateError(int error) {
+ NOTREACHED() << "PNaCl update error: " << error;
+}
+
+bool PNaClComponentInstaller::Install(base::DictionaryValue* manifest,
+ const FilePath& unpack_path) {
+ Version version;
+ if (!CheckPNaClComponentManifest(manifest, &version))
+ return false;
+ if (current_version_.CompareTo(version) > 0)
+ return false;
+
+ // Make sure that at least one of the compiler files exists.
+ if (!file_util::PathExists(unpack_path.Append(kPNaClCompilerFileName)))
+ return false;
+
+ // Passed the basic tests. Time to install it.
+ FilePath path =
+ GetPNaClBaseDirectory().AppendASCII(version.GetString());
+ if (file_util::PathExists(path))
+ return false;
+ if (!file_util::Move(unpack_path, path))
+ return false;
+
+ // Installation is done. Now tell the rest of chrome (just the path service
+ // for now). TODO(jvoung): we need notifications if someone surfed to a
+ // PNaCl webpage and PNaCl was just installed at this time. They should
+ // then be able to reload the page and retry (or something).
+ current_version_ = version;
+
+ PathService::Override(chrome::FILE_PNACL_COMPONENT, path);
+ return true;
+}
+
+bool CheckPNaClComponentManifest(base::DictionaryValue* manifest,
+ Version* version_out) {
+ // Make sure we have the right manifest file.
+ std::string name;
+ manifest->GetStringASCII("name", &name);
+ if (name != kPNaClManifestName)
+ return false;
+
+ std::string proposed_version;
+ manifest->GetStringASCII("version", &proposed_version);
+ Version version(proposed_version.c_str());
+ if (!version.IsValid())
+ return false;
+
+ std::string arch;
+ manifest->GetStringASCII("x-pnacl-arch", &arch);
+ if (arch != kPNaClArch)
+ return false;
+
+ *version_out = version;
+ return true;
+}
+
+namespace {
+
+// Finally, do the registration with the right version number.
+void FinishPNaClUpdateRegistration(ComponentUpdateService* cus,
+ const Version& version) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ CrxComponent pnacl;
+ pnacl.name = "pnacl";
+ pnacl.installer = new PNaClComponentInstaller(version);
+ pnacl.version = version;
+ pnacl.pk_hash.assign(sha256_hash, &sha256_hash[sizeof(sha256_hash)]);
+ if (cus->RegisterComponent(pnacl) != ComponentUpdateService::kOk) {
+ NOTREACHED() << "PNaCl component registration failed.";
+ }
+}
+
+// Check if there is an existing version on disk first to know when
+// a hosted version is actually newer.
+void StartPNaClUpdateRegistration(ComponentUpdateService* cus) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ FilePath path = GetPNaClBaseDirectory();
+ if (!file_util::PathExists(path)) {
+ if (!file_util::CreateDirectory(path)) {
+ NOTREACHED() << "Could not create PNaCl directory.";
+ return;
+ }
+ }
+
+ Version version(kNullVersion);
+ if (GetLatestPNaClDirectory(&path, &version)) {
+ // Check if one of the PNaCl files is really there.
+ FilePath compiler_path = path.Append(kPNaClCompilerFileName);
+ if (!file_util::PathExists(compiler_path)) {
+ version = Version(kNullVersion);
+ } else {
+ // Register the existing path for now, before checking for updates.
+ // TODO(jvoung): Will this race with the NaCl plugin in browser tests
+ // if we pre-populate the user profile directory with the components?
jvoung - send to chromium... 2011/11/11 22:57:34 Is this a race we need to worry about for testing
cpu_(ooo_6.6-7.5) 2011/11/23 01:39:12 Not sure, who is pre-populating? other thread? oth
jvoung - send to chromium... 2011/12/14 19:34:31 At the very least we don't want to be pinging serv
+ PathService::Override(chrome::FILE_PNACL_COMPONENT, path);
+ }
+ }
+
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ NewRunnableFunction(&FinishPNaClUpdateRegistration, cus, version));
+}
+
+} // namespace
+
+void RegisterPNaClComponent(ComponentUpdateService* cus) {
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ NewRunnableFunction(&StartPNaClUpdateRegistration, cus));
+}
« no previous file with comments | « chrome/nacl/pnacl_component_installer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698