OLD | NEW |
| (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 <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/files/file_util.h" | |
12 #include "base/location.h" | |
13 #include "base/logging.h" | |
14 #include "components/resource_provider/file_utils.h" | |
15 #include "mojo/platform_handle/platform_handle_functions.h" | |
16 #include "url/gurl.h" | |
17 | |
18 using mojo::ScopedHandle; | |
19 | |
20 namespace resource_provider { | |
21 namespace { | |
22 | |
23 ScopedHandle GetHandleForPath(const base::FilePath& path) { | |
24 if (path.empty()) | |
25 return ScopedHandle(); | |
26 | |
27 ScopedHandle to_pass; | |
28 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
29 if (!file.IsValid()) { | |
30 LOG(WARNING) << "file not valid, path=" << path.value(); | |
31 return ScopedHandle(); | |
32 } | |
33 | |
34 MojoHandle mojo_handle; | |
35 MojoResult create_result = | |
36 MojoCreatePlatformHandleWrapper(file.TakePlatformFile(), &mojo_handle); | |
37 if (create_result != MOJO_RESULT_OK) { | |
38 LOG(WARNING) << "unable to create wrapper, path=" << path.value() | |
39 << "result=" << create_result; | |
40 return ScopedHandle(); | |
41 } | |
42 | |
43 return ScopedHandle(mojo::Handle(mojo_handle)); | |
44 } | |
45 | |
46 } // namespace | |
47 | |
48 ResourceProviderImpl::ResourceProviderImpl( | |
49 const base::FilePath& application_path, | |
50 const std::string& resource_provider_app_url) | |
51 : application_path_(application_path), | |
52 resource_provider_app_url_(resource_provider_app_url) { | |
53 CHECK(!application_path_.empty()); | |
54 } | |
55 | |
56 ResourceProviderImpl::~ResourceProviderImpl() { | |
57 } | |
58 | |
59 void ResourceProviderImpl::GetResources(mojo::Array<mojo::String> paths, | |
60 const GetResourcesCallback& callback) { | |
61 mojo::Array<mojo::ScopedHandle> handles; | |
62 if (!paths.is_null()) { | |
63 handles.resize(paths.size()); | |
64 for (size_t i = 0; i < paths.size(); ++i) { | |
65 handles[i] = GetHandleForPath( | |
66 GetPathForResourceNamed(application_path_, paths[i])); | |
67 } | |
68 } | |
69 callback.Run(std::move(handles)); | |
70 } | |
71 | |
72 } // namespace resource_provider | |
OLD | NEW |