Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 92ad3f7 | 2011-09-02 03:06:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 4 | |
| 5 | // The portable representation of an instance and root scriptable object. |
| 6 | // The PPAPI version of the plugin instantiates a subclass of this class. |
| 7 | |
mseaborn | 45df3da | 2015-01-29 19:39:22 | [diff] [blame] | 8 | #ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_ |
| 9 | #define COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_ |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 10 | |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 11 | #include <stdint.h> |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 12 | #include <stdio.h> |
[email protected] | c98851c7 | 2011-09-08 18:06:44 | [diff] [blame] | 13 | |
dcheng | 24f43a5e9 | 2016-04-22 18:29:09 | [diff] [blame] | 14 | #include <memory> |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 15 | #include <string> |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 16 | |
mseaborn | 45df3da | 2015-01-29 19:39:22 | [diff] [blame] | 17 | #include "components/nacl/renderer/plugin/nacl_subprocess.h" |
| 18 | #include "components/nacl/renderer/plugin/pnacl_coordinator.h" |
| 19 | #include "components/nacl/renderer/plugin/service_runtime.h" |
mseaborn | 3cf62d9 | 2015-02-10 21:20:01 | [diff] [blame] | 20 | #include "components/nacl/renderer/ppb_nacl_private.h" |
[email protected] | 5feb6cd | 2014-02-12 22:41:13 | [diff] [blame] | 21 | #include "ppapi/cpp/instance.h" |
[email protected] | 4564949 | 2014-01-24 20:49:34 | [diff] [blame] | 22 | #include "ppapi/cpp/private/uma_private.h" |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 23 | #include "ppapi/cpp/url_loader.h" |
| 24 | #include "ppapi/cpp/var.h" |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 25 | #include "ppapi/cpp/view.h" |
[email protected] | b7a490e | 2014-07-23 16:58:15 | [diff] [blame] | 26 | #include "ppapi/utility/completion_callback_factory.h" |
| 27 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 28 | namespace pp { |
[email protected] | 557c300 | 2014-03-27 07:39:10 | [diff] [blame] | 29 | class CompletionCallback; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 30 | class URLLoader; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 33 | namespace plugin { |
| 34 | |
| 35 | class ErrorInfo; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 36 | |
[email protected] | 5e64f638 | 2014-06-30 16:18:53 | [diff] [blame] | 37 | const PP_NaClFileInfo kInvalidNaClFileInfo = { |
| 38 | PP_kInvalidFileHandle, |
| 39 | 0, // token_lo |
| 40 | 0, // token_hi |
| 41 | }; |
| 42 | |
[email protected] | 5feb6cd | 2014-02-12 22:41:13 | [diff] [blame] | 43 | class Plugin : public pp::Instance { |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 44 | public: |
[email protected] | 2a292cf4 | 2014-04-29 20:12:43 | [diff] [blame] | 45 | explicit Plugin(PP_Instance instance); |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 46 | |
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 47 | Plugin(const Plugin&) = delete; |
| 48 | Plugin& operator=(const Plugin&) = delete; |
| 49 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 50 | // ----- Methods inherited from pp::Instance: |
| 51 | |
| 52 | // Initializes this plugin with <embed/object ...> tag attribute count |argc|, |
| 53 | // names |argn| and values |argn|. Returns false on failure. |
| 54 | // Gets called by the browser right after New(). |
mseaborn | a202e283 | 2015-01-26 17:59:37 | [diff] [blame] | 55 | bool Init(uint32_t argc, const char* argn[], const char* argv[]) override; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 56 | |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 57 | // Handles document load, when the plugin is a MIME type handler. |
mseaborn | a202e283 | 2015-01-26 17:59:37 | [diff] [blame] | 58 | bool HandleDocumentLoad(const pp::URLLoader& url_loader) override; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 59 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 60 | // Load support. |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 61 | // |
| 62 | // Starts NaCl module but does not wait until low-level |
[email protected] | 2efbb64 | 2014-05-28 22:43:17 | [diff] [blame] | 63 | // initialization (e.g. ld.so dynamic loading of manifest files) is |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 64 | // done. The module will become ready later, asynchronously. Other |
| 65 | // event handlers should block until the module is ready before |
| 66 | // trying to communicate with it, i.e., until nacl_ready_state is |
[email protected] | 7522077b | 2013-01-17 23:39:40 | [diff] [blame] | 67 | // DONE. |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 68 | // |
| 69 | // NB: currently we do not time out, so if the untrusted code |
| 70 | // does not signal that it is ready, then we will deadlock the main |
| 71 | // thread of the renderer on this subsequent event delivery. We |
| 72 | // should include a time-out at which point we declare the |
| 73 | // nacl_ready_state to be done, and let the normal crash detection |
| 74 | // mechanism(s) take over. |
jvoung | a26ccdf | 2015-05-12 15:19:31 | [diff] [blame] | 75 | // This function takes over ownership of the file_info. |
[email protected] | 2efbb64 | 2014-05-28 22:43:17 | [diff] [blame] | 76 | void LoadNaClModule(PP_NaClFileInfo file_info, |
mseaborn | 6e97577 | 2015-01-23 04:39:15 | [diff] [blame] | 77 | PP_NaClAppProcessType process_type); |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 78 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 79 | // Load support. |
[email protected] | 5e64f638 | 2014-06-30 16:18:53 | [diff] [blame] | 80 | // A helper SRPC NaCl module can be loaded given a PP_NaClFileInfo. |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 81 | // Does not update nacl_module_origin(). |
jvoung | a26ccdf | 2015-05-12 15:19:31 | [diff] [blame] | 82 | // Uses the given NaClSubprocess to contain the new SelLdr process. |
| 83 | // The given callback is called when the loading is complete. |
| 84 | // This function takes over ownership of the file_info. |
| 85 | void LoadHelperNaClModule(const std::string& helper_url, |
| 86 | PP_NaClFileInfo file_info, |
| 87 | NaClSubprocess* subprocess_to_init, |
| 88 | pp::CompletionCallback callback); |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 89 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 90 | // Report an error that was encountered while loading a module. |
| 91 | void ReportLoadError(const ErrorInfo& error_info); |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 92 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 93 | private: |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 94 | // The browser will invoke the destructor via the pp::Instance |
| 95 | // pointer to this object, not from base's Delete(). |
mseaborn | a202e283 | 2015-01-26 17:59:37 | [diff] [blame] | 96 | ~Plugin() override; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 97 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 98 | // Shuts down socket connection, service runtime, and receive thread, |
[email protected] | 828ba952 | 2012-01-30 23:11:02 | [diff] [blame] | 99 | // in this order, for the main nacl subprocess. |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 100 | void ShutDownSubprocesses(); |
| 101 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 102 | // Callback used when getting the URL for the .nexe file. If the URL loading |
| 103 | // is successful, the file descriptor is opened and can be passed to sel_ldr |
| 104 | // with the sandbox on. |
| 105 | void NexeFileDidOpen(int32_t pp_error); |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 106 | |
| 107 | // Callback used when a .nexe is translated from bitcode. If the translation |
| 108 | // is successful, the file descriptor is opened and can be passed to sel_ldr |
| 109 | // with the sandbox on. |
| 110 | void BitcodeDidTranslate(int32_t pp_error); |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 111 | |
| 112 | // NaCl ISA selection manifest file support. The manifest file is specified |
| 113 | // using the "nacl" attribute in the <embed> tag. First, the manifest URL (or |
| 114 | // data: URI) is fetched, then the JSON is parsed. Once a valid .nexe is |
| 115 | // chosen for the sandbox ISA, any current service runtime is shut down, the |
| 116 | // .nexe is loaded and run. |
| 117 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 118 | // Callback used when getting the manifest file as a local file descriptor. |
| 119 | void NaClManifestFileDidOpen(int32_t pp_error); |
| 120 | |
| 121 | // Processes the JSON manifest string and starts loading the nexe. |
Justin TerAvest | 82f0d05 | 2014-09-10 20:02:03 | [diff] [blame] | 122 | void ProcessNaClManifest(const std::string& manifest_json); |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 123 | |
[email protected] | 828ba952 | 2012-01-30 23:11:02 | [diff] [blame] | 124 | // Keep track of the NaCl module subprocess that was spun up in the plugin. |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 125 | NaClSubprocess main_subprocess_; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 126 | |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 127 | pp::CompletionCallbackFactory<Plugin> callback_factory_; |
| 128 | |
dcheng | 24f43a5e9 | 2016-04-22 18:29:09 | [diff] [blame] | 129 | std::unique_ptr<PnaclCoordinator> pnacl_coordinator_; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 130 | |
[email protected] | 0554b5d | 2013-11-27 18:39:19 | [diff] [blame] | 131 | int exit_status_; |
[email protected] | 17b5a817 | 2012-06-22 21:09:09 | [diff] [blame] | 132 | |
[email protected] | 2efbb64 | 2014-05-28 22:43:17 | [diff] [blame] | 133 | PP_NaClFileInfo nexe_file_info_; |
[email protected] | 728a963 | 2014-05-16 20:18:20 | [diff] [blame] | 134 | |
[email protected] | 4564949 | 2014-01-24 20:49:34 | [diff] [blame] | 135 | pp::UMAPrivate uma_interface_; |
[email protected] | c59f771b | 2011-08-25 01:07:44 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | } // namespace plugin |
| 139 | |
mseaborn | 45df3da | 2015-01-29 19:39:22 | [diff] [blame] | 140 | #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_ |