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

Side by Side 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, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "components/resource_provider/resource_provider_impl.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "components/resource_provider/file_utils.h"
14
15 namespace resource_provider {
16
17 // Loading is done on a background thread. The data is passed between the
18 // two threads using this.
19 struct ResourceProviderImpl::Data : public base::RefCounted<Data> {
20 public:
21 mojo::Array<uint8_t> data;
22
23 private:
24 friend class base::RefCounted<Data>;
25
26 ~Data() {}
27 };
28
29 ResourceProviderImpl::ResourceProviderImpl(
30 const base::FilePath& application_path,
31 base::SequencedWorkerPool* blocking_pool)
32 : application_path_(application_path),
33 blocking_pool_(blocking_pool),
34 weak_factory_(this) {
35 CHECK(!application_path_.empty());
36 }
37
38 ResourceProviderImpl::~ResourceProviderImpl() {
39 }
40
41 // static
42 void ResourceProviderImpl::ReadFileOnWorkerThread(const base::FilePath& path,
43 scoped_refptr<Data> data) {
44 int64 file_size = 0;
45 if (!base::GetFileSize(path, &file_size) || file_size < 0 ||
46 file_size > std::numeric_limits<int>::max())
47 return;
48
49 data->data.resize(file_size);
50 if (base::ReadFile(path, reinterpret_cast<char*>(&data->data.front()),
51 file_size) != file_size) {
52 data->data.reset();
53 }
54 }
55
56 void ResourceProviderImpl::OnReadFile(scoped_refptr<Data> data,
57 const GetResourceCallback& callback) {
58 callback.Run(data->data.Pass());
59 }
60
61 void ResourceProviderImpl::GetResource(const mojo::String& path,
62 const GetResourceCallback& callback) {
63 const base::FilePath resource_path(
64 GetPathForResourceNamed(application_path_, path));
65 if (resource_path.empty()) {
66 callback.Run(mojo::Array<uint8_t>());
67 return;
68 }
69
70 scoped_refptr<Data> data(new Data);
71 scoped_refptr<base::SequencedTaskRunner> runner(
72 blocking_pool_->GetSequencedTaskRunnerWithShutdownBehavior(
73 blocking_pool_->GetSequenceToken(),
74 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
75 runner->PostTaskAndReply(
76 FROM_HERE, base::Bind(&ResourceProviderImpl::ReadFileOnWorkerThread,
77 resource_path, data),
78 base::Bind(&ResourceProviderImpl::OnReadFile, weak_factory_.GetWeakPtr(),
79 data, callback));
80 }
81
82 } // namespace resource_provider
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698