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/extensions/api/content_settings/content_settings_api.h"
14 #include "chrome/browser/extensions/extension_apitest.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "components/content_settings/core/browser/cookie_settings.h"
19 #include "components/content_settings/core/browser/host_content_settings_map.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/plugin_service.h"
22 #include "content/public/common/webplugininfo.h"
23 #include "content/public/test/test_utils.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/browser/test_extension_registry_observer.h"
29 void ReleaseBrowserProcessModule() {
30 g_browser_process
->ReleaseModule();
35 namespace extensions
{
37 class ExtensionContentSettingsApiTest
: public ExtensionApiTest
{
39 ExtensionContentSettingsApiTest() : profile_(NULL
) {}
41 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
42 ExtensionApiTest::SetUpCommandLine(command_line
);
43 command_line
->AppendSwitch(switches::kDisablePluginsDiscovery
);
46 void SetUpOnMainThread() override
{
47 ExtensionApiTest::SetUpOnMainThread();
49 // The browser might get closed later (and therefore be destroyed), so we
51 profile_
= browser()->profile();
53 // Closing the last browser window also releases a module reference. Make
54 // sure it's not the last one, so the message loop doesn't quit
56 g_browser_process
->AddRefModule();
59 void TearDownOnMainThread() override
{
60 // ReleaseBrowserProcessModule() needs to be called in a message loop, so we
61 // post a task to do it, then run the message loop.
62 base::ThreadTaskRunnerHandle::Get()->PostTask(
63 FROM_HERE
, base::Bind(&ReleaseBrowserProcessModule
));
64 content::RunAllPendingInMessageLoop();
66 ExtensionApiTest::TearDownOnMainThread();
70 void CheckContentSettingsSet() {
71 HostContentSettingsMap
* map
=
72 profile_
->GetHostContentSettingsMap();
73 content_settings::CookieSettings
* cookie_settings
=
74 CookieSettingsFactory::GetForProfile(profile_
).get();
76 // Check default content settings by using an unknown URL.
77 GURL
example_url("http://www.example.com");
78 EXPECT_TRUE(cookie_settings
->IsReadingCookieAllowed(
79 example_url
, example_url
));
80 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(
81 example_url
, example_url
));
82 EXPECT_TRUE(cookie_settings
->IsCookieSessionOnly(example_url
));
83 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
84 map
->GetContentSetting(example_url
,
86 CONTENT_SETTINGS_TYPE_IMAGES
,
88 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
89 map
->GetContentSetting(example_url
,
91 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
93 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
94 map
->GetContentSetting(example_url
,
96 CONTENT_SETTINGS_TYPE_PLUGINS
,
98 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
99 map
->GetContentSetting(example_url
,
101 CONTENT_SETTINGS_TYPE_POPUPS
,
103 EXPECT_EQ(CONTENT_SETTING_ASK
,
104 map
->GetContentSetting(example_url
,
106 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
108 EXPECT_EQ(CONTENT_SETTING_ASK
,
109 map
->GetContentSetting(example_url
,
111 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
113 EXPECT_EQ(CONTENT_SETTING_ASK
,
114 map
->GetContentSetting(example_url
,
116 CONTENT_SETTINGS_TYPE_FULLSCREEN
,
118 EXPECT_EQ(CONTENT_SETTING_ASK
,
119 map
->GetContentSetting(example_url
,
121 CONTENT_SETTINGS_TYPE_MOUSELOCK
,
123 EXPECT_EQ(CONTENT_SETTING_ASK
,
124 map
->GetContentSetting(example_url
,
126 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
,
128 EXPECT_EQ(CONTENT_SETTING_ASK
,
129 map
->GetContentSetting(example_url
,
131 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
,
133 EXPECT_EQ(CONTENT_SETTING_ASK
,
134 map
->GetContentSetting(example_url
,
136 CONTENT_SETTINGS_TYPE_PPAPI_BROKER
,
138 EXPECT_EQ(CONTENT_SETTING_ASK
,
139 map
->GetContentSetting(example_url
,
141 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
144 // Check content settings for www.google.com
145 GURL
url("http://www.google.com");
146 EXPECT_FALSE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
147 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
148 map
->GetContentSetting(
149 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
150 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
151 map
->GetContentSetting(
152 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
153 EXPECT_EQ(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
,
154 map
->GetContentSetting(
155 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
156 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
157 map
->GetContentSetting(
158 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
159 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
160 map
->GetContentSetting(
161 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
163 CONTENT_SETTING_BLOCK
,
164 map
->GetContentSetting(
165 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
166 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
167 map
->GetContentSetting(
168 url
, url
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
169 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
170 map
->GetContentSetting(
171 url
, url
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
172 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
173 map
->GetContentSetting(
174 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, std::string()));
175 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
176 map
->GetContentSetting(
177 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, std::string()));
178 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
179 map
->GetContentSetting(
180 url
, url
, CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, std::string()));
181 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
182 map
->GetContentSetting(
183 url
, url
, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
187 void CheckContentSettingsDefault() {
188 HostContentSettingsMap
* map
=
189 profile_
->GetHostContentSettingsMap();
190 content_settings::CookieSettings
* cookie_settings
=
191 CookieSettingsFactory::GetForProfile(profile_
).get();
193 // Check content settings for www.google.com
194 GURL
url("http://www.google.com");
195 EXPECT_TRUE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
196 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(url
, url
));
197 EXPECT_FALSE(cookie_settings
->IsCookieSessionOnly(url
));
198 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
199 map
->GetContentSetting(
200 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
201 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
202 map
->GetContentSetting(
203 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
204 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
205 map
->GetContentSetting(
206 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
207 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
208 map
->GetContentSetting(
209 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
210 EXPECT_EQ(CONTENT_SETTING_ASK
,
211 map
->GetContentSetting(
212 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
215 map
->GetContentSetting(
216 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
217 EXPECT_EQ(CONTENT_SETTING_ASK
,
218 map
->GetContentSetting(
219 url
, url
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
220 EXPECT_EQ(CONTENT_SETTING_ASK
,
221 map
->GetContentSetting(
222 url
, url
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
223 EXPECT_EQ(CONTENT_SETTING_ASK
,
224 map
->GetContentSetting(
225 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
, std::string()));
226 EXPECT_EQ(CONTENT_SETTING_ASK
,
227 map
->GetContentSetting(
228 url
, url
, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
, std::string()));
229 EXPECT_EQ(CONTENT_SETTING_ASK
,
230 map
->GetContentSetting(
231 url
, url
, CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, std::string()));
232 EXPECT_EQ(CONTENT_SETTING_ASK
,
233 map
->GetContentSetting(
234 url
, url
, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
242 // http://crbug.com/177163
243 #if defined(OS_WIN) && !defined(NDEBUG)
244 #define MAYBE_Standard DISABLED_Standard
246 #define MAYBE_Standard Standard
248 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
, MAYBE_Standard
) {
249 CheckContentSettingsDefault();
251 const char kExtensionPath
[] = "content_settings/standard";
253 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath
, "test.html")) << message_
;
254 CheckContentSettingsSet();
256 // The settings should not be reset when the extension is reloaded.
257 ReloadExtension(last_loaded_extension_id());
258 CheckContentSettingsSet();
260 // Uninstalling and installing the extension (without running the test that
261 // calls the extension API) should clear the settings.
262 TestExtensionRegistryObserver
observer(ExtensionRegistry::Get(profile()),
263 last_loaded_extension_id());
264 UninstallExtension(last_loaded_extension_id());
265 observer
.WaitForExtensionUninstalled();
266 CheckContentSettingsDefault();
268 LoadExtension(test_data_dir_
.AppendASCII(kExtensionPath
));
269 CheckContentSettingsDefault();
272 // Flaky on the trybots. See http://crbug.com/96725.
273 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
,
274 DISABLED_GetResourceIdentifiers
) {
275 base::FilePath::CharType kFooPath
[] =
276 FILE_PATH_LITERAL("/plugins/foo.plugin");
277 base::FilePath::CharType kBarPath
[] =
278 FILE_PATH_LITERAL("/plugins/bar.plugin");
279 const char kFooName
[] = "Foo Plugin";
280 const char kBarName
[] = "Bar Plugin";
282 content::PluginService::GetInstance()->RegisterInternalPlugin(
283 content::WebPluginInfo(base::ASCIIToUTF16(kFooName
),
284 base::FilePath(kFooPath
),
285 base::ASCIIToUTF16("1.2.3"),
286 base::ASCIIToUTF16("foo")),
288 content::PluginService::GetInstance()->RegisterInternalPlugin(
289 content::WebPluginInfo(base::ASCIIToUTF16(kBarName
),
290 base::FilePath(kBarPath
),
291 base::ASCIIToUTF16("2.3.4"),
292 base::ASCIIToUTF16("bar")),
295 EXPECT_TRUE(RunExtensionTest("content_settings/getresourceidentifiers"))
299 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
,
300 UnsupportedDefaultSettings
) {
301 const char kExtensionPath
[] = "content_settings/unsupporteddefaultsettings";
302 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath
, "test.html")) << message_
;
305 } // namespace extensions