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 #include "base/location.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/content_settings/cookie_settings_factory.h"
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
14 #include "chrome/browser/extensions/api/content_settings/content_settings_api.h"
15 #include "chrome/browser/extensions/extension_apitest.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "components/content_settings/core/browser/cookie_settings.h"
20 #include "components/content_settings/core/browser/host_content_settings_map.h"
21 #include "components/content_settings/core/browser/plugins_field_trial.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/plugin_service.h"
24 #include "content/public/common/webplugininfo.h"
25 #include "content/public/test/test_utils.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/test_extension_registry_observer.h"
31 void ReleaseBrowserProcessModule() {
32 g_browser_process
->ReleaseModule();
35 using content_settings::PluginsFieldTrial
;
39 namespace extensions
{
41 class ExtensionContentSettingsApiTest
: public ExtensionApiTest
{
43 ExtensionContentSettingsApiTest() : profile_(NULL
) {}
45 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
46 ExtensionApiTest::SetUpCommandLine(command_line
);
47 command_line
->AppendSwitch(switches::kDisablePluginsDiscovery
);
50 void SetUpOnMainThread() override
{
51 ExtensionApiTest::SetUpOnMainThread();
53 // The browser might get closed later (and therefore be destroyed), so we
55 profile_
= browser()->profile();
57 // Closing the last browser window also releases a module reference. Make
58 // sure it's not the last one, so the message loop doesn't quit
60 g_browser_process
->AddRefModule();
63 void TearDownOnMainThread() override
{
64 // ReleaseBrowserProcessModule() needs to be called in a message loop, so we
65 // post a task to do it, then run the message loop.
66 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE
, base::Bind(&ReleaseBrowserProcessModule
));
68 content::RunAllPendingInMessageLoop();
70 ExtensionApiTest::TearDownOnMainThread();
74 void CheckContentSettingsSet() {
75 HostContentSettingsMap
* map
=
76 HostContentSettingsMapFactory::GetForProfile(profile_
);
77 content_settings::CookieSettings
* cookie_settings
=
78 CookieSettingsFactory::GetForProfile(profile_
).get();
80 // Check default content settings by using an unknown URL.
81 GURL
example_url("http://www.example.com");
82 EXPECT_TRUE(cookie_settings
->IsReadingCookieAllowed(
83 example_url
, example_url
));
84 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(
85 example_url
, example_url
));
86 EXPECT_TRUE(cookie_settings
->IsCookieSessionOnly(example_url
));
87 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
88 map
->GetContentSetting(example_url
,
90 CONTENT_SETTINGS_TYPE_IMAGES
,
92 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
93 map
->GetContentSetting(example_url
,
95 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
97 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
98 map
->GetContentSetting(example_url
,
100 CONTENT_SETTINGS_TYPE_PLUGINS
,
102 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
103 map
->GetContentSetting(example_url
,
105 CONTENT_SETTINGS_TYPE_POPUPS
,
107 EXPECT_EQ(CONTENT_SETTING_ASK
,
108 map
->GetContentSetting(example_url
,
110 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
112 EXPECT_EQ(CONTENT_SETTING_ASK
,
113 map
->GetContentSetting(example_url
,
115 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
117 EXPECT_EQ(CONTENT_SETTING_ASK
,
118 map
->GetContentSetting(example_url
,
120 CONTENT_SETTINGS_TYPE_FULLSCREEN
,
122 EXPECT_EQ(CONTENT_SETTING_ASK
,
123 map
->GetContentSetting(example_url
,
125 CONTENT_SETTINGS_TYPE_MOUSELOCK
,
127 EXPECT_EQ(CONTENT_SETTING_ASK
,
128 map
->GetContentSetting(example_url
,
130 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
,
132 EXPECT_EQ(CONTENT_SETTING_ASK
,
133 map
->GetContentSetting(example_url
,
135 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
,
137 EXPECT_EQ(CONTENT_SETTING_ASK
,
138 map
->GetContentSetting(example_url
,
140 CONTENT_SETTINGS_TYPE_PPAPI_BROKER
,
142 EXPECT_EQ(CONTENT_SETTING_ASK
,
143 map
->GetContentSetting(example_url
,
145 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
148 // Check content settings for www.google.com
149 GURL
url("http://www.google.com");
150 EXPECT_FALSE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
151 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
152 map
->GetContentSetting(
153 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
154 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
155 map
->GetContentSetting(
156 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
157 EXPECT_EQ(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
,
158 map
->GetContentSetting(
159 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
160 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
161 map
->GetContentSetting(
162 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
163 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
164 map
->GetContentSetting(
165 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
167 CONTENT_SETTING_BLOCK
,
168 map
->GetContentSetting(
169 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
170 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
171 map
->GetContentSetting(
172 url
, url
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
173 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
174 map
->GetContentSetting(
175 url
, url
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
176 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
177 map
->GetContentSetting(
178 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, std::string()));
179 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
180 map
->GetContentSetting(
181 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, std::string()));
182 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
183 map
->GetContentSetting(
184 url
, url
, CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, std::string()));
185 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
186 map
->GetContentSetting(
187 url
, url
, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
191 void CheckContentSettingsDefault() {
192 HostContentSettingsMap
* map
=
193 HostContentSettingsMapFactory::GetForProfile(profile_
);
194 content_settings::CookieSettings
* cookie_settings
=
195 CookieSettingsFactory::GetForProfile(profile_
).get();
197 // Check content settings for www.google.com
198 GURL
url("http://www.google.com");
199 EXPECT_TRUE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
200 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(url
, url
));
201 EXPECT_FALSE(cookie_settings
->IsCookieSessionOnly(url
));
202 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
203 map
->GetContentSetting(
204 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
205 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
206 map
->GetContentSetting(
207 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
208 EXPECT_EQ(PluginsFieldTrial::GetDefaultPluginsContentSetting(),
209 map
->GetContentSetting(
210 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
211 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
212 map
->GetContentSetting(
213 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
214 EXPECT_EQ(CONTENT_SETTING_ASK
,
215 map
->GetContentSetting(
216 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
219 map
->GetContentSetting(
220 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
221 EXPECT_EQ(CONTENT_SETTING_ASK
,
222 map
->GetContentSetting(
223 url
, url
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
224 EXPECT_EQ(CONTENT_SETTING_ASK
,
225 map
->GetContentSetting(
226 url
, url
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
227 EXPECT_EQ(CONTENT_SETTING_ASK
,
228 map
->GetContentSetting(
229 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, std::string()));
230 EXPECT_EQ(CONTENT_SETTING_ASK
,
231 map
->GetContentSetting(
232 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, std::string()));
233 EXPECT_EQ(CONTENT_SETTING_ASK
,
234 map
->GetContentSetting(
235 url
, url
, CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, std::string()));
236 EXPECT_EQ(CONTENT_SETTING_ASK
,
237 map
->GetContentSetting(
238 url
, url
, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
246 // http://crbug.com/177163
247 #if defined(OS_WIN) && !defined(NDEBUG)
248 #define MAYBE_Standard DISABLED_Standard
250 #define MAYBE_Standard Standard
252 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
, MAYBE_Standard
) {
253 CheckContentSettingsDefault();
255 const char kExtensionPath
[] = "content_settings/standard";
257 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath
, "test.html")) << message_
;
258 CheckContentSettingsSet();
260 // The settings should not be reset when the extension is reloaded.
261 ReloadExtension(last_loaded_extension_id());
262 CheckContentSettingsSet();
264 // Uninstalling and installing the extension (without running the test that
265 // calls the extension API) should clear the settings.
266 TestExtensionRegistryObserver
observer(ExtensionRegistry::Get(profile()),
267 last_loaded_extension_id());
268 UninstallExtension(last_loaded_extension_id());
269 observer
.WaitForExtensionUninstalled();
270 CheckContentSettingsDefault();
272 LoadExtension(test_data_dir_
.AppendASCII(kExtensionPath
));
273 CheckContentSettingsDefault();
276 // Flaky on the trybots. See http://crbug.com/96725.
277 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
,
278 DISABLED_GetResourceIdentifiers
) {
279 base::FilePath::CharType kFooPath
[] =
280 FILE_PATH_LITERAL("/plugins/foo.plugin");
281 base::FilePath::CharType kBarPath
[] =
282 FILE_PATH_LITERAL("/plugins/bar.plugin");
283 const char kFooName
[] = "Foo Plugin";
284 const char kBarName
[] = "Bar Plugin";
286 content::PluginService::GetInstance()->RegisterInternalPlugin(
287 content::WebPluginInfo(base::ASCIIToUTF16(kFooName
),
288 base::FilePath(kFooPath
),
289 base::ASCIIToUTF16("1.2.3"),
290 base::ASCIIToUTF16("foo")),
292 content::PluginService::GetInstance()->RegisterInternalPlugin(
293 content::WebPluginInfo(base::ASCIIToUTF16(kBarName
),
294 base::FilePath(kBarPath
),
295 base::ASCIIToUTF16("2.3.4"),
296 base::ASCIIToUTF16("bar")),
299 EXPECT_TRUE(RunExtensionTest("content_settings/getresourceidentifiers"))
303 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
,
304 UnsupportedDefaultSettings
) {
305 const char kExtensionPath
[] = "content_settings/unsupporteddefaultsettings";
306 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath
, "test.html")) << message_
;
309 } // namespace extensions