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/prefs/pref_service.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/content_settings/cookie_settings.h"
10 #include "chrome/browser/content_settings/host_content_settings_map.h"
11 #include "chrome/browser/extensions/api/content_settings/content_settings_api.h"
12 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/plugin_service.h"
19 #include "content/public/common/webplugininfo.h"
20 #include "content/public/test/test_utils.h"
24 void ReleaseBrowserProcessModule() {
25 g_browser_process
->ReleaseModule();
30 namespace extensions
{
32 class ExtensionContentSettingsApiTest
: public ExtensionApiTest
{
34 ExtensionContentSettingsApiTest() : profile_(NULL
) {}
36 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
37 ExtensionApiTest::SetUpCommandLine(command_line
);
38 command_line
->AppendSwitch(switches::kDisablePluginsDiscovery
);
41 virtual void SetUpOnMainThread() OVERRIDE
{
42 ExtensionApiTest::SetUpOnMainThread();
44 // The browser might get closed later (and therefore be destroyed), so we
46 profile_
= browser()->profile();
48 // Closing the last browser window also releases a module reference. Make
49 // sure it's not the last one, so the message loop doesn't quit
51 g_browser_process
->AddRefModule();
54 virtual void CleanUpOnMainThread() OVERRIDE
{
55 // ReleaseBrowserProcessModule() needs to be called in a message loop, so we
56 // post a task to do it, then run the message loop.
57 base::MessageLoop::current()->PostTask(
58 FROM_HERE
, base::Bind(&ReleaseBrowserProcessModule
));
59 content::RunAllPendingInMessageLoop();
61 ExtensionApiTest::CleanUpOnMainThread();
65 void CheckContentSettingsSet() {
66 HostContentSettingsMap
* map
=
67 profile_
->GetHostContentSettingsMap();
68 CookieSettings
* cookie_settings
=
69 CookieSettings::Factory::GetForProfile(profile_
).get();
71 // Check default content settings by using an unknown URL.
72 GURL
example_url("http://www.example.com");
73 EXPECT_TRUE(cookie_settings
->IsReadingCookieAllowed(
74 example_url
, example_url
));
75 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(
76 example_url
, example_url
));
77 EXPECT_TRUE(cookie_settings
->IsCookieSessionOnly(example_url
));
78 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
79 map
->GetContentSetting(example_url
,
81 CONTENT_SETTINGS_TYPE_IMAGES
,
83 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
84 map
->GetContentSetting(example_url
,
86 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
88 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
89 map
->GetContentSetting(example_url
,
91 CONTENT_SETTINGS_TYPE_PLUGINS
,
93 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
94 map
->GetContentSetting(example_url
,
96 CONTENT_SETTINGS_TYPE_POPUPS
,
99 // TODO(bauerb): Enable once geolocation settings are integrated into the
100 // HostContentSettingsMap.
101 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
102 map
->GetContentSetting(example_url
,
104 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
107 EXPECT_EQ(CONTENT_SETTING_ASK
,
108 map
->GetContentSetting(example_url
,
110 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
113 // Check content settings for www.google.com
114 GURL
url("http://www.google.com");
115 EXPECT_FALSE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
116 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
117 map
->GetContentSetting(
118 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
119 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
120 map
->GetContentSetting(
121 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
122 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
123 map
->GetContentSetting(
124 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
125 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
126 map
->GetContentSetting(
127 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
129 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
130 map
->GetContentSetting(
131 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
134 CONTENT_SETTING_BLOCK
,
135 map
->GetContentSetting(
136 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
139 void CheckContentSettingsDefault() {
140 HostContentSettingsMap
* map
=
141 profile_
->GetHostContentSettingsMap();
142 CookieSettings
* cookie_settings
=
143 CookieSettings::Factory::GetForProfile(profile_
).get();
145 // Check content settings for www.google.com
146 GURL
url("http://www.google.com");
147 EXPECT_TRUE(cookie_settings
->IsReadingCookieAllowed(url
, url
));
148 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(url
, url
));
149 EXPECT_FALSE(cookie_settings
->IsCookieSessionOnly(url
));
150 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
151 map
->GetContentSetting(
152 url
, url
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
153 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
154 map
->GetContentSetting(
155 url
, url
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
156 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
157 map
->GetContentSetting(
158 url
, url
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
159 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
160 map
->GetContentSetting(
161 url
, url
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
163 // TODO(bauerb): Enable once geolocation settings are integrated into the
164 // HostContentSettingsMap.
165 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
166 map
->GetContentSetting(
167 url
, url
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
171 map
->GetContentSetting(
172 url
, url
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
179 // http://crbug.com/177163
180 #if defined(OS_WIN) && !defined(NDEBUG)
181 #define MAYBE_Standard DISABLED_Standard
183 #define MAYBE_Standard Standard
185 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
, MAYBE_Standard
) {
186 CheckContentSettingsDefault();
188 const char kExtensionPath
[] = "content_settings/standard";
190 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath
, "test.html")) << message_
;
191 CheckContentSettingsSet();
193 // The settings should not be reset when the extension is reloaded.
194 ReloadExtension(last_loaded_extension_id());
195 CheckContentSettingsSet();
197 // Uninstalling and installing the extension (without running the test that
198 // calls the extension API) should clear the settings.
199 content::WindowedNotificationObserver
observer(
200 chrome::NOTIFICATION_EXTENSION_UNINSTALLED
,
201 content::NotificationService::AllSources());
202 UninstallExtension(last_loaded_extension_id());
204 CheckContentSettingsDefault();
206 LoadExtension(test_data_dir_
.AppendASCII(kExtensionPath
));
207 CheckContentSettingsDefault();
210 // Flaky on the trybots. See http://crbug.com/96725.
211 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest
,
212 DISABLED_GetResourceIdentifiers
) {
213 base::FilePath::CharType kFooPath
[] =
214 FILE_PATH_LITERAL("/plugins/foo.plugin");
215 base::FilePath::CharType kBarPath
[] =
216 FILE_PATH_LITERAL("/plugins/bar.plugin");
217 const char* kFooName
= "Foo Plugin";
218 const char* kBarName
= "Bar Plugin";
220 content::PluginService::GetInstance()->RegisterInternalPlugin(
221 content::WebPluginInfo(base::ASCIIToUTF16(kFooName
),
222 base::FilePath(kFooPath
),
223 base::ASCIIToUTF16("1.2.3"),
224 base::ASCIIToUTF16("foo")),
226 content::PluginService::GetInstance()->RegisterInternalPlugin(
227 content::WebPluginInfo(base::ASCIIToUTF16(kBarName
),
228 base::FilePath(kBarPath
),
229 base::ASCIIToUTF16("2.3.4"),
230 base::ASCIIToUTF16("bar")),
233 EXPECT_TRUE(RunExtensionTest("content_settings/getresourceidentifiers"))
237 } // namespace extensions