Roll src/third_party/WebKit e353f22:4d062fa (svn 200353:200356)
[chromium-blink-merge.git] / content / browser / plugin_loader_posix.h
blob2db16341b0d5c204878101dfd935211490efe9c3
1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_BROWSER_PLUGIN_LOADER_POSIX_H_
6 #define CONTENT_BROWSER_PLUGIN_LOADER_POSIX_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/time/time.h"
14 #include "content/browser/plugin_service_impl.h"
15 #include "content/public/browser/utility_process_host_client.h"
16 #include "content/public/common/webplugininfo.h"
17 #include "ipc/ipc_sender.h"
19 namespace content {
20 class UtilityProcessHost;
22 // This class is responsible for managing the out-of-process plugin loading on
23 // POSIX systems. It primarily lives on the IO thread, but has a brief stay on
24 // the FILE thread to iterate over plugin directories when it is first
25 // constructed.
27 // The following is the algorithm used to load plugins:
28 // 1. This asks the PluginList for the list of all potential plugins to attempt
29 // to load. This is referred to as the canonical list.
30 // 2. The child process this hosts is forked and the canonical list is sent to
31 // it.
32 // 3. The child process iterates over the canonical list, attempting to load
33 // each plugin in the order specified by the list. It sends an IPC message
34 // to the browser after each load, indicating success or failure. The two
35 // processes synchronize the position in the vector that will be used to
36 // attempt to load the next plugin.
37 // 4. If the child dies during this process, the host forks another child and
38 // resumes loading at the position past the plugin that it just attempted to
39 // load, bypassing the problematic plugin.
40 // 5. This algorithm continues until the canonical list has been walked to the
41 // end, after which the list of loaded plugins is set on the PluginList and
42 // the completion callback is run.
43 class CONTENT_EXPORT PluginLoaderPosix
44 : public NON_EXPORTED_BASE(UtilityProcessHostClient),
45 public IPC::Sender {
46 public:
47 PluginLoaderPosix();
49 // Must be called from the IO thread. The |callback| will be called on the IO
50 // thread too.
51 void GetPlugins(const PluginService::GetPluginsCallback& callback);
53 // UtilityProcessHostClient:
54 void OnProcessCrashed(int exit_code) override;
55 void OnProcessLaunchFailed() override;
56 bool OnMessageReceived(const IPC::Message& message) override;
58 // IPC::Sender:
59 bool Send(IPC::Message* msg) override;
61 private:
62 ~PluginLoaderPosix() override;
64 // Called on the FILE thread to get the list of plugin paths to probe.
65 void GetPluginsToLoad();
67 // Must be called on the IO thread.
68 virtual void LoadPluginsInternal();
70 // Called after plugin loading has finished, if we don't know whether the
71 // plugin list has been invalidated in the mean time.
72 void GetPluginsWrapper(
73 const PluginService::GetPluginsCallback& callback,
74 const std::vector<WebPluginInfo>& plugins_unused);
76 // Message handlers.
77 void OnPluginLoaded(uint32 index, const WebPluginInfo& plugin);
78 void OnPluginLoadFailed(uint32 index, const base::FilePath& plugin_path);
80 // Returns an iterator to the plugin in |internal_plugins_| whose path
81 // matches |plugin_path|.
82 std::vector<WebPluginInfo>::iterator FindInternalPlugin(
83 const base::FilePath& plugin_path);
85 // Runs all the registered callbacks on each's target loop if the condition
86 // for ending the load process is done (i.e. the |next_load_index_| is outside
87 // the range of the |canonical_list_|).
88 bool MaybeRunPendingCallbacks();
90 // Returns true if there are no plugins left to load.
91 bool IsFinishedLoadingPlugins();
93 // This method should be called when the plugins are finished loading.
94 // It updates the PluginList's list of plugins, and runs the queued callbacks.
95 void FinishedLoadingPlugins();
97 // Launches the utility process that loads the plugins.
98 // Virtual for testing.
99 virtual bool LaunchUtilityProcess();
101 // The process host for which this is a client.
102 base::WeakPtr<UtilityProcessHost> process_host_;
104 // A list of paths to plugins which will be loaded by the utility process, in
105 // the order specified by this vector.
106 std::vector<base::FilePath> canonical_list_;
108 // The index in |canonical_list_| of the plugin that the child process will
109 // attempt to load next.
110 size_t next_load_index_;
112 // Internal plugins that have been registered at the time of loading.
113 std::vector<WebPluginInfo> internal_plugins_;
115 // A vector of plugins that have been loaded successfully.
116 std::vector<WebPluginInfo> loaded_plugins_;
118 // The callback and message loop on which the callback will be run when the
119 // plugin loading process has been completed.
120 std::vector<PluginService::GetPluginsCallback> callbacks_;
122 // True if there is (or is about to be) a utility process that loads plugins.
123 bool loading_plugins_;
125 friend class MockPluginLoaderPosix;
126 DISALLOW_COPY_AND_ASSIGN(PluginLoaderPosix);
129 } // namespace content
131 #endif // CONTENT_BROWSER_PLUGIN_LOADER_POSIX_H_