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_PPAPI_BROKER
,
128 EXPECT_EQ(CONTENT_SETTING_ASK
,
129 map
->GetContentSetting(example_url
,
131 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
134 // Check content settings for www.google.com
135 GURL
url("http://www.google.com");
136 EXPECT_FALSE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
137 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
138 map
->GetContentSetting(
139 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
140 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
141 map
->GetContentSetting(
142 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
143 EXPECT_EQ(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
,
144 map
->GetContentSetting(
145 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
146 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
147 map
->GetContentSetting(
148 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
149 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
150 map
->GetContentSetting(
151 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
153 CONTENT_SETTING_BLOCK
,
154 map
->GetContentSetting(
155 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
156 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
157 map
->GetContentSetting(
158 url
, url
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
159 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
160 map
->GetContentSetting(
161 url
, url
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
162 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
163 map
->GetContentSetting(
164 url
, url
, CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, std::string()));
165 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
166 map
->GetContentSetting(
167 url
, url
, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
171 void CheckContentSettingsDefault() {
172 HostContentSettingsMap
* map
=
173 profile_
->GetHostContentSettingsMap();
174 content_settings::CookieSettings
* cookie_settings
=
175 CookieSettingsFactory::GetForProfile(profile_
).get();
177 // Check content settings for www.google.com
178 GURL
url("http://www.google.com");
179 EXPECT_TRUE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
180 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(url
, url
));
181 EXPECT_FALSE(cookie_settings
->IsCookieSessionOnly(url
));
182 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
183 map
->GetContentSetting(
184 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
185 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
186 map
->GetContentSetting(
187 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
188 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
189 map
->GetContentSetting(
190 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
191 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
192 map
->GetContentSetting(
193 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
194 EXPECT_EQ(CONTENT_SETTING_ASK
,
195 map
->GetContentSetting(
196 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
199 map
->GetContentSetting(
200 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
201 EXPECT_EQ(CONTENT_SETTING_ASK
,
202 map
->GetContentSetting(
203 url
, url
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
204 EXPECT_EQ(CONTENT_SETTING_ASK
,
205 map
->GetContentSetting(
206 url
, url
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
207 EXPECT_EQ(CONTENT_SETTING_ASK
,
208 map
->GetContentSetting(
209 url
, url
, CONTENT_SETTINGS_TYPE_PPAPI_BROKER
, std::string()));
210 EXPECT_EQ(CONTENT_SETTING_ASK
,
211 map
->GetContentSetting(
212 url
, url
, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS
,
220 // http://crbug.com/177163
221 #if defined(OS_WIN) && !defined(NDEBUG)
222 #define MAYBE_Standard DISABLED_Standard
224 #define MAYBE_Standard Standard
226 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
, MAYBE_Standard
) {
227 CheckContentSettingsDefault();
229 const char kExtensionPath
[] = "content_settings/standard";
231 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath
, "test.html")) << message_
;
232 CheckContentSettingsSet();
234 // The settings should not be reset when the extension is reloaded.
235 ReloadExtension(last_loaded_extension_id());
236 CheckContentSettingsSet();
238 // Uninstalling and installing the extension (without running the test that
239 // calls the extension API) should clear the settings.
240 TestExtensionRegistryObserver
observer(ExtensionRegistry::Get(profile()),
241 last_loaded_extension_id());
242 UninstallExtension(last_loaded_extension_id());
243 observer
.WaitForExtensionUninstalled();
244 CheckContentSettingsDefault();
246 LoadExtension(test_data_dir_
.AppendASCII(kExtensionPath
));
247 CheckContentSettingsDefault();
250 // Flaky on the trybots. See http://crbug.com/96725.
251 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
,
252 DISABLED_GetResourceIdentifiers
) {
253 base::FilePath::CharType kFooPath
[] =
254 FILE_PATH_LITERAL("/plugins/foo.plugin");
255 base::FilePath::CharType kBarPath
[] =
256 FILE_PATH_LITERAL("/plugins/bar.plugin");
257 const char kFooName
[] = "Foo Plugin";
258 const char kBarName
[] = "Bar Plugin";
260 content::PluginService::GetInstance()->RegisterInternalPlugin(
261 content::WebPluginInfo(base::ASCIIToUTF16(kFooName
),
262 base::FilePath(kFooPath
),
263 base::ASCIIToUTF16("1.2.3"),
264 base::ASCIIToUTF16("foo")),
266 content::PluginService::GetInstance()->RegisterInternalPlugin(
267 content::WebPluginInfo(base::ASCIIToUTF16(kBarName
),
268 base::FilePath(kBarPath
),
269 base::ASCIIToUTF16("2.3.4"),
270 base::ASCIIToUTF16("bar")),
273 EXPECT_TRUE(RunExtensionTest("content_settings/getresourceidentifiers"))
277 } // namespace extensions