OLD | NEW |
---|---|
(Empty) | |
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 #include "chrome/browser/chrome_plugin_service_helper.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "chrome/common/chrome_notification_types.h" | |
9 #include "content/browser/browser_thread.h" | |
10 #include "content/browser/plugin_filter.h" | |
11 #include "content/browser/renderer_host/render_process_host.h" | |
12 #include "content/browser/resource_context.h" | |
13 #include "content/common/notification_service.h" | |
14 #include "webkit/plugins/npapi/plugin_list.h" | |
15 #include "webkit/plugins/npapi/webplugininfo.h" | |
16 | |
17 namespace { | |
18 | |
19 class ChromePluginFilter : public PluginFilter { | |
20 public: | |
21 ChromePluginFilter(int render_process_id, | |
22 int render_view_id, | |
23 const content::ResourceContext& context, | |
24 const GURL& url, | |
25 const GURL& policy_url); | |
26 virtual ~ChromePluginFilter(); | |
27 | |
28 // PluginService::Filter implementation: | |
29 virtual bool ShouldUsePlugin(webkit::npapi::WebPluginInfo* plugin) OVERRIDE; | |
30 | |
31 private: | |
32 GURL policy_url_; | |
33 | |
34 scoped_ptr<webkit::npapi::WebPluginInfo> overridden_plugin_; | |
35 bool use_default_plugin_; | |
36 }; | |
37 | |
38 ChromePluginFilter::ChromePluginFilter( | |
39 int render_process_id, | |
40 int render_view_id, | |
41 const content::ResourceContext& resource_context, | |
42 const GURL& url, | |
43 const GURL& policy_url) | |
44 : policy_url_(policy_url), | |
45 use_default_plugin_(true) { | |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
47 ChromePluginServiceHelper* helper = ChromePluginServiceHelper::GetInstance(); | |
48 overridden_plugin_.reset(helper->FindOverriddenPluginForTab( | |
49 render_process_id, render_view_id, url)); | |
50 } | |
51 | |
52 ChromePluginFilter::~ChromePluginFilter() { | |
53 } | |
54 | |
55 bool ChromePluginFilter::ShouldUsePlugin(webkit::npapi::WebPluginInfo* plugin) { | |
56 // Check whether the plugin is overridden. | |
57 if (overridden_plugin_.get()) { | |
58 if (overridden_plugin_->path != plugin->path) | |
59 return false; | |
60 *plugin = *overridden_plugin_; | |
jam
2011/08/02 04:18:18
why is this line needed? i.e. under what condition
Bernhard Bauer
2011/08/02 12:45:08
When we want to use an out-of-date version of Read
| |
61 return true; | |
62 } | |
63 | |
64 // Check whether the plugin is disabled. | |
65 if (!webkit::npapi::IsPluginEnabled(*plugin)) { | |
66 // Don't use the default plug-in to handle a disabled plug-in (because | |
67 // offering to install an already installed plug-in would be confusing to | |
68 // the user). | |
69 // TODO(bauerb): Implement the default plug-in functionality directly | |
jam
2011/08/01 17:25:04
I don't understand this comment? Why do you think
Bernhard Bauer
2011/08/01 18:07:39
Using a plug-in that handles MIME type "*" is a ni
| |
70 // without using a plug-in and get rid of all this hackery. | |
71 use_default_plugin_ = false; | |
72 return false; | |
73 } | |
74 | |
75 if (!use_default_plugin_ && | |
76 plugin->path.value() == webkit::npapi::kDefaultPluginLibraryName) { | |
77 return false; | |
78 } | |
79 | |
80 // Check whether the plugin is restricted to a URL. | |
jam
2011/08/02 04:18:18
btw, now that this code lives in chrome, we can ge
Bernhard Bauer
2011/08/02 12:45:08
Hmm, ExtensionService currently lives on the UI th
| |
81 ChromePluginServiceHelper* helper = ChromePluginServiceHelper::GetInstance(); | |
82 GURL restricted_url = helper->FindRestrictedURLForPlugin(plugin->path); | |
83 if (!restricted_url.is_empty() && | |
84 (policy_url_.scheme() != restricted_url.scheme() || | |
85 policy_url_.host() != restricted_url.host())) { | |
86 return false; | |
87 } | |
88 | |
89 return true; | |
90 } | |
91 | |
92 } // namespace | |
93 | |
94 // static | |
95 ChromePluginServiceHelper* ChromePluginServiceHelper::GetInstance() { | |
96 return Singleton<ChromePluginServiceHelper>::get(); | |
97 } | |
98 | |
99 void ChromePluginServiceHelper::OverridePluginForTab( | |
100 int render_process_id, | |
101 int render_view_id, | |
102 const GURL& url, | |
103 const webkit::npapi::WebPluginInfo& plugin) { | |
104 OverriddenPlugin overridden_plugin; | |
105 overridden_plugin.render_process_id = render_process_id; | |
106 overridden_plugin.render_view_id = render_view_id; | |
107 overridden_plugin.url = url; | |
108 overridden_plugin.plugin = plugin; | |
109 base::AutoLock auto_lock(lock_); | |
110 overridden_plugins_.push_back(overridden_plugin); | |
111 } | |
112 | |
113 webkit::npapi::WebPluginInfo* | |
114 ChromePluginServiceHelper::FindOverriddenPluginForTab( | |
115 int render_process_id, | |
116 int render_view_id, | |
117 const GURL& url) { | |
118 base::AutoLock auto_lock(lock_); | |
119 for (size_t i = 0; i < overridden_plugins_.size(); ++i) { | |
120 if (overridden_plugins_[i].render_process_id == render_process_id && | |
121 overridden_plugins_[i].render_view_id == render_view_id && | |
122 overridden_plugins_[i].url == url) { | |
123 return new webkit::npapi::WebPluginInfo(overridden_plugins_[i].plugin); | |
124 } | |
125 } | |
126 return NULL; | |
127 } | |
128 | |
129 void ChromePluginServiceHelper::RestrictPluginToUrl(const FilePath& plugin_path, | |
130 const GURL& url) { | |
131 base::AutoLock auto_lock(lock_); | |
132 if (url.is_empty()) | |
133 restricted_plugins_.erase(plugin_path); | |
134 else | |
135 restricted_plugins_[plugin_path] = url; | |
136 } | |
137 | |
138 GURL ChromePluginServiceHelper::FindRestrictedURLForPlugin( | |
139 const FilePath& plugin_path) { | |
140 base::AutoLock auto_lock(lock_); | |
141 RestrictedPluginMap::iterator it = restricted_plugins_.find(plugin_path); | |
142 if (it != restricted_plugins_.end()) | |
143 return it->second; | |
144 return GURL(); | |
145 } | |
146 | |
147 PluginFilter* ChromePluginServiceHelper::CreatePluginFilter( | |
148 int render_process_id, | |
149 int render_view_id, | |
150 const content::ResourceContext& resource_context, | |
151 const GURL& url, | |
152 const GURL& policy_url) { | |
153 return new ChromePluginFilter( | |
154 render_process_id, render_view_id, resource_context, url, policy_url); | |
155 } | |
156 | |
157 ChromePluginServiceHelper::ChromePluginServiceHelper() { | |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
159 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
160 NotificationService::AllSources()); | |
161 registrar_.Add(this, chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED, | |
162 NotificationService::AllSources()); | |
163 } | |
164 | |
165 ChromePluginServiceHelper::~ChromePluginServiceHelper() { | |
166 } | |
167 | |
168 void ChromePluginServiceHelper::Observe(int type, | |
169 const NotificationSource& source, | |
170 const NotificationDetails& details) { | |
171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
172 switch (type) { | |
173 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
174 int render_process_id = Source<RenderProcessHost>(source).ptr()->id(); | |
175 | |
176 base::AutoLock auto_lock(lock_); | |
177 for (size_t i = 0; i < overridden_plugins_.size(); ++i) { | |
178 if (overridden_plugins_[i].render_process_id == render_process_id) { | |
179 overridden_plugins_.erase(overridden_plugins_.begin() + i); | |
180 break; | |
181 } | |
182 } | |
183 break; | |
184 } | |
185 case chrome::NOTIFICATION_PLUGIN_ENABLE_STATUS_CHANGED: { | |
186 PluginService::GetInstance()->PurgePluginListCache(false); | |
187 break; | |
188 } | |
189 default: { | |
190 NOTREACHED(); | |
191 } | |
192 } | |
193 } | |
OLD | NEW |