OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/storage/durable_storage_permission_context.h" | 5 #include "chrome/browser/storage/durable_storage_permission_context.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
11 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 11 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
13 #include "chrome/browser/engagement/important_sites_util.h" | |
14 #include "chrome/browser/engagement/site_engagement_score.h" | |
13 #include "chrome/browser/permissions/permission_request_id.h" | 15 #include "chrome/browser/permissions/permission_request_id.h" |
14 #include "chrome/browser/permissions/permission_request_manager.h" | 16 #include "chrome/browser/permissions/permission_request_manager.h" |
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
16 #include "chrome/test/base/testing_profile.h" | 18 #include "chrome/test/base/testing_profile.h" |
17 #include "components/bookmarks/test/bookmark_test_helpers.h" | 19 #include "components/bookmarks/test/bookmark_test_helpers.h" |
18 #include "components/content_settings/core/browser/cookie_settings.h" | 20 #include "components/content_settings/core/browser/cookie_settings.h" |
19 #include "components/content_settings/core/browser/host_content_settings_map.h" | 21 #include "components/content_settings/core/browser/host_content_settings_map.h" |
20 #include "content/public/browser/permission_manager.h" | 22 #include "content/public/browser/permission_manager.h" |
21 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 content_setting); | 69 content_setting); |
68 } | 70 } |
69 | 71 |
70 int permission_set_count_; | 72 int permission_set_count_; |
71 bool last_permission_set_persisted_; | 73 bool last_permission_set_persisted_; |
72 ContentSetting last_permission_set_setting_; | 74 ContentSetting last_permission_set_setting_; |
73 }; | 75 }; |
74 | 76 |
75 } // namespace | 77 } // namespace |
76 | 78 |
77 class BookmarksOriginTest : public ::testing::Test { | |
78 protected: | |
79 static std::vector<BookmarkModel::URLAndTitle> MakeBookmarks( | |
80 const std::string urls[], | |
81 const int array_size) { | |
82 std::vector<BookmarkModel::URLAndTitle> bookmarks; | |
83 for (int i = 0; i < array_size; ++i) { | |
84 BookmarkModel::URLAndTitle bookmark; | |
85 bookmark.url = GURL(urls[i]); | |
86 EXPECT_TRUE(bookmark.url.is_valid()); | |
87 bookmarks.push_back(bookmark); | |
88 } | |
89 return bookmarks; | |
90 } | |
91 }; | |
92 | |
93 TEST_F(BookmarksOriginTest, Exists) { | |
94 std::string urls[] = { | |
95 "http://www.google.com/", | |
96 "https://dogs.com/somepage.html", | |
97 "https://mail.google.com/mail/u/0/#inbox", | |
98 }; | |
99 std::vector<BookmarkModel::URLAndTitle> bookmarks = | |
100 MakeBookmarks(urls, arraysize(urls)); | |
101 GURL looking_for("https://dogs.com"); | |
102 EXPECT_TRUE(DurableStoragePermissionContext::IsOriginBookmarked( | |
103 bookmarks, looking_for)); | |
104 } | |
105 | |
106 TEST_F(BookmarksOriginTest, DoesntExist) { | |
107 std::string urls[] = { | |
108 "http://www.google.com/", | |
109 "https://www.google.com/", | |
110 }; | |
111 std::vector<BookmarkModel::URLAndTitle> bookmarks = | |
112 MakeBookmarks(urls, arraysize(urls)); | |
113 GURL looking_for("https://dogs.com"); | |
114 EXPECT_FALSE(DurableStoragePermissionContext::IsOriginBookmarked( | |
115 bookmarks, looking_for)); | |
116 } | |
117 | 79 |
118 class DurableStoragePermissionContextTest | 80 class DurableStoragePermissionContextTest |
119 : public ChromeRenderViewHostTestHarness { | 81 : public ChromeRenderViewHostTestHarness { |
120 protected: | 82 protected: |
121 void SetUp() override { | 83 void SetUp() override { |
122 ChromeRenderViewHostTestHarness::SetUp(); | 84 ChromeRenderViewHostTestHarness::SetUp(); |
123 HostContentSettingsMapFactory::GetForProfile(profile()) | 85 SiteEngagementScore::SetParamValuesForTesting(); |
michaeln
2016/10/05 23:27:45
would it work/makesense to put this call inside of
dmurph
2016/10/06 01:02:00
Done.
| |
124 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); | |
125 } | 86 } |
126 | 87 |
127 void AddBookmark(const GURL& origin) { | 88 void MakeOriginImportant(const GURL& origin) { |
128 if (!model_) { | 89 ImportantSitesUtil::MarkOriginAsImportantForTesting(profile(), origin); |
129 profile()->CreateBookmarkModel(true); | |
130 model_ = BookmarkModelFactory::GetForBrowserContext(profile()); | |
131 bookmarks::test::WaitForBookmarkModelToLoad(model_); | |
132 } | |
133 | |
134 model_->AddURL(model_->bookmark_bar_node(), 0, | |
135 base::ASCIIToUTF16(origin.spec()), origin); | |
136 } | 90 } |
137 | |
138 BookmarkModel* model_ = nullptr; | |
139 }; | 91 }; |
140 | 92 |
141 TEST_F(DurableStoragePermissionContextTest, Bookmarked) { | 93 TEST_F(DurableStoragePermissionContextTest, Bookmarked) { |
142 TestDurablePermissionContext permission_context(profile()); | 94 TestDurablePermissionContext permission_context(profile()); |
143 GURL url("https://www.google.com"); | 95 GURL url("https://www.google.com"); |
144 AddBookmark(url); | 96 MakeOriginImportant(url); |
145 NavigateAndCommit(url); | 97 NavigateAndCommit(url); |
146 | 98 |
147 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 99 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
148 web_contents()->GetMainFrame()->GetRoutingID(), | 100 web_contents()->GetMainFrame()->GetRoutingID(), |
149 -1); | 101 -1); |
150 | 102 |
151 ASSERT_EQ(0, permission_context.permission_set_count()); | 103 ASSERT_EQ(0, permission_context.permission_set_count()); |
152 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 104 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
153 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 105 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
154 permission_context.last_permission_set_setting()); | 106 permission_context.last_permission_set_setting()); |
155 | 107 |
156 permission_context.DecidePermission(web_contents(), id, url, url, | 108 permission_context.DecidePermission(web_contents(), id, url, url, |
157 true /* user_gesture */, | 109 true /* user_gesture */, |
158 base::Bind(&DoNothing)); | 110 base::Bind(&DoNothing)); |
159 // Success. | 111 // Success. |
160 EXPECT_EQ(1, permission_context.permission_set_count()); | 112 EXPECT_EQ(1, permission_context.permission_set_count()); |
161 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 113 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
162 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 114 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
163 permission_context.last_permission_set_setting()); | 115 permission_context.last_permission_set_setting()); |
164 } | 116 } |
165 | 117 |
166 TEST_F(DurableStoragePermissionContextTest, BookmarkAndIncognitoMode) { | 118 TEST_F(DurableStoragePermissionContextTest, BookmarkAndIncognitoMode) { |
167 TestDurablePermissionContext permission_context( | 119 TestDurablePermissionContext permission_context( |
168 profile()->GetOffTheRecordProfile()); | 120 profile()->GetOffTheRecordProfile()); |
169 GURL url("https://www.google.com"); | 121 GURL url("https://www.google.com"); |
170 AddBookmark(url); | 122 MakeOriginImportant(url); |
171 NavigateAndCommit(url); | 123 NavigateAndCommit(url); |
172 | 124 |
173 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 125 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
174 web_contents()->GetMainFrame()->GetRoutingID(), | 126 web_contents()->GetMainFrame()->GetRoutingID(), |
175 -1); | 127 -1); |
176 | 128 |
177 ASSERT_EQ(0, permission_context.permission_set_count()); | 129 ASSERT_EQ(0, permission_context.permission_set_count()); |
178 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 130 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
179 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 131 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
180 permission_context.last_permission_set_setting()); | 132 permission_context.last_permission_set_setting()); |
(...skipping 29 matching lines...) Expand all Loading... | |
210 // We shouldn't be granted. | 162 // We shouldn't be granted. |
211 EXPECT_EQ(1, permission_context.permission_set_count()); | 163 EXPECT_EQ(1, permission_context.permission_set_count()); |
212 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | 164 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
213 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 165 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
214 permission_context.last_permission_set_setting()); | 166 permission_context.last_permission_set_setting()); |
215 } | 167 } |
216 | 168 |
217 TEST_F(DurableStoragePermissionContextTest, CookiesNotAllowed) { | 169 TEST_F(DurableStoragePermissionContextTest, CookiesNotAllowed) { |
218 TestDurablePermissionContext permission_context(profile()); | 170 TestDurablePermissionContext permission_context(profile()); |
219 GURL url("https://www.google.com"); | 171 GURL url("https://www.google.com"); |
220 AddBookmark(url); | 172 MakeOriginImportant(url); |
221 NavigateAndCommit(url); | 173 NavigateAndCommit(url); |
222 | 174 |
223 scoped_refptr<content_settings::CookieSettings> cookie_settings = | 175 scoped_refptr<content_settings::CookieSettings> cookie_settings = |
224 CookieSettingsFactory::GetForProfile(profile()); | 176 CookieSettingsFactory::GetForProfile(profile()); |
225 | 177 |
226 cookie_settings->SetCookieSetting(url, CONTENT_SETTING_BLOCK); | 178 cookie_settings->SetCookieSetting(url, CONTENT_SETTING_BLOCK); |
227 | 179 |
228 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 180 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
229 web_contents()->GetMainFrame()->GetRoutingID(), | 181 web_contents()->GetMainFrame()->GetRoutingID(), |
230 -1); | 182 -1); |
(...skipping 10 matching lines...) Expand all Loading... | |
241 EXPECT_EQ(1, permission_context.permission_set_count()); | 193 EXPECT_EQ(1, permission_context.permission_set_count()); |
242 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | 194 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
243 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 195 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
244 permission_context.last_permission_set_setting()); | 196 permission_context.last_permission_set_setting()); |
245 } | 197 } |
246 | 198 |
247 TEST_F(DurableStoragePermissionContextTest, EmbeddedFrame) { | 199 TEST_F(DurableStoragePermissionContextTest, EmbeddedFrame) { |
248 TestDurablePermissionContext permission_context(profile()); | 200 TestDurablePermissionContext permission_context(profile()); |
249 GURL url("https://www.google.com"); | 201 GURL url("https://www.google.com"); |
250 GURL requesting_url("https://www.youtube.com"); | 202 GURL requesting_url("https://www.youtube.com"); |
251 AddBookmark(url); | 203 MakeOriginImportant(url); |
252 NavigateAndCommit(url); | 204 NavigateAndCommit(url); |
253 | 205 |
254 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 206 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
255 web_contents()->GetMainFrame()->GetRoutingID(), | 207 web_contents()->GetMainFrame()->GetRoutingID(), |
256 -1); | 208 -1); |
257 | 209 |
258 ASSERT_EQ(0, permission_context.permission_set_count()); | 210 ASSERT_EQ(0, permission_context.permission_set_count()); |
259 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 211 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
260 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 212 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
261 permission_context.last_permission_set_setting()); | 213 permission_context.last_permission_set_setting()); |
262 | 214 |
263 permission_context.DecidePermission(web_contents(), id, requesting_url, url, | 215 permission_context.DecidePermission(web_contents(), id, requesting_url, url, |
264 true /* user_gesture */, | 216 true /* user_gesture */, |
265 base::Bind(&DoNothing)); | 217 base::Bind(&DoNothing)); |
266 // We shouldn't be granted. | 218 // We shouldn't be granted. |
267 EXPECT_EQ(1, permission_context.permission_set_count()); | 219 EXPECT_EQ(1, permission_context.permission_set_count()); |
268 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | 220 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
269 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 221 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
270 permission_context.last_permission_set_setting()); | 222 permission_context.last_permission_set_setting()); |
271 } | 223 } |
272 | 224 |
273 TEST_F(DurableStoragePermissionContextTest, NonsecureOrigin) { | 225 TEST_F(DurableStoragePermissionContextTest, NonsecureOrigin) { |
274 TestDurablePermissionContext permission_context(profile()); | 226 TestDurablePermissionContext permission_context(profile()); |
275 GURL url("http://www.google.com"); | 227 GURL url("http://www.google.com"); |
276 | 228 |
277 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 229 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
278 permission_context.GetPermissionStatus(url, url)); | 230 permission_context.GetPermissionStatus(url, url)); |
279 } | 231 } |
OLD | NEW |