OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/plugins/plugin_info_message_filter.h" | 5 #include "chrome/browser/plugins/plugin_info_message_filter.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
15 #include "chrome/browser/plugins/plugin_utils.h" | 15 #include "chrome/browser/plugins/plugin_utils.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "chrome/common/render_messages.h" | 17 #include "chrome/common/render_messages.h" |
18 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
19 #include "components/content_settings/core/browser/host_content_settings_map.h" | 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
20 #include "components/content_settings/core/common/pref_names.h" | 20 #include "components/content_settings/core/common/pref_names.h" |
21 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 21 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
22 #include "content/public/browser/plugin_service.h" | 22 #include "content/public/browser/plugin_service.h" |
23 #include "content/public/browser/plugin_service_filter.h" | 23 #include "content/public/browser/plugin_service_filter.h" |
24 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/common/content_constants.h" | 25 #include "content/public/common/content_constants.h" |
26 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "url/origin.h" |
29 | 30 |
30 using content::PluginService; | 31 using content::PluginService; |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 void PluginsLoaded(const base::Closure& callback, | 35 void PluginsLoaded(const base::Closure& callback, |
35 const std::vector<content::WebPluginInfo>& plugins) { | 36 const std::vector<content::WebPluginInfo>& plugins) { |
36 callback.Run(); | 37 callback.Run(); |
37 } | 38 } |
38 | 39 |
39 class FakePluginServiceFilter : public content::PluginServiceFilter { | 40 class FakePluginServiceFilter : public content::PluginServiceFilter { |
40 public: | 41 public: |
41 FakePluginServiceFilter() {} | 42 FakePluginServiceFilter() {} |
42 ~FakePluginServiceFilter() override {} | 43 ~FakePluginServiceFilter() override {} |
43 | 44 |
44 bool IsPluginAvailable(int render_process_id, | 45 bool IsPluginAvailable(int render_process_id, |
45 int render_view_id, | 46 int render_view_id, |
46 const void* context, | 47 const void* context, |
47 const GURL& url, | 48 const GURL& url, |
48 const GURL& policy_url, | 49 const url::Origin& main_frame_origin, |
49 content::WebPluginInfo* plugin) override; | 50 content::WebPluginInfo* plugin) override; |
50 | 51 |
51 bool CanLoadPlugin(int render_process_id, | 52 bool CanLoadPlugin(int render_process_id, |
52 const base::FilePath& path) override; | 53 const base::FilePath& path) override; |
53 | 54 |
54 void set_plugin_enabled(const base::FilePath& plugin_path, bool enabled) { | 55 void set_plugin_enabled(const base::FilePath& plugin_path, bool enabled) { |
55 plugin_state_[plugin_path] = enabled; | 56 plugin_state_[plugin_path] = enabled; |
56 } | 57 } |
57 | 58 |
58 private: | 59 private: |
59 std::map<base::FilePath, bool> plugin_state_; | 60 std::map<base::FilePath, bool> plugin_state_; |
60 }; | 61 }; |
61 | 62 |
62 bool FakePluginServiceFilter::IsPluginAvailable( | 63 bool FakePluginServiceFilter::IsPluginAvailable( |
63 int render_process_id, | 64 int render_process_id, |
64 int render_view_id, | 65 int render_view_id, |
65 const void* context, | 66 const void* context, |
66 const GURL& url, | 67 const GURL& url, |
67 const GURL& policy_url, | 68 const url::Origin& main_frame_origin, |
68 content::WebPluginInfo* plugin) { | 69 content::WebPluginInfo* plugin) { |
69 std::map<base::FilePath, bool>::iterator it = | 70 std::map<base::FilePath, bool>::iterator it = |
70 plugin_state_.find(plugin->path); | 71 plugin_state_.find(plugin->path); |
71 if (it == plugin_state_.end()) { | 72 if (it == plugin_state_.end()) { |
72 ADD_FAILURE() << "No plugin state for '" << plugin->path.value() << "'"; | 73 ADD_FAILURE() << "No plugin state for '" << plugin->path.value() << "'"; |
73 return false; | 74 return false; |
74 } | 75 } |
75 return it->second; | 76 return it->second; |
76 } | 77 } |
77 | 78 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 ContentSetting setting = expected_setting == CONTENT_SETTING_DEFAULT ? | 145 ContentSetting setting = expected_setting == CONTENT_SETTING_DEFAULT ? |
145 CONTENT_SETTING_BLOCK : CONTENT_SETTING_DEFAULT; | 146 CONTENT_SETTING_BLOCK : CONTENT_SETTING_DEFAULT; |
146 bool is_default = !expected_is_default; | 147 bool is_default = !expected_is_default; |
147 bool is_managed = !expected_is_managed; | 148 bool is_managed = !expected_is_managed; |
148 | 149 |
149 // Pass in a fake Flash plugin info. | 150 // Pass in a fake Flash plugin info. |
150 content::WebPluginInfo plugin_info( | 151 content::WebPluginInfo plugin_info( |
151 base::ASCIIToUTF16(content::kFlashPluginName), base::FilePath(), | 152 base::ASCIIToUTF16(content::kFlashPluginName), base::FilePath(), |
152 base::ASCIIToUTF16("1"), base::ASCIIToUTF16("Fake Flash")); | 153 base::ASCIIToUTF16("1"), base::ASCIIToUTF16("Fake Flash")); |
153 | 154 |
154 PluginUtils::GetPluginContentSetting(host_content_settings_map_, | 155 PluginUtils::GetPluginContentSetting( |
155 plugin_info, url, url, plugin, | 156 host_content_settings_map_, plugin_info, url::Origin(url), url, plugin, |
156 &setting, &is_default, &is_managed); | 157 &setting, &is_default, &is_managed); |
157 EXPECT_EQ(expected_setting, setting); | 158 EXPECT_EQ(expected_setting, setting); |
158 EXPECT_EQ(expected_is_default, is_default); | 159 EXPECT_EQ(expected_is_default, is_default); |
159 EXPECT_EQ(expected_is_managed, is_managed); | 160 EXPECT_EQ(expected_is_managed, is_managed); |
160 } | 161 } |
161 | 162 |
162 base::FilePath foo_plugin_path_; | 163 base::FilePath foo_plugin_path_; |
163 base::FilePath bar_plugin_path_; | 164 base::FilePath bar_plugin_path_; |
164 FakePluginServiceFilter filter_; | 165 FakePluginServiceFilter filter_; |
165 | 166 |
166 private: | 167 private: |
167 base::ShadowingAtExitManager at_exit_manager_; // Destroys the PluginService. | 168 base::ShadowingAtExitManager at_exit_manager_; // Destroys the PluginService. |
168 content::TestBrowserThreadBundle test_thread_bundle; | 169 content::TestBrowserThreadBundle test_thread_bundle; |
169 TestingProfile profile_; | 170 TestingProfile profile_; |
170 PluginInfoMessageFilter::Context context_; | 171 PluginInfoMessageFilter::Context context_; |
171 HostContentSettingsMap* host_content_settings_map_; | 172 HostContentSettingsMap* host_content_settings_map_; |
172 }; | 173 }; |
173 | 174 |
174 TEST_F(PluginInfoMessageFilterTest, FindEnabledPlugin) { | 175 TEST_F(PluginInfoMessageFilterTest, FindEnabledPlugin) { |
175 filter_.set_plugin_enabled(foo_plugin_path_, true); | 176 filter_.set_plugin_enabled(foo_plugin_path_, true); |
176 filter_.set_plugin_enabled(bar_plugin_path_, true); | 177 filter_.set_plugin_enabled(bar_plugin_path_, true); |
177 { | 178 { |
178 ChromeViewHostMsg_GetPluginInfo_Status status; | 179 ChromeViewHostMsg_GetPluginInfo_Status status; |
179 content::WebPluginInfo plugin; | 180 content::WebPluginInfo plugin; |
180 std::string actual_mime_type; | 181 std::string actual_mime_type; |
181 EXPECT_TRUE(context()->FindEnabledPlugin( | 182 EXPECT_TRUE(context()->FindEnabledPlugin(0, GURL(), url::Origin(), |
182 0, GURL(), GURL(), "foo/bar", &status, &plugin, &actual_mime_type, | 183 "foo/bar", &status, &plugin, |
183 NULL)); | 184 &actual_mime_type, NULL)); |
184 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kAllowed, status); | 185 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kAllowed, status); |
185 EXPECT_EQ(foo_plugin_path_.value(), plugin.path.value()); | 186 EXPECT_EQ(foo_plugin_path_.value(), plugin.path.value()); |
186 } | 187 } |
187 | 188 |
188 filter_.set_plugin_enabled(foo_plugin_path_, false); | 189 filter_.set_plugin_enabled(foo_plugin_path_, false); |
189 { | 190 { |
190 ChromeViewHostMsg_GetPluginInfo_Status status; | 191 ChromeViewHostMsg_GetPluginInfo_Status status; |
191 content::WebPluginInfo plugin; | 192 content::WebPluginInfo plugin; |
192 std::string actual_mime_type; | 193 std::string actual_mime_type; |
193 EXPECT_TRUE(context()->FindEnabledPlugin( | 194 EXPECT_TRUE(context()->FindEnabledPlugin(0, GURL(), url::Origin(), |
194 0, GURL(), GURL(), "foo/bar", &status, &plugin, &actual_mime_type, | 195 "foo/bar", &status, &plugin, |
195 NULL)); | 196 &actual_mime_type, NULL)); |
196 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kAllowed, status); | 197 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kAllowed, status); |
197 EXPECT_EQ(bar_plugin_path_.value(), plugin.path.value()); | 198 EXPECT_EQ(bar_plugin_path_.value(), plugin.path.value()); |
198 } | 199 } |
199 | 200 |
200 filter_.set_plugin_enabled(bar_plugin_path_, false); | 201 filter_.set_plugin_enabled(bar_plugin_path_, false); |
201 { | 202 { |
202 ChromeViewHostMsg_GetPluginInfo_Status status; | 203 ChromeViewHostMsg_GetPluginInfo_Status status; |
203 content::WebPluginInfo plugin; | 204 content::WebPluginInfo plugin; |
204 std::string actual_mime_type; | 205 std::string actual_mime_type; |
205 std::string identifier; | 206 std::string identifier; |
206 base::string16 plugin_name; | 207 base::string16 plugin_name; |
207 EXPECT_FALSE(context()->FindEnabledPlugin( | 208 EXPECT_FALSE(context()->FindEnabledPlugin(0, GURL(), url::Origin(), |
208 0, GURL(), GURL(), "foo/bar", &status, &plugin, &actual_mime_type, | 209 "foo/bar", &status, &plugin, |
209 NULL)); | 210 &actual_mime_type, NULL)); |
210 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kDisabled, status); | 211 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kDisabled, status); |
211 EXPECT_EQ(foo_plugin_path_.value(), plugin.path.value()); | 212 EXPECT_EQ(foo_plugin_path_.value(), plugin.path.value()); |
212 } | 213 } |
213 { | 214 { |
214 ChromeViewHostMsg_GetPluginInfo_Status status; | 215 ChromeViewHostMsg_GetPluginInfo_Status status; |
215 content::WebPluginInfo plugin; | 216 content::WebPluginInfo plugin; |
216 std::string actual_mime_type; | 217 std::string actual_mime_type; |
217 EXPECT_FALSE(context()->FindEnabledPlugin( | 218 EXPECT_FALSE(context()->FindEnabledPlugin(0, GURL(), url::Origin(), |
218 0, GURL(), GURL(), "baz/blurp", &status, &plugin, &actual_mime_type, | 219 "baz/blurp", &status, &plugin, |
219 NULL)); | 220 &actual_mime_type, NULL)); |
220 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kNotFound, status); | 221 EXPECT_EQ(ChromeViewHostMsg_GetPluginInfo_Status::kNotFound, status); |
221 EXPECT_EQ(FILE_PATH_LITERAL(""), plugin.path.value()); | 222 EXPECT_EQ(FILE_PATH_LITERAL(""), plugin.path.value()); |
222 } | 223 } |
223 } | 224 } |
224 | 225 |
225 TEST_F(PluginInfoMessageFilterTest, GetPluginContentSetting) { | 226 TEST_F(PluginInfoMessageFilterTest, GetPluginContentSetting) { |
226 HostContentSettingsMap* map = | 227 HostContentSettingsMap* map = |
227 HostContentSettingsMapFactory::GetForProfile(profile()); | 228 HostContentSettingsMapFactory::GetForProfile(profile()); |
228 | 229 |
229 // Block plugins by default. | 230 // Block plugins by default. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 profile()->GetTestingPrefService(); | 271 profile()->GetTestingPrefService(); |
271 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, | 272 prefs->SetManagedPref(prefs::kManagedDefaultPluginsSetting, |
272 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | 273 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); |
273 | 274 |
274 // All plugins should be blocked now. | 275 // All plugins should be blocked now. |
275 VerifyPluginContentSetting(host, "foo", CONTENT_SETTING_BLOCK, true, true); | 276 VerifyPluginContentSetting(host, "foo", CONTENT_SETTING_BLOCK, true, true); |
276 VerifyPluginContentSetting(host, "bar", CONTENT_SETTING_BLOCK, true, true); | 277 VerifyPluginContentSetting(host, "bar", CONTENT_SETTING_BLOCK, true, true); |
277 VerifyPluginContentSetting(unmatched_host, "bar", CONTENT_SETTING_BLOCK, | 278 VerifyPluginContentSetting(unmatched_host, "bar", CONTENT_SETTING_BLOCK, |
278 true, true); | 279 true, true); |
279 } | 280 } |
OLD | NEW |