Upload upstream chromium 71.0.3578.0
[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;
44   base::PathService::Get(DIR_SOURCE_ROOT, &computed_path);
45   computed_path = computed_path.AppendASCII("lib").Append(components[0]);
46   base::File library(computed_path,
47                      base::File::FLAG_OPEN | base::File::FLAG_READ);
48   if (!library.IsValid()) {
49     if (error) {
50       error->message = base::StringPrintf(
51           "open library: %s",
52           base::File::ErrorToString(library.error_details()).c_str());
53     }
54     return nullptr;
55   }
56
57   zx::vmo vmo;
58   zx_status_t status = fdio_get_vmo_clone(library.GetPlatformFile(),
59                                           vmo.reset_and_get_address());
60   if (status != ZX_OK) {
61     if (error) {
62       error->message = base::StringPrintf("fdio_get_vmo_clone: %s",
63                                           zx_status_get_string(status));
64     }
65     return nullptr;
66   }
67   NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
68   return result;
69 }
70
71 void UnloadNativeLibrary(NativeLibrary library) {
72   // dlclose() is a no-op on Fuchsia, so do nothing here.
73 }
74
75 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
76                                           StringPiece name) {
77   return dlsym(library, name.data());
78 }
79
80 std::string GetNativeLibraryName(StringPiece name) {
81   return base::StringPrintf("lib%s.so", name.as_string().c_str());
82 }
83
84 std::string GetLoadableModuleName(StringPiece name) {
85   return GetNativeLibraryName(name);
86 }
87
88 }  // namespace base