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.
5 #include "components/resource_provider/resource_provider_impl.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "components/resource_provider/file_utils.h"
12 #include "mojo/platform_handle/platform_handle_functions.h"
14 using mojo::ScopedHandle
;
16 namespace resource_provider
{
19 ScopedHandle
GetHandleForPath(const base::FilePath
& path
) {
21 return ScopedHandle();
24 base::File
file(path
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
25 if (!file
.IsValid()) {
26 LOG(WARNING
) << "file not valid, path=" << path
.value();
27 return ScopedHandle();
30 MojoHandle mojo_handle
;
31 MojoResult create_result
=
32 MojoCreatePlatformHandleWrapper(file
.TakePlatformFile(), &mojo_handle
);
33 if (create_result
!= MOJO_RESULT_OK
) {
34 LOG(WARNING
) << "unable to create wrapper, path=" << path
.value()
35 << "result=" << create_result
;
36 return ScopedHandle();
39 return ScopedHandle(mojo::Handle(mojo_handle
)).Pass();
44 ResourceProviderImpl::ResourceProviderImpl(
45 const base::FilePath
& application_path
)
46 : application_path_(application_path
) {
47 CHECK(!application_path_
.empty());
50 ResourceProviderImpl::~ResourceProviderImpl() {
53 void ResourceProviderImpl::GetResources(mojo::Array
<mojo::String
> paths
,
54 const GetResourcesCallback
& callback
) {
55 mojo::Array
<mojo::ScopedHandle
> handles
;
56 if (!paths
.is_null()) {
57 handles
.resize(paths
.size());
58 for (size_t i
= 0; i
< paths
.size(); ++i
) {
59 handles
[i
] = GetHandleForPath(
60 GetPathForResourceNamed(application_path_
, paths
[i
]));
63 callback
.Run(handles
.Pass());
66 } // namespace resource_provider