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"
15 using mojo::ScopedHandle
;
17 namespace resource_provider
{
20 const char kResourceIcudtl
[] = "icudtl.dat";
22 ScopedHandle
GetHandleForPath(const base::FilePath
& path
) {
24 return ScopedHandle();
27 base::File
file(path
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
28 if (!file
.IsValid()) {
29 LOG(WARNING
) << "file not valid, path=" << path
.value();
30 return ScopedHandle();
33 MojoHandle mojo_handle
;
34 MojoResult create_result
=
35 MojoCreatePlatformHandleWrapper(file
.TakePlatformFile(), &mojo_handle
);
36 if (create_result
!= MOJO_RESULT_OK
) {
37 LOG(WARNING
) << "unable to create wrapper, path=" << path
.value()
38 << "result=" << create_result
;
39 return ScopedHandle();
42 return ScopedHandle(mojo::Handle(mojo_handle
)).Pass();
47 ResourceProviderImpl::ResourceProviderImpl(
48 const base::FilePath
& application_path
,
49 const std::string
& resource_provider_app_url
)
50 : application_path_(application_path
),
51 resource_provider_app_url_(resource_provider_app_url
) {
52 CHECK(!application_path_
.empty());
55 ResourceProviderImpl::~ResourceProviderImpl() {
58 void ResourceProviderImpl::GetResources(mojo::Array
<mojo::String
> paths
,
59 const GetResourcesCallback
& callback
) {
60 mojo::Array
<mojo::ScopedHandle
> handles
;
61 if (!paths
.is_null()) {
62 handles
.resize(paths
.size());
63 for (size_t i
= 0; i
< paths
.size(); ++i
) {
64 handles
[i
] = GetHandleForPath(
65 GetPathForResourceNamed(application_path_
, paths
[i
]));
68 callback
.Run(handles
.Pass());
71 void ResourceProviderImpl::GetICUHandle(const GetICUHandleCallback
& callback
) {
72 const base::FilePath
resource_app_path(
73 GetPathForApplicationUrl(GURL(resource_provider_app_url_
)));
74 mojo::ScopedHandle handle
= GetHandleForPath(
75 GetPathForResourceNamed(resource_app_path
, kResourceIcudtl
));
76 callback
.Run(handle
.Pass());
79 } // namespace resource_provider