Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / base / native_library_fuchsia.cc
1 // Copyright 2018 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 "base/native_library.h"
6
7 #include <fcntl.h>
8 #include <lib/fdio/io.h>
9 #include <lib/zx/vmo.h>
10 #include <stdio.h>
11 #include <zircon/dlfcn.h>
12 #include <zircon/status.h>
13 #include <zircon/syscalls.h>
14
15 #include "base/base_paths_fuchsia.h"
16 #include "base/files/file.h"
17 #include "base/files/file_path.h"
18 #include "base/fuchsia/fuchsia_logging.h"
19 #include "base/logging.h"
20 #include "base/path_service.h"
21 #include "base/posix/safe_strerror.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/threading/thread_restrictions.h"
25
26 namespace base {
27
28 std::string NativeLibraryLoadError::ToString() const {
29   return message;
30 }
31
32 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
33                                            const NativeLibraryOptions& options,
34                                            NativeLibraryLoadError* error) {
35   std::vector<base::FilePath::StringType> components;
36   library_path.GetComponents(&components);
37   if (components.size() != 1u) {
38     NOTREACHED() << "library_path is a path, should be a filename: "
39                  << library_path.MaybeAsASCII();
40     return nullptr;
41   }
42
43   FilePath computed_path = base::GetPackageRoot();
44   computed_path = computed_path.AppendASCII("lib").Append(components[0]);
45   base::File library(computed_path,
46                      base::File::FLAG_OPEN | base::File::FLAG_READ);
47   if (!library.IsValid()) {
48     if (error) {
49       error->message = base::StringPrintf(
50           "open library: %s",
51           base::File::ErrorToString(library.error_details()).c_str());
52     }
53     return nullptr;
54   }
55
56   zx::vmo vmo;
57   zx_status_t status = fdio_get_vmo_clone(library.GetPlatformFile(),
58                                           vmo.reset_and_get_address());
59   if (status != ZX_OK) {
60     if (error) {
61       error->message = base::StringPrintf("fdio_get_vmo_clone: %s",
62                                           zx_status_get_string(status));
63     }
64     return nullptr;
65   }
66   NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
67   return result;
68 }
69
70 void UnloadNativeLibrary(NativeLibrary library) {
71   // dlclose() is a no-op on Fuchsia, so do nothing here.
72 }
73
74 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
75                                           StringPiece name) {
76   return dlsym(library, name.data());
77 }
78
79 std::string GetNativeLibraryName(StringPiece name) {
80   return base::StringPrintf("lib%s.so", name.as_string().c_str());
81 }
82
83 std::string GetLoadableModuleName(StringPiece name) {
84   return GetNativeLibraryName(name);
85 }
86
87 }  // namespace base