Index: chrome/common/component_flash_hint_file.cc |
diff --git a/chrome/common/component_flash_hint_file.cc b/chrome/common/component_flash_hint_file.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..77071155967245dacc66b325205b0706ed0c7d4e |
--- /dev/null |
+++ b/chrome/common/component_flash_hint_file.cc |
@@ -0,0 +1,187 @@ |
+// Copyright 2015 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/common/component_flash_hint_file.h" |
+ |
+#include "base/base64.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/files/important_file_writer.h" |
+#include "base/files/memory_mapped_file.h" |
+#include "base/json/json_string_value_serializer.h" |
+#include "base/path_service.h" |
+#include "base/stl_util.h" |
+#include "base/values.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "crypto/sha2.h" |
+ |
+namespace chrome { |
+ |
+// The current version of the hints file. |
+static const int kCurrentHintFileVersion = 0x10; |
+// The earliest version of the hints file. |
+static const int kEarliestHintFileVersion = 0x10; |
+// The Version field in the JSON encoded file. |
+static const char kVersionField[] = "Version"; |
+// The HashAlgorithm field in the JSON encoded file. |
+static const char kHashAlgoField[] = "HashAlgorithm"; |
+// The Hash field in the JSON encoded file. |
+static const char kHashField[] = "Hash"; |
+// The PluginPath field in the JSON encoded file. |
+static const char kPluginPath[] = "PluginPath"; |
+// The PluginVersion field in the JSON encoded file. |
+static const char kPluginVersion[] = "PluginVersion"; |
+ |
+ComponentFlashHintFile::ComponentFlashHintFile() |
+ : was_previous_hint_file_(false), previous_hint_file_() {} |
+ |
+bool ComponentFlashHintFile::RecordFlashUpdate(base::FilePath& unpacked_plugin, |
+ base::FilePath& moved_plugin, |
+ const std::string& version) { |
+ base::MemoryMappedFile mapped_file; |
+ if (!mapped_file.Initialize(unpacked_plugin)) |
+ return false; |
+ |
+ std::string hash(crypto::kSHA256Length, 0); |
+ crypto::SHA256HashBytes(mapped_file.data(), mapped_file.length(), |
+ string_as_array(&hash), hash.size()); |
+ |
+ return WriteToDisk(kCurrentHintFileVersion, |
+ crypto::SecureHash::Algorithm::SHA256, hash, moved_plugin, |
+ version); |
+} |
+ |
+// static |
+bool ComponentFlashHintFile::DoesHintFileExist() { |
+ base::FilePath hint_file_path; |
+ if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) |
+ return false; |
+ return base::PathExists(hint_file_path); |
+} |
+ |
+bool ComponentFlashHintFile::Revert() { |
+ if (!was_previous_hint_file_) |
+ return false; |
+ base::FilePath hint_file_path; |
+ if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) |
+ return false; |
+ return base::ImportantFileWriter::WriteFileAtomically(hint_file_path, |
+ previous_hint_file_); |
+} |
+ |
+bool ComponentFlashHintFile::WriteToDisk( |
+ int version, |
rickyz (no longer on Chrome)
2015/07/31 00:13:56
Don't think this param is used - kCurrentHintFileV
Greg K
2015/08/04 00:21:17
That is correct, although I corrected it to use th
|
+ crypto::SecureHash::Algorithm algorithm, |
+ const std::string& hash, |
+ base::FilePath& plugin_path, |
+ const std::string& flash_version) { |
+ base::FilePath hint_file_path; |
+ if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) |
+ return false; |
+ |
+ // Check for and try to back up the current hints file in case it needs to be |
+ // reverted. |
+ was_previous_hint_file_ = base::PathExists(hint_file_path); |
+ if (was_previous_hint_file_) { |
+ if (!ReadFileToString(hint_file_path, &previous_hint_file_)) |
rickyz (no longer on Chrome)
2015/07/31 00:13:56
Might it be easier to back it up to a file instead
Greg K
2015/08/04 00:21:17
We don't know to revert at all anymore since the f
|
+ return false; |
+ } |
+ |
+ std::string encoded_hash; |
+ base::Base64Encode(hash, &encoded_hash); |
+ |
+ // Now construct a Value object to convert to JSON. |
+ base::DictionaryValue dict; |
+ dict.SetInteger(kVersionField, kCurrentHintFileVersion); |
+ dict.SetInteger(kHashAlgoField, crypto::SecureHash::SHA256); |
+ dict.SetString(kHashField, encoded_hash); |
+ dict.SetString(kPluginPath, plugin_path.value()); |
+ dict.SetString(kPluginVersion, flash_version); |
+ // Do the serialization of the DictionaryValue to JSON. |
+ std::string json_string; |
+ JSONStringValueSerializer serializer(&json_string); |
+ if (!serializer.Serialize(dict)) |
+ return false; |
+ |
+ return base::ImportantFileWriter::WriteFileAtomically(hint_file_path, |
+ json_string); |
+} |
+ |
+// static |
+bool ComponentFlashHintFile::VerifyAndReturnFlashLocation( |
+ base::FilePath* path, |
+ std::string* flash_version) { |
+ base::FilePath hint_file_path; |
+ if (!PathService::Get(chrome::FILE_COMPONENT_FLASH_HINT, &hint_file_path)) |
+ return false; |
+ |
+ std::string json_string; |
+ if (!base::ReadFileToString(hint_file_path, &json_string)) |
+ return false; |
+ |
+ int error_code; |
+ std::string error_message; |
+ JSONStringValueDeserializer deserializer(json_string); |
+ scoped_ptr<base::Value> value( |
+ deserializer.Deserialize(&error_code, &error_message)); |
+ |
+ if (value == nullptr) { |
+ LOG(ERROR) |
+ << "Could not deserialize the component updated flash hint file. Error " |
+ << error_code << ": " << error_message; |
+ return false; |
+ } |
+ |
+ base::DictionaryValue* dict = nullptr; |
+ if (!value->GetAsDictionary(&dict)) |
+ return false; |
+ |
+ int version; |
+ if (!dict->GetInteger(kVersionField, &version)) |
+ return false; |
+ if (version < kEarliestHintFileVersion || version > kCurrentHintFileVersion) |
+ return false; |
+ |
+ int hash_algorithm; |
+ if (!dict->GetInteger(kHashAlgoField, &hash_algorithm)) |
+ return false; |
+ if (hash_algorithm != crypto::SecureHash::SHA256) |
+ return false; |
+ |
+ std::string hash; |
+ if (!dict->GetString(kHashField, &hash)) |
+ return false; |
+ |
+ std::string plugin_path_str; |
+ if (!dict->GetString(kPluginPath, &plugin_path_str)) |
+ return false; |
+ |
+ std::string plugin_version_str; |
+ if (!dict->GetString(kPluginVersion, &plugin_version_str)) |
+ return false; |
+ |
+ std::string decoded_hash; |
+ if (!base::Base64Decode(hash, &decoded_hash)) |
+ return false; |
+ |
+ base::FilePath plugin_path(plugin_path_str); |
+ base::MemoryMappedFile plugin_file; |
+ if (!plugin_file.Initialize(plugin_path)) |
+ return false; |
+ |
+ std::vector<uint8_t> file_hash(crypto::kSHA256Length, 0); |
+ crypto::SHA256HashBytes(plugin_file.data(), plugin_file.length(), |
+ &file_hash[0], file_hash.size()); |
+ bool hashes_same = memcmp(&file_hash[0], string_as_array(&decoded_hash), |
+ crypto::kSHA256Length) == 0; |
+ DCHECK(hashes_same); |
rickyz (no longer on Chrome)
2015/07/31 00:13:56
I'd probably replace this with a LOG(WARNING) inst
Greg K
2015/08/04 00:21:17
I went with LOG(ERROR) on the grounds that this co
|
+ if (!hashes_same) |
+ return false; |
+ |
+ *path = plugin_path; |
+ flash_version->assign(plugin_version_str); |
+ return true; |
+} |
+ |
+} // namespace chrome |