blob: 5c37c011347463c128a69d53f693fe320dc95e1c [file] [log] [blame]
[email protected]a0421732011-02-23 03:55:401// 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// This class responds to requests from renderers for the list of plugins, and
6// also a proxy object for plugin instances.
7
8#ifndef CONTENT_BROWSER_PLUGIN_SERVICE_H_
9#define CONTENT_BROWSER_PLUGIN_SERVICE_H_
10#pragma once
11
12#include <string>
13
14#include "base/basictypes.h"
15#include "base/file_path.h"
16#include "base/hash_tables.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/scoped_vector.h"
18#include "base/memory/singleton.h"
[email protected]a0421732011-02-23 03:55:4019#include "base/synchronization/lock.h"
20#include "base/synchronization/waitable_event_watcher.h"
21#include "build/build_config.h"
[email protected]a0421732011-02-23 03:55:4022#include "content/browser/plugin_process_host.h"
23#include "content/browser/ppapi_plugin_process_host.h"
[email protected]eb415bf0e2011-04-14 02:45:4224#include "content/browser/ppapi_broker_process_host.h"
[email protected]7f070d42011-03-09 20:25:3225#include "content/common/notification_observer.h"
26#include "content/common/notification_registrar.h"
[email protected]a0421732011-02-23 03:55:4027#include "googleurl/src/gurl.h"
28#include "ipc/ipc_channel_handle.h"
29#include "webkit/plugins/npapi/webplugininfo.h"
30
31#if defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1532#include "base/memory/scoped_ptr.h"
[email protected]a0421732011-02-23 03:55:4033#include "base/win/registry.h"
34#endif
35
36#if defined(OS_LINUX)
[email protected]b8ca91f2011-03-18 04:11:4937#include "content/common/file_path_watcher/file_path_watcher.h"
[email protected]a0421732011-02-23 03:55:4038#endif
39
40#if defined(OS_CHROMEOS)
41namespace chromeos {
42class PluginSelectionPolicy;
43}
44#endif
45
46namespace IPC {
47class Message;
48}
49
50class MessageLoop;
51struct PepperPluginInfo;
52class PluginDirWatcherDelegate;
53class Profile;
54class ResourceDispatcherHost;
55
56namespace net {
57class URLRequestContext;
58} // namespace net
59
60// This must be created on the main thread but it's only called on the IO/file
61// thread.
62class PluginService
63 : public base::WaitableEventWatcher::Delegate,
64 public NotificationObserver {
65 public:
66 struct OverriddenPlugin {
67 int render_process_id;
68 int render_view_id;
69 GURL url;
70 webkit::npapi::WebPluginInfo plugin;
71 };
72
73 // Initializes the global instance; should be called on startup from the main
74 // thread.
75 static void InitGlobalInstance(Profile* profile);
76
77 // Returns the PluginService singleton.
78 static PluginService* GetInstance();
79
[email protected]a0421732011-02-23 03:55:4080 // Gets the browser's UI locale.
81 const std::string& GetUILocale();
82
83 // Returns the plugin process host corresponding to the plugin process that
84 // has been started by this service. Returns NULL if no process has been
85 // started.
86 PluginProcessHost* FindNpapiPluginProcess(const FilePath& plugin_path);
87 PpapiPluginProcessHost* FindPpapiPluginProcess(const FilePath& plugin_path);
[email protected]eb415bf0e2011-04-14 02:45:4288 PpapiBrokerProcessHost* FindPpapiBrokerProcess(const FilePath& broker_path);
[email protected]a0421732011-02-23 03:55:4089
90 // Returns the plugin process host corresponding to the plugin process that
91 // has been started by this service. This will start a process to host the
92 // 'plugin_path' if needed. If the process fails to start, the return value
93 // is NULL. Must be called on the IO thread.
94 PluginProcessHost* FindOrStartNpapiPluginProcess(
95 const FilePath& plugin_path);
96 PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
97 const FilePath& plugin_path);
[email protected]eb415bf0e2011-04-14 02:45:4298 PpapiBrokerProcessHost* FindOrStartPpapiBrokerProcess(
99 const FilePath& plugin_path);
[email protected]a0421732011-02-23 03:55:40100
101 // Opens a channel to a plugin process for the given mime type, starting
102 // a new plugin process if necessary. This must be called on the IO thread
103 // or else a deadlock can occur.
104 void OpenChannelToNpapiPlugin(int render_process_id,
105 int render_view_id,
106 const GURL& url,
107 const std::string& mime_type,
108 PluginProcessHost::Client* client);
109 void OpenChannelToPpapiPlugin(const FilePath& path,
110 PpapiPluginProcessHost::Client* client);
[email protected]eb415bf0e2011-04-14 02:45:42111 void OpenChannelToPpapiBroker(const FilePath& path,
112 PpapiBrokerProcessHost::Client* client);
[email protected]a0421732011-02-23 03:55:40113
114 // Gets the first allowed plugin in the list of plugins that matches
115 // the given url and mime type. Must be called on the FILE thread.
116 bool GetFirstAllowedPluginInfo(int render_process_id,
117 int render_view_id,
118 const GURL& url,
119 const std::string& mime_type,
120 webkit::npapi::WebPluginInfo* info,
121 std::string* actual_mime_type);
122
[email protected]a0421732011-02-23 03:55:40123 // Safe to be called from any thread.
[email protected]bd5174d2011-03-11 01:02:56124 void OverridePluginForTab(const OverriddenPlugin& plugin);
[email protected]a0421732011-02-23 03:55:40125
[email protected]2de307592011-04-05 21:16:58126 // Restricts the given plugin to the the scheme and host of the given url.
127 // Call with an empty url to reset this.
128 // Can be called on any thread.
129 void RestrictPluginToUrl(const FilePath& plugin_path, const GURL& url);
130
131 // Returns true if the given plugin is allowed to be used by a page with
132 // the given URL.
133 // Can be called on any thread.
134 bool PluginAllowedForURL(const FilePath& plugin_path, const GURL& url);
135
136 // Tells all the renderer processes to throw away their cache of the plugin
137 // list, and optionally also reload all the pages with plugins.
138 // NOTE: can only be called on the UI thread.
139 static void PurgePluginListCache(bool reload_pages);
140
[email protected]a0421732011-02-23 03:55:40141 // The UI thread's message loop
142 MessageLoop* main_message_loop() { return main_message_loop_; }
143
[email protected]a0421732011-02-23 03:55:40144 private:
145 friend struct DefaultSingletonTraits<PluginService>;
146
147 // Creates the PluginService object, but doesn't actually build the plugin
148 // list yet. It's generated lazily.
149 PluginService();
150 ~PluginService();
151
152 // base::WaitableEventWatcher::Delegate implementation.
153 virtual void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
154
155 // NotificationObserver implementation
156 virtual void Observe(NotificationType type, const NotificationSource& source,
157 const NotificationDetails& details);
158
159 void RegisterPepperPlugins();
160
[email protected]eb415bf0e2011-04-14 02:45:42161 PepperPluginInfo* GetRegisteredPpapiPluginInfo(const FilePath& plugin_path);
162
[email protected]a0421732011-02-23 03:55:40163 // Helper so we can do the plugin lookup on the FILE thread.
164 void GetAllowedPluginForOpenChannelToPlugin(
165 int render_process_id,
166 int render_view_id,
167 const GURL& url,
168 const std::string& mime_type,
169 PluginProcessHost::Client* client);
170
171 // Helper so we can finish opening the channel after looking up the
172 // plugin.
173 void FinishOpenChannelToPlugin(
174 const FilePath& plugin_path,
175 PluginProcessHost::Client* client);
176
177#if defined(OS_LINUX)
178 // Registers a new FilePathWatcher for a given path.
179 static void RegisterFilePathWatcher(
180 FilePathWatcher* watcher,
181 const FilePath& path,
182 FilePathWatcher::Delegate* delegate);
183#endif
184
185 // The main thread's message loop.
186 MessageLoop* main_message_loop_;
187
[email protected]a0421732011-02-23 03:55:40188 // The browser's UI locale.
189 const std::string ui_locale_;
190
[email protected]2de307592011-04-05 21:16:58191 // Map of plugin paths to the origin they are restricted to.
192 base::Lock restricted_plugin_lock_; // Guards access to restricted_plugin_.
193 typedef base::hash_map<FilePath, GURL> RestrictedPluginMap;
194 RestrictedPluginMap restricted_plugin_;
[email protected]a0421732011-02-23 03:55:40195
196 NotificationRegistrar registrar_;
197
198#if defined(OS_CHROMEOS)
199 scoped_refptr<chromeos::PluginSelectionPolicy> plugin_selection_policy_;
200#endif
201
202#if defined(OS_WIN)
203 // Registry keys for getting notifications when new plugins are installed.
204 base::win::RegKey hkcu_key_;
205 base::win::RegKey hklm_key_;
206 scoped_ptr<base::WaitableEvent> hkcu_event_;
207 scoped_ptr<base::WaitableEvent> hklm_event_;
208 base::WaitableEventWatcher hkcu_watcher_;
209 base::WaitableEventWatcher hklm_watcher_;
210#endif
211
212#if defined(OS_LINUX)
213 ScopedVector<FilePathWatcher> file_watchers_;
214 scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_;
215#endif
216
217 std::vector<PepperPluginInfo> ppapi_plugins_;
218
[email protected]a0421732011-02-23 03:55:40219 std::vector<OverriddenPlugin> overridden_plugins_;
220 base::Lock overridden_plugins_lock_;
221
222 DISALLOW_COPY_AND_ASSIGN(PluginService);
223};
224
225DISABLE_RUNNABLE_METHOD_REFCOUNT(PluginService);
226
227#endif // CONTENT_BROWSER_PLUGIN_SERVICE_H_