blob: 87812f9b3b8d0e898a3ca8a6c029110012b7133d [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"
[email protected]91d9f3d2011-08-14 05:24:4429#include "webkit/plugins/webplugininfo.h"
[email protected]a0421732011-02-23 03:55:4030
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
[email protected]e63c4d72011-05-31 22:38:2936#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]493c8002011-04-14 16:56:0137#include "base/files/file_path_watcher.h"
[email protected]a0421732011-02-23 03:55:4038#endif
39
[email protected]a0421732011-02-23 03:55:4040struct PepperPluginInfo;
41class PluginDirWatcherDelegate;
[email protected]a0421732011-02-23 03:55:4042
43// This must be created on the main thread but it's only called on the IO/file
44// thread.
45class PluginService
46 : public base::WaitableEventWatcher::Delegate,
47 public NotificationObserver {
48 public:
49 struct OverriddenPlugin {
50 int render_process_id;
51 int render_view_id;
52 GURL url;
[email protected]91d9f3d2011-08-14 05:24:4453 webkit::WebPluginInfo plugin;
[email protected]a0421732011-02-23 03:55:4054 };
55
[email protected]a0421732011-02-23 03:55:4056 // Returns the PluginService singleton.
57 static PluginService* GetInstance();
58
[email protected]a0421732011-02-23 03:55:4059 // Gets the browser's UI locale.
60 const std::string& GetUILocale();
61
62 // Returns the plugin process host corresponding to the plugin process that
63 // has been started by this service. Returns NULL if no process has been
64 // started.
65 PluginProcessHost* FindNpapiPluginProcess(const FilePath& plugin_path);
66 PpapiPluginProcessHost* FindPpapiPluginProcess(const FilePath& plugin_path);
[email protected]eb415bf0e2011-04-14 02:45:4267 PpapiBrokerProcessHost* FindPpapiBrokerProcess(const FilePath& broker_path);
[email protected]a0421732011-02-23 03:55:4068
69 // Returns the plugin process host corresponding to the plugin process that
70 // has been started by this service. This will start a process to host the
71 // 'plugin_path' if needed. If the process fails to start, the return value
72 // is NULL. Must be called on the IO thread.
73 PluginProcessHost* FindOrStartNpapiPluginProcess(
74 const FilePath& plugin_path);
75 PpapiPluginProcessHost* FindOrStartPpapiPluginProcess(
[email protected]d259a8e2011-05-18 22:31:0976 const FilePath& plugin_path,
77 PpapiPluginProcessHost::Client* client);
[email protected]eb415bf0e2011-04-14 02:45:4278 PpapiBrokerProcessHost* FindOrStartPpapiBrokerProcess(
79 const FilePath& plugin_path);
[email protected]a0421732011-02-23 03:55:4080
81 // Opens a channel to a plugin process for the given mime type, starting
82 // a new plugin process if necessary. This must be called on the IO thread
83 // or else a deadlock can occur.
84 void OpenChannelToNpapiPlugin(int render_process_id,
85 int render_view_id,
86 const GURL& url,
87 const std::string& mime_type,
88 PluginProcessHost::Client* client);
89 void OpenChannelToPpapiPlugin(const FilePath& path,
90 PpapiPluginProcessHost::Client* client);
[email protected]eb415bf0e2011-04-14 02:45:4291 void OpenChannelToPpapiBroker(const FilePath& path,
92 PpapiBrokerProcessHost::Client* client);
[email protected]a0421732011-02-23 03:55:4093
[email protected]11e1c182011-05-17 20:26:2794 // Gets the plugin in the list of plugins that matches the given url and mime
95 // type. Must be called on the FILE thread.
96 bool GetPluginInfo(int render_process_id,
97 int render_view_id,
98 const GURL& url,
99 const std::string& mime_type,
[email protected]91d9f3d2011-08-14 05:24:44100 webkit::WebPluginInfo* info,
[email protected]11e1c182011-05-17 20:26:27101 std::string* actual_mime_type);
[email protected]a0421732011-02-23 03:55:40102
[email protected]a0421732011-02-23 03:55:40103 // Safe to be called from any thread.
[email protected]bd5174d2011-03-11 01:02:56104 void OverridePluginForTab(const OverriddenPlugin& plugin);
[email protected]a0421732011-02-23 03:55:40105
[email protected]2de307592011-04-05 21:16:58106 // Restricts the given plugin to the the scheme and host of the given url.
107 // Call with an empty url to reset this.
108 // Can be called on any thread.
109 void RestrictPluginToUrl(const FilePath& plugin_path, const GURL& url);
110
111 // Returns true if the given plugin is allowed to be used by a page with
112 // the given URL.
113 // Can be called on any thread.
114 bool PluginAllowedForURL(const FilePath& plugin_path, const GURL& url);
115
116 // Tells all the renderer processes to throw away their cache of the plugin
117 // list, and optionally also reload all the pages with plugins.
118 // NOTE: can only be called on the UI thread.
119 static void PurgePluginListCache(bool reload_pages);
120
[email protected]a0421732011-02-23 03:55:40121 private:
122 friend struct DefaultSingletonTraits<PluginService>;
123
124 // Creates the PluginService object, but doesn't actually build the plugin
125 // list yet. It's generated lazily.
126 PluginService();
[email protected]3690ebe02011-05-25 09:08:19127 virtual ~PluginService();
[email protected]a0421732011-02-23 03:55:40128
129 // base::WaitableEventWatcher::Delegate implementation.
130 virtual void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
131
132 // NotificationObserver implementation
[email protected]432115822011-07-10 15:52:27133 virtual void Observe(int type, const NotificationSource& source,
[email protected]a0421732011-02-23 03:55:40134 const NotificationDetails& details);
135
136 void RegisterPepperPlugins();
137
[email protected]eb415bf0e2011-04-14 02:45:42138 PepperPluginInfo* GetRegisteredPpapiPluginInfo(const FilePath& plugin_path);
139
[email protected]a0421732011-02-23 03:55:40140 // Helper so we can do the plugin lookup on the FILE thread.
141 void GetAllowedPluginForOpenChannelToPlugin(
142 int render_process_id,
143 int render_view_id,
144 const GURL& url,
145 const std::string& mime_type,
146 PluginProcessHost::Client* client);
147
148 // Helper so we can finish opening the channel after looking up the
149 // plugin.
150 void FinishOpenChannelToPlugin(
151 const FilePath& plugin_path,
152 PluginProcessHost::Client* client);
153
[email protected]e63c4d72011-05-31 22:38:29154#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]a0421732011-02-23 03:55:40155 // Registers a new FilePathWatcher for a given path.
156 static void RegisterFilePathWatcher(
[email protected]493c8002011-04-14 16:56:01157 base::files::FilePathWatcher* watcher,
[email protected]a0421732011-02-23 03:55:40158 const FilePath& path,
[email protected]493c8002011-04-14 16:56:01159 base::files::FilePathWatcher::Delegate* delegate);
[email protected]a0421732011-02-23 03:55:40160#endif
161
[email protected]a0421732011-02-23 03:55:40162 // The browser's UI locale.
163 const std::string ui_locale_;
164
[email protected]2de307592011-04-05 21:16:58165 // Map of plugin paths to the origin they are restricted to.
166 base::Lock restricted_plugin_lock_; // Guards access to restricted_plugin_.
167 typedef base::hash_map<FilePath, GURL> RestrictedPluginMap;
168 RestrictedPluginMap restricted_plugin_;
[email protected]a0421732011-02-23 03:55:40169
170 NotificationRegistrar registrar_;
171
[email protected]a0421732011-02-23 03:55:40172#if defined(OS_WIN)
173 // Registry keys for getting notifications when new plugins are installed.
174 base::win::RegKey hkcu_key_;
175 base::win::RegKey hklm_key_;
176 scoped_ptr<base::WaitableEvent> hkcu_event_;
177 scoped_ptr<base::WaitableEvent> hklm_event_;
178 base::WaitableEventWatcher hkcu_watcher_;
179 base::WaitableEventWatcher hklm_watcher_;
180#endif
181
[email protected]e63c4d72011-05-31 22:38:29182#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]493c8002011-04-14 16:56:01183 ScopedVector<base::files::FilePathWatcher> file_watchers_;
[email protected]a0421732011-02-23 03:55:40184 scoped_refptr<PluginDirWatcherDelegate> file_watcher_delegate_;
185#endif
186
187 std::vector<PepperPluginInfo> ppapi_plugins_;
188
[email protected]a0421732011-02-23 03:55:40189 std::vector<OverriddenPlugin> overridden_plugins_;
190 base::Lock overridden_plugins_lock_;
191
192 DISALLOW_COPY_AND_ASSIGN(PluginService);
193};
194
195DISABLE_RUNNABLE_METHOD_REFCOUNT(PluginService);
196
197#endif // CONTENT_BROWSER_PLUGIN_SERVICE_H_