• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2018 Marshall A. Greenblatt. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef CEF_INCLUDE_WRAPPER_CEF_LIBRARY_LOADER_H_
31 #define CEF_INCLUDE_WRAPPER_CEF_LIBRARY_LOADER_H_
32 #pragma once
33 
34 #include "include/base/cef_build.h"
35 
36 #ifdef __cplusplus
37 #include <string>
38 
39 extern "C" {
40 #endif  // __cplusplus
41 
42 ///
43 // Load the CEF library at the specified |path|. Returns true (1) on
44 // success and false (0) on failure.
45 ///
46 int cef_load_library(const char* path);
47 
48 ///
49 // Unload the CEF library that was previously loaded. Returns true (1)
50 // on success and false (0) on failure.
51 ///
52 int cef_unload_library();
53 
54 #ifdef __cplusplus
55 }
56 
57 #if defined(OS_MAC)
58 
59 ///
60 // Scoped helper for loading and unloading the CEF framework library at
61 // runtime from the expected location in the app bundle. Loading at runtime
62 // instead of linking directly is a requirement of the macOS sandbox
63 // implementation.
64 //
65 // Example usage in the main process:
66 //
67 //   #include "include/wrapper/cef_library_loader.h"
68 //
69 //   int main(int argc, char* argv[]) {
70 //     // Dynamically load the CEF framework library.
71 //     CefScopedLibraryLoader library_loader;
72 //     if (!library_loader.LoadInMain())
73 //       return 1;
74 //
75 //     // Continue with CEF initialization...
76 //   }
77 //
78 // Example usage in the helper process:
79 //
80 //   #include "include/cef_sandbox_mac.h"
81 //   #include "include/wrapper/cef_library_loader.h"
82 //
83 //   int main(int argc, char* argv[]) {
84 //     // Initialize the macOS sandbox for this helper process.
85 //     CefScopedSandboxContext sandbox_context;
86 //     if (!sandbox_context.Initialize(argc, argv))
87 //       return 1;
88 //
89 //     // Dynamically load the CEF framework library.
90 //     CefScopedLibraryLoader library_loader;
91 //     if (!library_loader.LoadInHelper())
92 //       return 1;
93 //
94 //     // Continue with CEF initialization...
95 //   }
96 ///
97 class CefScopedLibraryLoader {
98  public:
99   CefScopedLibraryLoader();
100 
101   CefScopedLibraryLoader(const CefScopedLibraryLoader&) = delete;
102   CefScopedLibraryLoader& operator=(const CefScopedLibraryLoader&) = delete;
103 
104   ~CefScopedLibraryLoader();
105 
106   ///
107   // Load the CEF framework in the main process from the expected app
108   // bundle location relative to the executable. Returns true if the
109   // load succeeds.
110   ///
LoadInMain()111   bool LoadInMain() { return Load(false); }
112 
113   ///
114   // Load the CEF framework in the helper process from the expected app
115   // bundle location relative to the executable. Returns true if the
116   // load succeeds.
117   ///
LoadInHelper()118   bool LoadInHelper() { return Load(true); }
119 
120  private:
121   bool Load(bool helper);
122 
123   bool loaded_;
124 };
125 
126 #endif  // defined(OS_MAC)
127 #endif  // __cplusplus
128 
129 #endif  // CEF_INCLUDE_WRAPPER_CEF_LIBRARY_LOADER_H_
130