[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 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 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/memory/ref_counted.h" |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 12 | #include "content/browser/plugin_service.h" |
| 13 | #include "content/browser/utility_process_host.h" |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 14 | #include "ipc/ipc_message.h" |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 15 | #include "webkit/plugins/webplugininfo.h" |
| 16 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 17 | class FilePath; |
| 18 | class UtilityProcessHost; |
| 19 | |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 20 | namespace base { |
| 21 | class MessageLoopProxy; |
| 22 | } |
| 23 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 24 | // This class is responsible for managing the out-of-process plugin loading on |
| 25 | // POSIX systems. It primarily lives on the IO thread, but has a brief stay on |
| 26 | // the FILE thread to iterate over plugin directories when it is first |
| 27 | // constructed. |
| 28 | // |
| 29 | // The following is the algorithm used to load plugins: |
| 30 | // 1. This asks the PluginList for the list of all potential plugins to attempt |
| 31 | // to load. This is referred to as the canonical list. |
| 32 | // 2. The child process this hosts is forked and the canonical list is sent to |
| 33 | // it. |
| 34 | // 3. The child process iterates over the canonical list, attempting to load |
| 35 | // each plugin in the order specified by the list. It sends an IPC message |
| 36 | // to the browser after each load, indicating success or failure. The two |
| 37 | // processes synchronize the position in the vector that will be used to |
| 38 | // attempt to load the next plugin. |
| 39 | // 4. If the child dies during this process, the host forks another child and |
| 40 | // resumes loading at the position past the plugin that it just attempted to |
| 41 | // load, bypassing the problematic plugin. |
| 42 | // 5. This algorithm continues until the canonical list has been walked to the |
| 43 | // end, after which the list of loaded plugins is set on the PluginList and |
| 44 | // the completion callback is run. |
[email protected] | f7e91ea4b | 2011-11-01 20:17:19 | [diff] [blame] | 45 | class CONTENT_EXPORT PluginLoaderPosix : public UtilityProcessHost::Client, |
| 46 | IPC::Message::Sender { |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 47 | public: |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 48 | PluginLoaderPosix(); |
| 49 | |
| 50 | // Must be called from the IO thread. |
| 51 | void LoadPlugins(scoped_refptr<base::MessageLoopProxy> target_loop, |
| 52 | const PluginService::GetPluginsCallback& callback); |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 53 | |
| 54 | // UtilityProcessHost::Client: |
| 55 | virtual void OnProcessCrashed(int exit_code) OVERRIDE; |
| 56 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 57 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 58 | // IPC::Message::Sender: |
| 59 | virtual bool Send(IPC::Message* msg); |
| 60 | |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 61 | private: |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 62 | struct PendingCallback { |
| 63 | PendingCallback(scoped_refptr<base::MessageLoopProxy> target_loop, |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 64 | const PluginService::GetPluginsCallback& callback); |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 65 | ~PendingCallback(); |
| 66 | |
| 67 | scoped_refptr<base::MessageLoopProxy> target_loop; |
| 68 | PluginService::GetPluginsCallback callback; |
| 69 | }; |
| 70 | |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 71 | virtual ~PluginLoaderPosix(); |
| 72 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 73 | // Called on the FILE thread to get the list of plugin paths to probe. |
| 74 | void GetPluginsToLoad(); |
| 75 | |
| 76 | // Must be called on the IO thread. |
| 77 | virtual void LoadPluginsInternal(); |
| 78 | |
| 79 | // Message handlers. |
| 80 | void OnPluginLoaded(const webkit::WebPluginInfo& plugin); |
| 81 | void OnPluginLoadFailed(const FilePath& plugin_path); |
| 82 | |
| 83 | // Checks if the plugin path is an internal plugin, and, if it is, adds it to |
| 84 | // |loaded_plugins_|. |
| 85 | bool MaybeAddInternalPlugin(const FilePath& plugin_path); |
| 86 | |
| 87 | // Runs all the registered callbacks on each's target loop if the condition |
| 88 | // for ending the load process is done (i.e. the |next_load_index_| is outside |
| 89 | // the ranage of the |canonical_list_|). |
[email protected] | 6a0dc7a | 2011-11-02 14:37:07 | [diff] [blame^] | 90 | bool MaybeRunPendingCallbacks(); |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 91 | |
| 92 | // The process host for which this is a client. |
| 93 | UtilityProcessHost* process_host_; |
| 94 | |
| 95 | // A list of paths to plugins which will be loaded by the utility process, in |
| 96 | // the order specified by this vector. |
| 97 | std::vector<FilePath> canonical_list_; |
| 98 | |
| 99 | // The index in |canonical_list_| of the plugin that the child process will |
| 100 | // attempt to load next. |
| 101 | size_t next_load_index_; |
| 102 | |
| 103 | // Internal plugins that have been registered at the time of loading. |
| 104 | std::vector<webkit::WebPluginInfo> internal_plugins_; |
| 105 | |
| 106 | // A vector of plugins that have been loaded successfully. |
| 107 | std::vector<webkit::WebPluginInfo> loaded_plugins_; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 108 | |
| 109 | // The callback and message loop on which the callback will be run when the |
| 110 | // plugin loading process has been completed. |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 111 | std::vector<PendingCallback> callbacks_; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 112 | |
[email protected] | d4af1e7 | 2011-10-21 17:45:43 | [diff] [blame] | 113 | // The time at which plugin loading started. |
| 114 | base::TimeTicks load_start_time_; |
| 115 | |
| 116 | friend class MockPluginLoaderPosix; |
[email protected] | 6be08ae | 2011-10-18 02:23:23 | [diff] [blame] | 117 | DISALLOW_COPY_AND_ASSIGN(PluginLoaderPosix); |
| 118 | }; |
| 119 | |
| 120 | #endif // CONTENT_BROWSER_PLUGIN_LOADER_POSIX_H_ |