• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2018 The Chromium Embedded Framework Authors. All rights
2// reserved. Use of this source code is governed by a BSD-style license that
3// can be found in the LICENSE file.
4
5#include "include/wrapper/cef_library_loader.h"
6
7#include <libgen.h>
8#include <mach-o/dyld.h>
9#include <stdio.h>
10
11#include <memory>
12#include <sstream>
13
14namespace {
15
16const char kFrameworkPath[] =
17    "Chromium Embedded Framework.framework/Chromium Embedded Framework";
18const char kPathFromHelperExe[] = "../../..";
19const char kPathFromMainExe[] = "../Frameworks";
20
21std::string GetFrameworkPath(bool helper) {
22  uint32_t exec_path_size = 0;
23  int rv = _NSGetExecutablePath(NULL, &exec_path_size);
24  if (rv != -1) {
25    return std::string();
26  }
27
28  std::unique_ptr<char[]> exec_path(new char[exec_path_size]);
29  rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
30  if (rv != 0) {
31    return std::string();
32  }
33
34  // Get the directory path of the executable.
35  const char* parent_dir = dirname(exec_path.get());
36  if (!parent_dir) {
37    return std::string();
38  }
39
40  // Append the relative path to the framework.
41  std::stringstream ss;
42  ss << parent_dir << "/" << (helper ? kPathFromHelperExe : kPathFromMainExe)
43     << "/" << kFrameworkPath;
44  return ss.str();
45}
46
47}  // namespace
48
49CefScopedLibraryLoader::CefScopedLibraryLoader() : loaded_(false) {}
50
51bool CefScopedLibraryLoader::Load(bool helper) {
52  if (loaded_) {
53    return false;
54  }
55
56  const std::string& framework_path = GetFrameworkPath(helper);
57  if (framework_path.empty()) {
58    fprintf(stderr, "App does not have the expected bundle structure.\n");
59    return false;
60  }
61
62  // Load the CEF framework library.
63  if (!cef_load_library(framework_path.c_str())) {
64    fprintf(stderr, "Failed to load the CEF framework.\n");
65    return false;
66  }
67
68  loaded_ = true;
69  return true;
70}
71
72CefScopedLibraryLoader::~CefScopedLibraryLoader() {
73  if (loaded_) {
74    // Unload the CEF framework library.
75    cef_unload_library();
76  }
77}
78