[email protected] | a3b85d85 | 2012-01-27 02:04:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_PLUGIN_LOADER_POSIX_H_ |
| 6 | #define CONTENT_BROWSER_PLUGIN_LOADER_POSIX_H_ |
| 7 | |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | d9b73c8 | 2011-11-09 23:17:28 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 13 | #include "base/time/time.h" |
[email protected] | e67385f | 2011-12-21 06:00:56 | [diff] [blame] | 14 | #include "content/browser/plugin_service_impl.h" |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 15 | #include "content/public/browser/utility_process_host_client.h" |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 16 | #include "content/public/common/webplugininfo.h" |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 17 | #include "ipc/ipc_sender.h" |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 18 | |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 19 | namespace base { |
| 20 | class MessageLoopProxy; |
| 21 | } |
| 22 | |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 23 | namespace content { |
| 24 | class UtilityProcessHost; |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 25 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 26 | // This class is responsible for managing the out-of-process plugin loading on |
| 27 | // POSIX systems. It primarily lives on the IO thread, but has a brief stay on |
| 28 | // the FILE thread to iterate over plugin directories when it is first |
| 29 | // constructed. |
| 30 | // |
| 31 | // The following is the algorithm used to load plugins: |
| 32 | // 1. This asks the PluginList for the list of all potential plugins to attempt |
| 33 | // to load. This is referred to as the canonical list. |
| 34 | // 2. The child process this hosts is forked and the canonical list is sent to |
| 35 | // it. |
| 36 | // 3. The child process iterates over the canonical list, attempting to load |
| 37 | // each plugin in the order specified by the list. It sends an IPC message |
| 38 | // to the browser after each load, indicating success or failure. The two |
| 39 | // processes synchronize the position in the vector that will be used to |
| 40 | // attempt to load the next plugin. |
| 41 | // 4. If the child dies during this process, the host forks another child and |
| 42 | // resumes loading at the position past the plugin that it just attempted to |
| 43 | // load, bypassing the problematic plugin. |
| 44 | // 5. This algorithm continues until the canonical list has been walked to the |
| 45 | // end, after which the list of loaded plugins is set on the PluginList and |
| 46 | // the completion callback is run. |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 47 | class CONTENT_EXPORT PluginLoaderPosix |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 48 | : public NON_EXPORTED_BASE(UtilityProcessHostClient), |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 49 | public IPC::Sender { |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 50 | public: |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 51 | PluginLoaderPosix(); |
| 52 | |
[email protected] | aa7f880 | 2014-01-27 16:56:32 | [diff] [blame] | 53 | // Must be called from the IO thread. The |callback| will be called on the IO |
| 54 | // thread too. |
| 55 | void GetPlugins(const PluginService::GetPluginsCallback& callback); |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 56 | |
[email protected] | c4f883a | 2012-02-03 17:02:07 | [diff] [blame] | 57 | // UtilityProcessHostClient: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 58 | void OnProcessCrashed(int exit_code) override; |
erikchen | f610f138 | 2014-11-11 03:28:16 | [diff] [blame] | 59 | void OnProcessLaunchFailed() override; |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 60 | bool OnMessageReceived(const IPC::Message& message) override; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 61 | |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 62 | // IPC::Sender: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 63 | bool Send(IPC::Message* msg) override; |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 64 | |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 65 | private: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 66 | ~PluginLoaderPosix() override; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 67 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 68 | // Called on the FILE thread to get the list of plugin paths to probe. |
| 69 | void GetPluginsToLoad(); |
| 70 | |
| 71 | // Must be called on the IO thread. |
| 72 | virtual void LoadPluginsInternal(); |
| 73 | |
[email protected] | aa7f880 | 2014-01-27 16:56:32 | [diff] [blame] | 74 | // Called after plugin loading has finished, if we don't know whether the |
| 75 | // plugin list has been invalidated in the mean time. |
| 76 | void GetPluginsWrapper( |
| 77 | const PluginService::GetPluginsCallback& callback, |
| 78 | const std::vector<WebPluginInfo>& plugins_unused); |
| 79 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 80 | // Message handlers. |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 81 | void OnPluginLoaded(uint32 index, const WebPluginInfo& plugin); |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 82 | void OnPluginLoadFailed(uint32 index, const base::FilePath& plugin_path); |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 83 | |
erikchen | f610f138 | 2014-11-11 03:28:16 | [diff] [blame] | 84 | // Returns an iterator to the plugin in |internal_plugins_| whose path |
| 85 | // matches |plugin_path|. |
| 86 | std::vector<WebPluginInfo>::iterator FindInternalPlugin( |
| 87 | const base::FilePath& plugin_path); |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 88 | |
| 89 | // Runs all the registered callbacks on each's target loop if the condition |
| 90 | // for ending the load process is done (i.e. the |next_load_index_| is outside |
[email protected] | 5abe630 | 2011-12-20 23:44:32 | [diff] [blame] | 91 | // the range of the |canonical_list_|). |
[email protected] | 6a0dc7a | 2011-11-02 14:37:07 | [diff] [blame] | 92 | bool MaybeRunPendingCallbacks(); |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 93 | |
erikchen | f610f138 | 2014-11-11 03:28:16 | [diff] [blame] | 94 | // Returns true if there are no plugins left to load. |
| 95 | bool IsFinishedLoadingPlugins(); |
| 96 | |
| 97 | // This method should be called when the plugins are finished loading. |
| 98 | // It updates the PluginList's list of plugins, and runs the queued callbacks. |
| 99 | void FinishedLoadingPlugins(); |
| 100 | |
| 101 | // Launches the utility process that loads the plugins. |
| 102 | // Virtual for testing. |
| 103 | virtual bool LaunchUtilityProcess(); |
| 104 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 105 | // The process host for which this is a client. |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 106 | base::WeakPtr<UtilityProcessHost> process_host_; |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 107 | |
| 108 | // A list of paths to plugins which will be loaded by the utility process, in |
| 109 | // the order specified by this vector. |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 110 | std::vector<base::FilePath> canonical_list_; |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 111 | |
| 112 | // The index in |canonical_list_| of the plugin that the child process will |
| 113 | // attempt to load next. |
| 114 | size_t next_load_index_; |
| 115 | |
| 116 | // Internal plugins that have been registered at the time of loading. |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 117 | std::vector<WebPluginInfo> internal_plugins_; |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 118 | |
| 119 | // A vector of plugins that have been loaded successfully. |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 120 | std::vector<WebPluginInfo> loaded_plugins_; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 121 | |
| 122 | // The callback and message loop on which the callback will be run when the |
| 123 | // plugin loading process has been completed. |
[email protected] | aa7f880 | 2014-01-27 16:56:32 | [diff] [blame] | 124 | std::vector<PluginService::GetPluginsCallback> callbacks_; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 125 | |
erikchen | f610f138 | 2014-11-11 03:28:16 | [diff] [blame] | 126 | // True if there is (or is about to be) a utility process that loads plugins. |
| 127 | bool loading_plugins_; |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 128 | |
| 129 | friend class MockPluginLoaderPosix; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 130 | DISALLOW_COPY_AND_ASSIGN(PluginLoaderPosix); |
| 131 | }; |
| 132 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 133 | } // namespace content |
| 134 | |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 135 | #endif // CONTENT_BROWSER_PLUGIN_LOADER_POSIX_H_ |