Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / nacl / renderer / plugin / plugin.h
blobf0af7645a5acea6b5cc274d95cb502afc5069b06
1 // -*- c++ -*-
2 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 // The portable representation of an instance and root scriptable object.
7 // The PPAPI version of the plugin instantiates a subclass of this class.
9 #ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_
10 #define COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_
12 #include <stdio.h>
14 #include <string>
16 #include "components/nacl/renderer/plugin/nacl_subprocess.h"
17 #include "components/nacl/renderer/plugin/pnacl_coordinator.h"
18 #include "components/nacl/renderer/plugin/service_runtime.h"
19 #include "components/nacl/renderer/plugin/utility.h"
20 #include "components/nacl/renderer/ppb_nacl_private.h"
21 #include "native_client/src/include/nacl_macros.h"
22 #include "native_client/src/include/nacl_scoped_ptr.h"
23 #include "ppapi/cpp/instance.h"
24 #include "ppapi/cpp/private/uma_private.h"
25 #include "ppapi/cpp/url_loader.h"
26 #include "ppapi/cpp/var.h"
27 #include "ppapi/cpp/view.h"
28 #include "ppapi/utility/completion_callback_factory.h"
30 namespace nacl {
31 class DescWrapper;
32 class DescWrapperFactory;
33 } // namespace nacl
35 namespace pp {
36 class CompletionCallback;
37 class URLLoader;
38 class URLUtil_Dev;
41 namespace plugin {
43 class ErrorInfo;
44 class Manifest;
46 const PP_NaClFileInfo kInvalidNaClFileInfo = {
47 PP_kInvalidFileHandle,
48 0, // token_lo
49 0, // token_hi
52 class Plugin : public pp::Instance {
53 public:
54 explicit Plugin(PP_Instance instance);
56 // ----- Methods inherited from pp::Instance:
58 // Initializes this plugin with <embed/object ...> tag attribute count |argc|,
59 // names |argn| and values |argn|. Returns false on failure.
60 // Gets called by the browser right after New().
61 bool Init(uint32_t argc, const char* argn[], const char* argv[]) override;
63 // Handles document load, when the plugin is a MIME type handler.
64 bool HandleDocumentLoad(const pp::URLLoader& url_loader) override;
66 // Load support.
68 // Starts NaCl module but does not wait until low-level
69 // initialization (e.g. ld.so dynamic loading of manifest files) is
70 // done. The module will become ready later, asynchronously. Other
71 // event handlers should block until the module is ready before
72 // trying to communicate with it, i.e., until nacl_ready_state is
73 // DONE.
75 // NB: currently we do not time out, so if the untrusted code
76 // does not signal that it is ready, then we will deadlock the main
77 // thread of the renderer on this subsequent event delivery. We
78 // should include a time-out at which point we declare the
79 // nacl_ready_state to be done, and let the normal crash detection
80 // mechanism(s) take over.
81 // This function takes over ownership of the file_info.
82 void LoadNaClModule(PP_NaClFileInfo file_info,
83 bool uses_nonsfi_mode,
84 PP_NaClAppProcessType process_type);
86 // Load support.
87 // A helper SRPC NaCl module can be loaded given a PP_NaClFileInfo.
88 // Does not update nacl_module_origin().
89 // Uses the given NaClSubprocess to contain the new SelLdr process.
90 // The given callback is called when the loading is complete.
91 // This function takes over ownership of the file_info.
92 void LoadHelperNaClModule(const std::string& helper_url,
93 PP_NaClFileInfo file_info,
94 NaClSubprocess* subprocess_to_init,
95 pp::CompletionCallback callback);
97 // Report an error that was encountered while loading a module.
98 void ReportLoadError(const ErrorInfo& error_info);
100 nacl::DescWrapperFactory* wrapper_factory() const { return wrapper_factory_; }
102 const PPB_NaCl_Private* nacl_interface() const { return nacl_interface_; }
104 private:
105 NACL_DISALLOW_COPY_AND_ASSIGN(Plugin);
106 // The browser will invoke the destructor via the pp::Instance
107 // pointer to this object, not from base's Delete().
108 ~Plugin() override;
110 // Shuts down socket connection, service runtime, and receive thread,
111 // in this order, for the main nacl subprocess.
112 void ShutDownSubprocesses();
114 // Start sel_ldr given the start params. This is invoked on the main thread.
115 void StartSelLdr(ServiceRuntime* service_runtime,
116 const SelLdrStartParams& params,
117 pp::CompletionCallback callback);
119 // This is invoked on the main thread.
120 void StartNexe(int32_t pp_error, ServiceRuntime* service_runtime);
122 // Continuation for LoadHelperNaClModule. This is invoked on the main thread.
123 void StartHelperNexe(int32_t pp_error,
124 NaClSubprocess* subprocess_to_init,
125 pp::CompletionCallback callback);
127 // Callback used when getting the URL for the .nexe file. If the URL loading
128 // is successful, the file descriptor is opened and can be passed to sel_ldr
129 // with the sandbox on.
130 void NexeFileDidOpen(int32_t pp_error);
132 // Callback used when a .nexe is translated from bitcode. If the translation
133 // is successful, the file descriptor is opened and can be passed to sel_ldr
134 // with the sandbox on.
135 void BitcodeDidTranslate(int32_t pp_error);
137 // NaCl ISA selection manifest file support. The manifest file is specified
138 // using the "nacl" attribute in the <embed> tag. First, the manifest URL (or
139 // data: URI) is fetched, then the JSON is parsed. Once a valid .nexe is
140 // chosen for the sandbox ISA, any current service runtime is shut down, the
141 // .nexe is loaded and run.
143 // Callback used when getting the manifest file as a local file descriptor.
144 void NaClManifestFileDidOpen(int32_t pp_error);
146 // Processes the JSON manifest string and starts loading the nexe.
147 void ProcessNaClManifest(const std::string& manifest_json);
149 // Keep track of the NaCl module subprocess that was spun up in the plugin.
150 NaClSubprocess main_subprocess_;
152 bool uses_nonsfi_mode_;
154 nacl::DescWrapperFactory* wrapper_factory_;
156 pp::CompletionCallbackFactory<Plugin> callback_factory_;
158 nacl::scoped_ptr<PnaclCoordinator> pnacl_coordinator_;
160 int exit_status_;
162 PP_NaClFileInfo nexe_file_info_;
164 const PPB_NaCl_Private* nacl_interface_;
165 pp::UMAPrivate uma_interface_;
168 } // namespace plugin
170 #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PLUGIN_H_