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

Unified Diff: components/resource_provider/resource_provider_impl.cc

Issue 1108403008: Adds resource_provider::ResourceProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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
Index: components/resource_provider/resource_provider_impl.cc
diff --git a/components/resource_provider/resource_provider_impl.cc b/components/resource_provider/resource_provider_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ebf1af82b6b325b52dde7b868a26e55aad970230
--- /dev/null
+++ b/components/resource_provider/resource_provider_impl.cc
@@ -0,0 +1,82 @@
+// 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 "components/resource_provider/resource_provider_impl.h"
+
+#include "base/bind.h"
+#include "base/files/file_util.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/sequenced_task_runner.h"
+#include "base/threading/sequenced_worker_pool.h"
+#include "components/resource_provider/file_utils.h"
+
+namespace resource_provider {
+
+// Loading is done on a background thread. The data is passed between the
+// two threads using this.
+struct ResourceProviderImpl::Data : public base::RefCounted<Data> {
+ public:
+ mojo::Array<uint8_t> data;
+
+ private:
+ friend class base::RefCounted<Data>;
+
+ ~Data() {}
+};
+
+ResourceProviderImpl::ResourceProviderImpl(
+ const base::FilePath& application_path,
+ base::SequencedWorkerPool* blocking_pool)
+ : application_path_(application_path),
+ blocking_pool_(blocking_pool),
+ weak_factory_(this) {
+ CHECK(!application_path_.empty());
+}
+
+ResourceProviderImpl::~ResourceProviderImpl() {
+}
+
+// static
+void ResourceProviderImpl::ReadFileOnWorkerThread(const base::FilePath& path,
+ scoped_refptr<Data> data) {
+ int64 file_size = 0;
+ if (!base::GetFileSize(path, &file_size) || file_size < 0 ||
+ file_size > std::numeric_limits<int>::max())
+ return;
+
+ data->data.resize(file_size);
+ if (base::ReadFile(path, reinterpret_cast<char*>(&data->data.front()),
+ file_size) != file_size) {
+ data->data.reset();
+ }
+}
+
+void ResourceProviderImpl::OnReadFile(scoped_refptr<Data> data,
+ const GetResourceCallback& callback) {
+ callback.Run(data->data.Pass());
+}
+
+void ResourceProviderImpl::GetResource(const mojo::String& path,
+ const GetResourceCallback& callback) {
+ const base::FilePath resource_path(
+ GetPathForResourceNamed(application_path_, path));
+ if (resource_path.empty()) {
+ callback.Run(mojo::Array<uint8_t>());
+ return;
+ }
+
+ scoped_refptr<Data> data(new Data);
+ scoped_refptr<base::SequencedTaskRunner> runner(
+ blocking_pool_->GetSequencedTaskRunnerWithShutdownBehavior(
+ blocking_pool_->GetSequenceToken(),
+ base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
+ runner->PostTaskAndReply(
+ FROM_HERE, base::Bind(&ResourceProviderImpl::ReadFileOnWorkerThread,
+ resource_path, data),
+ base::Bind(&ResourceProviderImpl::OnReadFile, weak_factory_.GetWeakPtr(),
+ data, callback));
+}
+
+} // namespace resource_provider

Powered by Google App Engine
This is Rietveld 408576698