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