dullahan.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. @brief Dullahan - a headless browser rendering engine
  3. based around the Chromium Embedded Framework
  4. @author Callum Prentice 2017
  5. Copyright (c) 2017, Linden Research, Inc.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. */
  22. #ifndef _DULLAHAN
  23. #define _DULLAHAN
  24. #include <memory>
  25. #include <string>
  26. #include <functional>
  27. #include <vector>
  28. class dullahan_impl;
  29. class dullahan
  30. {
  31. public:
  32. ////////// keyboard constants //////////
  33. typedef enum e_key_event
  34. {
  35. KE_KEY_DOWN,
  36. KE_KEY_REPEAT,
  37. KE_KEY_UP,
  38. KE_KEY_CHAR,
  39. } EKeyEvent;
  40. typedef enum e_keyboard_modifier
  41. {
  42. KM_MODIFIER_NONE = 0x00,
  43. KM_MODIFIER_SHIFT = 0x01,
  44. KM_MODIFIER_CONTROL = 0x02,
  45. KM_MODIFIER_ALT = 0x04,
  46. KM_MODIFIER_META = 0x08
  47. } EKeyboardModifier;
  48. ////////// mouse constants //////////
  49. typedef enum e_mouse_event
  50. {
  51. ME_MOUSE_MOVE,
  52. ME_MOUSE_DOWN,
  53. ME_MOUSE_UP,
  54. ME_MOUSE_DOUBLE_CLICK
  55. } EMouseEvent;
  56. typedef enum e_mouse_button
  57. {
  58. MB_MOUSE_BUTTON_LEFT,
  59. MB_MOUSE_BUTTON_RIGHT,
  60. MB_MOUSE_BUTTON_MIDDLE
  61. } EMouseButton;
  62. ////////// cursor type //////////
  63. typedef enum e_cursor_type
  64. {
  65. CT_POINTER = 0,
  66. CT_CROSS,
  67. CT_HAND,
  68. CT_IBEAM,
  69. CT_WAIT,
  70. CT_HELP,
  71. CT_EASTRESIZE,
  72. CT_NORTHRESIZE,
  73. CT_NORTHEASTRESIZE,
  74. CT_NORTHWESTRESIZE,
  75. CT_SOUTHRESIZE,
  76. CT_SOUTHEASTRESIZE,
  77. CT_SOUTHWESTRESIZE,
  78. CT_WESTRESIZE,
  79. CT_NORTHSOUTHRESIZE,
  80. CT_EASTWESTRESIZE,
  81. CT_NORTHEASTSOUTHWESTRESIZE,
  82. CT_NORTHWESTSOUTHEASTRESIZE,
  83. CT_COLUMNRESIZE,
  84. CT_ROWRESIZE,
  85. CT_MIDDLEPANNING,
  86. CT_EASTPANNING,
  87. CT_NORTHPANNING,
  88. CT_NORTHEASTPANNING,
  89. CT_NORTHWESTPANNING,
  90. CT_SOUTHPANNING,
  91. CT_SOUTHEASTPANNING,
  92. CT_SOUTHWESTPANNING,
  93. CT_WESTPANNING,
  94. CT_MOVE,
  95. CT_VERTICALTEXT,
  96. CT_CELL,
  97. CT_CONTEXTMENU,
  98. CT_ALIAS,
  99. CT_PROGRESS,
  100. CT_NODROP,
  101. CT_COPY,
  102. CT_NONE,
  103. CT_NOTALLOWED,
  104. CT_ZOOMIN,
  105. CT_ZOOMOUT,
  106. CT_GRAB,
  107. CT_GRABBING,
  108. CT_CUSTOM,
  109. } ECursorType;
  110. typedef enum e_file_dialog
  111. {
  112. FD_UNKNOWN,
  113. FD_OPEN_FILE,
  114. FD_OPEN_FOLDER,
  115. FD_OPEN_MULTIPLE_FILES,
  116. FD_SAVE_FILE,
  117. } EFileDialogType;
  118. public:
  119. //////////// initialization settings ////////////
  120. struct dullahan_settings
  121. {
  122. // initial dimensions of the browser window
  123. unsigned int initial_width = 512;
  124. unsigned int initial_height = 512;
  125. // host process name (for Windows - read only for API consumers)
  126. const std::string host_process_filename = "dullahan_host.exe";
  127. // host process path (Not required for macOS)
  128. std::string host_process_path = std::string();
  129. // substring inserted into existing user agent string
  130. // leave it blank by default otherwise "Chrome xx.x" part is removed
  131. std::string user_agent_substring = std::string();
  132. // default frame rate
  133. int frame_rate = 60;
  134. // enable/disable features - most obvious but listed for completeness
  135. bool begin_frame_scheduling = false; // fixes issue when onPaint not called
  136. bool cache_enabled = true; // local cache
  137. bool cookies_enabled = true; // cookies
  138. bool disable_gpu = true; // disable GPU and GPU compositing
  139. bool file_access_from_file_urls = false; // allow access files from local file system
  140. bool disable_web_security = false; // like adding --disable-web-security to Chrome command line
  141. bool disable_network_service = false; // like adding --disable-features=NetworkService to Chrome command line
  142. bool use_mock_keychain = false; // like adding --use-mock-keychain to Chrome command line
  143. bool autoplay_without_gesture = false; // like adding --autoplay-policy=???? to Chrome command line
  144. bool fake_ui_for_media_stream = false; // like adding --fake-ui-for-media-stream to Chrome command line
  145. bool flash_enabled = true; // system flash plugin
  146. bool force_wave_audio = false; // forces Windows WaveOut/In audio
  147. bool image_shrink_standalone_to_fit = true; // scale standalone images larger than browser size to fit
  148. bool java_enabled = false; // java
  149. bool javascript_enabled = true; // javascript
  150. bool media_stream_enabled = false; // web cams etc. (caution)
  151. bool plugins_enabled = true; // all plugins
  152. bool webgl_enabled = true; // webgl
  153. // explicitly set the path to the locales folder since defaults no longer work on some systems
  154. std::string locales_dir_path = std::string();
  155. // The root directory that all cache_path and context_cache_path values
  156. // must have in common.
  157. // If this value is empty and cache_path is non-empty then this value
  158. // will default to the cache_path value.
  159. std::string root_cache_path = std::string();
  160. // path to browser cache - cookies (if enabled) are also stored here as of Chrome 75
  161. // This will be used for global context
  162. std::string cache_path = std::string();
  163. // As of version 75 cef doesn't allow storing cookies separately from cache, but context
  164. // requests with individual cache path can be used to separate cookies.
  165. // Context's cache always should be a child to root cache path, simultaneous contexts with
  166. // same path do not share sessions.
  167. std::string context_cache_path = std::string();
  168. // list of language locale codes used to configure the Accept-Language HTTP header value
  169. // and change the default language of the browser
  170. std::string accept_language_list = "en-us";
  171. // host name:port to use as a web proxy
  172. std::string proxy_host_port = std::string();
  173. // background color displayed before first page loaded (RRGGBB)
  174. unsigned int background_color = 0xffffff;
  175. // flip pixel buffer in Y direction
  176. bool flip_pixels_y = false;
  177. // flip mouse input in Y direction
  178. bool flip_mouse_y = false;
  179. // location, name of CEF log file
  180. std::string log_file = "cef_log.txt";
  181. // whether to log verbosely (true) or not (false)
  182. int log_verbose = false;
  183. // allow Chrome (or other CEF windoW) to debug via http://localhost::PORT_NUMBER
  184. int remote_debugging_port = 1964;
  185. };
  186. public:
  187. //////////// the API itself ////////////
  188. dullahan();
  189. ~dullahan();
  190. // initialize everything - call before anything else
  191. bool init(dullahan_settings& user_settings);
  192. // close down CEF - call just before you exit
  193. void shutdown();
  194. // indicate to CEF you want to exit - after you call this,
  195. // wait for onRequestExit() callback before calling shutdown()
  196. void requestExit();
  197. // accessors for size of virtual window
  198. void getSize(int& width, int& height);
  199. void setSize(int width, int height);
  200. int getDepth();
  201. // run CEF in it's own message loop - doesn't exit until requestExit()
  202. // and shutdown() calls triggered
  203. // Note: complimentary to update();
  204. void run();
  205. // do some work in CEF - call regularly in your own message loop
  206. // Note: complimentary to run();
  207. void update();
  208. // transport control
  209. bool canGoBack();
  210. void goBack();
  211. bool canGoForward();
  212. void goForward();
  213. bool isLoading();
  214. void reload(const bool ignore_cache);
  215. void stop();
  216. // versions of CEF, Chrome and this library and one with everything
  217. const std::string dullahan_cef_version(bool show_bitwidth);
  218. const std::string dullahan_chrome_version(bool show_bitwidth);
  219. const std::string dullahan_version(bool show_bitwidth);
  220. const std::string composite_version();
  221. // returns a user agent string based off of passed in string that
  222. // is "more" compatible with sites that look for a specific string
  223. std::string makeCompatibleUserAgentString(const std::string base);
  224. // mouse input
  225. void mouseButton(EMouseButton mouse_button,
  226. EMouseEvent mouse_event,
  227. int x, int y);
  228. void mouseMove(int x, int y);
  229. void mouseWheel(int x, int y, int delta_x, int delta_y);
  230. // keyboard input
  231. #ifndef __linux__
  232. void nativeKeyboardEventWin(uint32_t msg, uint32_t wparam, uint64_t lparam);
  233. void nativeKeyboardEventOSX(void* event);
  234. void nativeKeyboardEventOSX(dullahan::EKeyEvent event_type, uint32_t event_modifiers, uint32_t event_keycode,
  235. uint32_t event_chars, uint32_t event_umodchars, bool event_isrepeat);
  236. #else
  237. void nativeKeyboardEvent(dullahan::EKeyEvent key_event, uint32_t native_scan_code, uint32_t native_virtual_key, uint32_t native_modifiers);
  238. void nativeKeyboardEventSDL2(dullahan::EKeyEvent key_event, uint32_t key_data, uint32_t key_modifiers, bool keypad_input);
  239. #endif
  240. // navigate to a URL
  241. void navigate(const std::string url);
  242. // give focus to virtual browser window
  243. void setFocus();
  244. // set the page zoom
  245. void setPageZoom(const double zoom_val);
  246. // indicates if there is something available to be copy/cut/pasted
  247. // (for UI purposes) and if so, provides methods to do so
  248. bool editCanCopy();
  249. bool editCanCut();
  250. bool editCanPaste();
  251. void editCopy();
  252. void editCut();
  253. void editPaste();
  254. // show/hide the dev tools
  255. void showDevTools();
  256. void closeDevTools();
  257. // print page to PDF
  258. void printToPDF(const std::string path);
  259. // cookies
  260. bool setCookie(const std::string url,
  261. const std::string name, const std::string value,
  262. const std::string domain, const std::string path,
  263. bool httponly, bool secure);
  264. const std::vector<std::string> getCookies();
  265. void deleteAllCookies();
  266. // POST data to a URL
  267. void postData(const std::string url,
  268. const std::string data,
  269. const std::string headers);
  270. // javascript
  271. bool executeJavaScript(const std::string cmd);
  272. // display a message page in the browser - e.g. URL cannot be loaded
  273. void showBrowserMessage(const std::string msg);
  274. // set/gate the schemes to intercept, halt browsing and trigger callback
  275. void setCustomSchemes(std::vector<std::string> custom_schemes);
  276. std::vector<std::string>& getCustomSchemes();
  277. //////////// callback setters ////////////
  278. // URL changes - e.g. redirect
  279. void setOnAddressChangeCallback(std::function<void(const std::string url)> callback);
  280. // message appears in the JavaScript console
  281. void setOnConsoleMessageCallback(std::function<void(const std::string message,
  282. const std::string source, int line)> callback);
  283. // cursor changes - e.g. as passed over hyperlink or entered text field
  284. void setOnCursorChangedCallback(std::function<void(const ECursorType type)> callback);
  285. // custom URL scheme link is clicked (see setCustomSchemes(..))
  286. void setOnCustomSchemeURLCallback(std::function<void(const std::string url,
  287. bool user_gesture,
  288. bool is_redirect)> callback);
  289. // HTTP auth request triggered
  290. void setOnHTTPAuthCallback(std::function<bool(const std::string host,
  291. const std::string realm,
  292. std::string& username, std::string& password)> callback);
  293. // page finishes loading
  294. void setOnLoadEndCallback(std::function<void(int status, const std::string url)> callback);
  295. // page load error - e.g. 404
  296. void setOnLoadErrorCallback(std::function<void(int status, const std::string error_text)> callback);
  297. // page starts to load
  298. void setOnLoadStartCallback(std::function<void()> callback);
  299. // popup opened
  300. void setOnOpenPopupCallback(std::function<void(const std::string url,
  301. const std::string target)> callback);
  302. // contents of the pages changes
  303. void setOnPageChangedCallback(std::function<void(const unsigned char* pixels,
  304. int x, int y,
  305. int width, int height)> callback);
  306. // exit app requested
  307. void setOnRequestExitCallback(std::function<void()> callback);
  308. // browser status message changes
  309. void setOnStatusMessageCallback(std::function<void(const std::string message)> callback);
  310. // page title changes
  311. void setOnTitleChangeCallback(std::function<void(const std::string title)> callback);
  312. void setOnTooltipCallback(std::function<void(const std::string text)> callback);
  313. // a call to printToPDF completed
  314. void setOnPdfPrintFinishedCallback(std::function<void(const std::string path, bool ok)> callback);
  315. // file download progress
  316. void setOnFileDownloadProgressCallback(std::function<void(int percent, bool complete)> callback);
  317. // file picker shown
  318. void setOnFileDialogCallback(std::function<const std::vector<std::string>(dullahan::EFileDialogType dialog_type, const std::string dialog_title, const std::string default_file, const std::string dialog_accept_filter, bool& use_default)> callback);
  319. // JS dialog shown (alert)
  320. void setOnJSDialogCallback(std::function<bool(const std::string origin_url,
  321. const std::string message_text,
  322. const std::string default_prompt_text)> callback);
  323. // JS before unload callback (alert)
  324. void setOnJSBeforeUnloadCallback(std::function<bool()> callback);
  325. private:
  326. std::unique_ptr <dullahan_impl> mImpl;
  327. };
  328. #endif // _DULLAHAN