1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 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 8 #ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_ 9 #define COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_ 10 11 #include <stdint.h> 12 #include <stdio.h> 13 14 #include <memory> 15 #include <string> 16 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" 20 #include "components/nacl/renderer/ppb_nacl_private.h" 21 #include "ppapi/cpp/instance.h" 22 #include "ppapi/cpp/private/uma_private.h" 23 #include "ppapi/cpp/url_loader.h" 24 #include "ppapi/cpp/var.h" 25 #include "ppapi/cpp/view.h" 26 #include "ppapi/utility/completion_callback_factory.h" 27 28 namespace pp { 29 class CompletionCallback; 30 class URLLoader; 31 } 32 33 namespace plugin { 34 35 class ErrorInfo; 36 37 const PP_NaClFileInfo kInvalidNaClFileInfo = { 38 PP_kInvalidFileHandle, 39 0, // token_lo 40 0, // token_hi 41 }; 42 43 class Plugin : public pp::Instance { 44 public: 45 explicit Plugin(PP_Instance instance); 46 47 Plugin(const Plugin&) = delete; 48 Plugin& operator=(const Plugin&) = delete; 49 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(). 55 bool Init(uint32_t argc, const char* argn[], const char* argv[]) override; 56 57 // Handles document load, when the plugin is a MIME type handler. 58 bool HandleDocumentLoad(const pp::URLLoader& url_loader) override; 59 60 // Load support. 61 // 62 // Starts NaCl module but does not wait until low-level 63 // initialization (e.g. ld.so dynamic loading of manifest files) is 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 67 // DONE. 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. 75 // This function takes over ownership of the file_info. 76 void LoadNaClModule(PP_NaClFileInfo file_info, 77 PP_NaClAppProcessType process_type); 78 79 // Load support. 80 // A helper SRPC NaCl module can be loaded given a PP_NaClFileInfo. 81 // Does not update nacl_module_origin(). 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); 89 90 // Report an error that was encountered while loading a module. 91 void ReportLoadError(const ErrorInfo& error_info); 92 93 private: 94 // The browser will invoke the destructor via the pp::Instance 95 // pointer to this object, not from base's Delete(). 96 ~Plugin() override; 97 98 // Shuts down socket connection, service runtime, and receive thread, 99 // in this order, for the main nacl subprocess. 100 void ShutDownSubprocesses(); 101 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); 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); 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 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. 122 void ProcessNaClManifest(const std::string& manifest_json); 123 124 // Keep track of the NaCl module subprocess that was spun up in the plugin. 125 NaClSubprocess main_subprocess_; 126 127 pp::CompletionCallbackFactory<Plugin> callback_factory_; 128 129 std::unique_ptr<PnaclCoordinator> pnacl_coordinator_; 130 131 int exit_status_; 132 133 PP_NaClFileInfo nexe_file_info_; 134 135 pp::UMAPrivate uma_interface_; 136 }; 137 138 } // namespace plugin 139 140 #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_ 141