Move file validation util methods from chrome/ to components/
Bug: 758690
Change-Id: I462d4e70518c7e4826eaffa903b61f68df38b7b4
Reviewed-on: https://chromium-review.googlesource.com/853200
Commit-Queue: Jian Li <[email protected]>
Reviewed-by: Adam Langley <[email protected]>
Reviewed-by: Peter Williamson <[email protected]>
Cr-Commit-Position: refs/heads/master@{#527478}
diff --git a/chrome/browser/offline_pages/android/offline_page_bridge.cc b/chrome/browser/offline_pages/android/offline_page_bridge.cc
index ba53d64..2ed92e5 100644
--- a/chrome/browser/offline_pages/android/offline_page_bridge.cc
+++ b/chrome/browser/offline_pages/android/offline_page_bridge.cc
@@ -29,6 +29,7 @@
#include "chrome/browser/offline_pages/request_coordinator_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
+#include "components/offline_pages/core/archive_validator.h"
#include "components/offline_pages/core/background/request_coordinator.h"
#include "components/offline_pages/core/background/request_queue_results.h"
#include "components/offline_pages/core/background/save_page_request.h"
@@ -809,7 +810,7 @@
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
- base::Bind(&OfflinePageUtils::ValidateFile, offline_page->file_path,
+ base::Bind(&ArchiveValidator::ValidateFile, offline_page->file_path,
offline_page->file_size, offline_page->digest),
base::Bind(&ValidateFileCallback, j_callback_obj, offline_page->url,
offline_page->file_path));
diff --git a/chrome/browser/offline_pages/offline_page_mhtml_archiver.cc b/chrome/browser/offline_pages/offline_page_mhtml_archiver.cc
index af676dc..63ea2663 100644
--- a/chrome/browser/offline_pages/offline_page_mhtml_archiver.cc
+++ b/chrome/browser/offline_pages/offline_page_mhtml_archiver.cc
@@ -16,6 +16,7 @@
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/offline_pages/offline_page_utils.h"
#include "chrome/browser/ssl/security_state_tab_helper.h"
+#include "components/offline_pages/core/archive_validator.h"
#include "components/security_state/core/security_state.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_entry.h"
@@ -44,7 +45,7 @@
const base::Callback<void(const std::string&)>& callback) {
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
- base::Bind(&OfflinePageUtils::ComputeDigest, file_path), callback);
+ base::Bind(&ArchiveValidator::ComputeDigest, file_path), callback);
}
} // namespace
diff --git a/chrome/browser/offline_pages/offline_page_request_job.cc b/chrome/browser/offline_pages/offline_page_request_job.cc
index d6b5bbf..5a432f1 100644
--- a/chrome/browser/offline_pages/offline_page_request_job.cc
+++ b/chrome/browser/offline_pages/offline_page_request_job.cc
@@ -21,6 +21,7 @@
#include "chrome/browser/offline_pages/offline_page_utils.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/renderer_host/chrome_navigation_ui_data.h"
+#include "components/offline_pages/core/archive_validator.h"
#include "components/offline_pages/core/client_namespace_constants.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "components/offline_pages/core/request_header/offline_page_header.h"
@@ -323,7 +324,7 @@
const base::Callback<void(bool)>& callback) {
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
- base::Bind(&OfflinePageUtils::ValidateFile, file_path, expected_file_size,
+ base::Bind(&ArchiveValidator::ValidateFile, file_path, expected_file_size,
expected_digest),
callback);
}
diff --git a/chrome/browser/offline_pages/offline_page_utils.cc b/chrome/browser/offline_pages/offline_page_utils.cc
index bc7b2aac..ed0fd51 100644
--- a/chrome/browser/offline_pages/offline_page_utils.cc
+++ b/chrome/browser/offline_pages/offline_page_utils.cc
@@ -5,9 +5,6 @@
#include "chrome/browser/offline_pages/offline_page_utils.h"
#include "base/bind.h"
-#include "base/files/file.h"
-#include "base/files/file_path.h"
-#include "base/files/file_util.h"
#include "base/location.h"
#include "base/metrics/histogram_macros.h"
#include "base/stl_util.h"
@@ -35,8 +32,6 @@
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
-#include "crypto/secure_hash.h"
-#include "crypto/sha2.h"
#include "net/base/mime_util.h"
namespace offline_pages {
@@ -337,45 +332,6 @@
}
// static
-std::string OfflinePageUtils::ComputeDigest(const base::FilePath& file_path) {
- base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
- if (!file.IsValid())
- return std::string();
-
- std::unique_ptr<crypto::SecureHash> secure_hash(
- crypto::SecureHash::Create(crypto::SecureHash::SHA256));
-
- const int kMaxBufferSize = 1024;
- std::vector<char> buffer(kMaxBufferSize);
- int bytes_read;
- do {
- bytes_read = file.ReadAtCurrentPos(buffer.data(), kMaxBufferSize);
- if (bytes_read > 0)
- secure_hash->Update(buffer.data(), bytes_read);
- } while (bytes_read > 0);
- if (bytes_read < 0)
- return std::string();
-
- std::string result_bytes(crypto::kSHA256Length, 0);
- secure_hash->Finish(&(result_bytes[0]), result_bytes.size());
- return result_bytes;
-}
-
-// static
-bool OfflinePageUtils::ValidateFile(const base::FilePath& file_path,
- int64_t expected_file_size,
- const std::string& expected_digest) {
- int64_t actual_file_size;
- if (!base::GetFileSize(file_path, &actual_file_size))
- return false;
- if (expected_file_size != actual_file_size)
- return false;
-
- std::string actual_digest = ComputeDigest(file_path);
- return expected_digest == actual_digest;
-}
-
-// static
std::string OfflinePageUtils::ExtractOfflineHeaderValueFromNavigationEntry(
const content::NavigationEntry& entry) {
std::string extra_headers = entry.GetExtraHeaders();
diff --git a/chrome/browser/offline_pages/offline_page_utils.h b/chrome/browser/offline_pages/offline_page_utils.h
index b414efd4..0e50e10 100644
--- a/chrome/browser/offline_pages/offline_page_utils.h
+++ b/chrome/browser/offline_pages/offline_page_utils.h
@@ -13,7 +13,6 @@
#include "url/gurl.h"
namespace base {
-class FilePath;
class Time;
}
@@ -153,16 +152,6 @@
const base::Time& begin_time,
const base::Time& end_time);
- // Computes a SHA256 digest of the specified file. Empty string will be
- // returned if the digest cannot be computed.
- static std::string ComputeDigest(const base::FilePath& file_path);
-
- // Returns true if the specified file has |expected_file_size| and
- // |expected_digest|.
- static bool ValidateFile(const base::FilePath& file_path,
- int64_t expected_file_size,
- const std::string& expected_digest);
-
// Extracts and returns the value of the custom offline header from a
// navigation entry. Empty string is returned if it is not found.
// Note that the offline header is assumed to be the onlt extra header if it
diff --git a/components/offline_pages/core/BUILD.gn b/components/offline_pages/core/BUILD.gn
index a349d06..5bdb2d8 100644
--- a/components/offline_pages/core/BUILD.gn
+++ b/components/offline_pages/core/BUILD.gn
@@ -10,6 +10,8 @@
sources = [
"archive_manager.cc",
"archive_manager.h",
+ "archive_validator.cc",
+ "archive_validator.h",
"client_id.cc",
"client_id.h",
"client_namespace_constants.cc",
@@ -84,6 +86,7 @@
":switches",
"//base",
"//components/keyed_service/core",
+ "//crypto",
"//net",
"//services/metrics/public/cpp:metrics_cpp",
"//services/metrics/public/cpp:ukm_builders",
diff --git a/components/offline_pages/core/DEPS b/components/offline_pages/core/DEPS
index bbd661af..52fe6296 100644
--- a/components/offline_pages/core/DEPS
+++ b/components/offline_pages/core/DEPS
@@ -2,6 +2,7 @@
"+components/keyed_service",
"+components/offline_items_collection",
"+components/ukm",
+ "+crypto",
"+services/metrics/public/cpp",
"+sql",
]
diff --git a/components/offline_pages/core/archive_validator.cc b/components/offline_pages/core/archive_validator.cc
new file mode 100644
index 0000000..b0ad3fc
--- /dev/null
+++ b/components/offline_pages/core/archive_validator.cc
@@ -0,0 +1,56 @@
+// Copyright 2018 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 "components/offline_pages/core/archive_validator.h"
+
+#include <vector>
+
+#include "base/files/file.h"
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "crypto/secure_hash.h"
+#include "crypto/sha2.h"
+
+namespace offline_pages {
+
+// static
+std::string ArchiveValidator::ComputeDigest(const base::FilePath& file_path) {
+ base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
+ if (!file.IsValid())
+ return std::string();
+
+ std::unique_ptr<crypto::SecureHash> secure_hash(
+ crypto::SecureHash::Create(crypto::SecureHash::SHA256));
+
+ const int kMaxBufferSize = 1024;
+ std::vector<char> buffer(kMaxBufferSize);
+ int bytes_read;
+ do {
+ bytes_read = file.ReadAtCurrentPos(buffer.data(), kMaxBufferSize);
+ if (bytes_read > 0)
+ secure_hash->Update(buffer.data(), bytes_read);
+ } while (bytes_read > 0);
+ if (bytes_read < 0)
+ return std::string();
+
+ std::string result_bytes(crypto::kSHA256Length, 0);
+ secure_hash->Finish(&result_bytes[0], result_bytes.size());
+ return result_bytes;
+}
+
+// static
+bool ArchiveValidator::ValidateFile(const base::FilePath& file_path,
+ int64_t expected_file_size,
+ const std::string& expected_digest) {
+ int64_t actual_file_size;
+ if (!base::GetFileSize(file_path, &actual_file_size))
+ return false;
+ if (expected_file_size != actual_file_size)
+ return false;
+
+ std::string actual_digest = ComputeDigest(file_path);
+ return expected_digest == actual_digest;
+}
+
+} // namespace offline_pages
diff --git a/components/offline_pages/core/archive_validator.h b/components/offline_pages/core/archive_validator.h
new file mode 100644
index 0000000..e5cf2d0
--- /dev/null
+++ b/components/offline_pages/core/archive_validator.h
@@ -0,0 +1,37 @@
+// Copyright 2018 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.
+
+#ifndef COMPONENTS_OFFLINE_PAGES_CORE_ARCHIVE_VALIDATOR_H_
+#define COMPONENTS_OFFLINE_PAGES_CORE_ARCHIVE_VALIDATOR_H_
+
+#include <string>
+
+#include "base/macros.h"
+
+namespace base {
+class FilePath;
+}
+
+namespace offline_pages {
+
+// Contains all helper functions to validate an archive file.
+class ArchiveValidator {
+ public:
+ // Computes a SHA256 digest of the specified file. Empty string will be
+ // returned if the digest cannot be computed.
+ static std::string ComputeDigest(const base::FilePath& file_path);
+
+ // Returns true if the specified file has |expected_file_size| and
+ // |expected_digest|.
+ static bool ValidateFile(const base::FilePath& file_path,
+ int64_t expected_file_size,
+ const std::string& expected_digest);
+
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(ArchiveValidator);
+};
+
+} // namespace offline_pages
+
+#endif // COMPONENTS_OFFLINE_PAGES_CORE_ARCHIVE_VALIDATOR_H_