| 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" |
| 13 #include "chrome/browser/permissions/permission_request_id.h" | 14 #include "chrome/browser/permissions/permission_request_id.h" |
| 14 #include "chrome/browser/permissions/permission_request_manager.h" | 15 #include "chrome/browser/permissions/permission_request_manager.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/bookmarks/test/bookmark_test_helpers.h" | 18 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 18 #include "components/content_settings/core/browser/cookie_settings.h" | 19 #include "components/content_settings/core/browser/cookie_settings.h" |
| 19 #include "components/content_settings/core/browser/host_content_settings_map.h" | 20 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 20 #include "content/public/browser/permission_manager.h" | 21 #include "content/public/browser/permission_manager.h" |
| 21 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 content_setting); | 68 content_setting); |
| 68 } | 69 } |
| 69 | 70 |
| 70 int permission_set_count_; | 71 int permission_set_count_; |
| 71 bool last_permission_set_persisted_; | 72 bool last_permission_set_persisted_; |
| 72 ContentSetting last_permission_set_setting_; | 73 ContentSetting last_permission_set_setting_; |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 } // namespace | 76 } // namespace |
| 76 | 77 |
| 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 | 78 |
| 118 class DurableStoragePermissionContextTest | 79 class DurableStoragePermissionContextTest |
| 119 : public ChromeRenderViewHostTestHarness { | 80 : public ChromeRenderViewHostTestHarness { |
| 120 protected: | 81 protected: |
| 121 void SetUp() override { | 82 void MakeOriginImportant(const GURL& origin) { |
| 122 ChromeRenderViewHostTestHarness::SetUp(); | 83 ImportantSitesUtil::MarkOriginAsImportantForTesting(profile(), origin); |
| 123 HostContentSettingsMapFactory::GetForProfile(profile()) | |
| 124 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); | |
| 125 } | 84 } |
| 126 | |
| 127 void AddBookmark(const GURL& origin) { | |
| 128 if (!model_) { | |
| 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 } | |
| 137 | |
| 138 BookmarkModel* model_ = nullptr; | |
| 139 }; | 85 }; |
| 140 | 86 |
| 141 TEST_F(DurableStoragePermissionContextTest, Bookmarked) { | 87 TEST_F(DurableStoragePermissionContextTest, Bookmarked) { |
| 142 TestDurablePermissionContext permission_context(profile()); | 88 TestDurablePermissionContext permission_context(profile()); |
| 143 GURL url("https://www.google.com"); | 89 GURL url("https://www.google.com"); |
| 144 AddBookmark(url); | 90 MakeOriginImportant(url); |
| 145 NavigateAndCommit(url); | 91 NavigateAndCommit(url); |
| 146 | 92 |
| 147 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 93 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 148 web_contents()->GetMainFrame()->GetRoutingID(), | 94 web_contents()->GetMainFrame()->GetRoutingID(), |
| 149 -1); | 95 -1); |
| 150 | 96 |
| 151 ASSERT_EQ(0, permission_context.permission_set_count()); | 97 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 152 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 98 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 153 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 99 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 154 permission_context.last_permission_set_setting()); | 100 permission_context.last_permission_set_setting()); |
| 155 | 101 |
| 156 permission_context.DecidePermission(web_contents(), id, url, url, | 102 permission_context.DecidePermission(web_contents(), id, url, url, |
| 157 true /* user_gesture */, | 103 true /* user_gesture */, |
| 158 base::Bind(&DoNothing)); | 104 base::Bind(&DoNothing)); |
| 159 // Success. | 105 // Success. |
| 160 EXPECT_EQ(1, permission_context.permission_set_count()); | 106 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 161 EXPECT_TRUE(permission_context.last_permission_set_persisted()); | 107 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 162 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 108 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 163 permission_context.last_permission_set_setting()); | 109 permission_context.last_permission_set_setting()); |
| 164 } | 110 } |
| 165 | 111 |
| 166 TEST_F(DurableStoragePermissionContextTest, BookmarkAndIncognitoMode) { | 112 TEST_F(DurableStoragePermissionContextTest, BookmarkAndIncognitoMode) { |
| 167 TestDurablePermissionContext permission_context( | 113 TestDurablePermissionContext permission_context( |
| 168 profile()->GetOffTheRecordProfile()); | 114 profile()->GetOffTheRecordProfile()); |
| 169 GURL url("https://www.google.com"); | 115 GURL url("https://www.google.com"); |
| 170 AddBookmark(url); | 116 MakeOriginImportant(url); |
| 171 NavigateAndCommit(url); | 117 NavigateAndCommit(url); |
| 172 | 118 |
| 173 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 119 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 174 web_contents()->GetMainFrame()->GetRoutingID(), | 120 web_contents()->GetMainFrame()->GetRoutingID(), |
| 175 -1); | 121 -1); |
| 176 | 122 |
| 177 ASSERT_EQ(0, permission_context.permission_set_count()); | 123 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 178 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 124 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 179 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 125 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 180 permission_context.last_permission_set_setting()); | 126 permission_context.last_permission_set_setting()); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 210 // We shouldn't be granted. | 156 // We shouldn't be granted. |
| 211 EXPECT_EQ(1, permission_context.permission_set_count()); | 157 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 212 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | 158 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
| 213 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 159 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 214 permission_context.last_permission_set_setting()); | 160 permission_context.last_permission_set_setting()); |
| 215 } | 161 } |
| 216 | 162 |
| 217 TEST_F(DurableStoragePermissionContextTest, CookiesNotAllowed) { | 163 TEST_F(DurableStoragePermissionContextTest, CookiesNotAllowed) { |
| 218 TestDurablePermissionContext permission_context(profile()); | 164 TestDurablePermissionContext permission_context(profile()); |
| 219 GURL url("https://www.google.com"); | 165 GURL url("https://www.google.com"); |
| 220 AddBookmark(url); | 166 MakeOriginImportant(url); |
| 221 NavigateAndCommit(url); | 167 NavigateAndCommit(url); |
| 222 | 168 |
| 223 scoped_refptr<content_settings::CookieSettings> cookie_settings = | 169 scoped_refptr<content_settings::CookieSettings> cookie_settings = |
| 224 CookieSettingsFactory::GetForProfile(profile()); | 170 CookieSettingsFactory::GetForProfile(profile()); |
| 225 | 171 |
| 226 cookie_settings->SetCookieSetting(url, CONTENT_SETTING_BLOCK); | 172 cookie_settings->SetCookieSetting(url, CONTENT_SETTING_BLOCK); |
| 227 | 173 |
| 228 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 174 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 229 web_contents()->GetMainFrame()->GetRoutingID(), | 175 web_contents()->GetMainFrame()->GetRoutingID(), |
| 230 -1); | 176 -1); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 241 EXPECT_EQ(1, permission_context.permission_set_count()); | 187 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 242 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | 188 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
| 243 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 189 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 244 permission_context.last_permission_set_setting()); | 190 permission_context.last_permission_set_setting()); |
| 245 } | 191 } |
| 246 | 192 |
| 247 TEST_F(DurableStoragePermissionContextTest, EmbeddedFrame) { | 193 TEST_F(DurableStoragePermissionContextTest, EmbeddedFrame) { |
| 248 TestDurablePermissionContext permission_context(profile()); | 194 TestDurablePermissionContext permission_context(profile()); |
| 249 GURL url("https://www.google.com"); | 195 GURL url("https://www.google.com"); |
| 250 GURL requesting_url("https://www.youtube.com"); | 196 GURL requesting_url("https://www.youtube.com"); |
| 251 AddBookmark(url); | 197 MakeOriginImportant(url); |
| 252 NavigateAndCommit(url); | 198 NavigateAndCommit(url); |
| 253 | 199 |
| 254 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), | 200 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 255 web_contents()->GetMainFrame()->GetRoutingID(), | 201 web_contents()->GetMainFrame()->GetRoutingID(), |
| 256 -1); | 202 -1); |
| 257 | 203 |
| 258 ASSERT_EQ(0, permission_context.permission_set_count()); | 204 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 259 ASSERT_FALSE(permission_context.last_permission_set_persisted()); | 205 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 260 ASSERT_EQ(CONTENT_SETTING_DEFAULT, | 206 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 261 permission_context.last_permission_set_setting()); | 207 permission_context.last_permission_set_setting()); |
| 262 | 208 |
| 263 permission_context.DecidePermission(web_contents(), id, requesting_url, url, | 209 permission_context.DecidePermission(web_contents(), id, requesting_url, url, |
| 264 true /* user_gesture */, | 210 true /* user_gesture */, |
| 265 base::Bind(&DoNothing)); | 211 base::Bind(&DoNothing)); |
| 266 // We shouldn't be granted. | 212 // We shouldn't be granted. |
| 267 EXPECT_EQ(1, permission_context.permission_set_count()); | 213 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 268 EXPECT_FALSE(permission_context.last_permission_set_persisted()); | 214 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
| 269 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 215 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 270 permission_context.last_permission_set_setting()); | 216 permission_context.last_permission_set_setting()); |
| 271 } | 217 } |
| 272 | 218 |
| 273 TEST_F(DurableStoragePermissionContextTest, NonsecureOrigin) { | 219 TEST_F(DurableStoragePermissionContextTest, NonsecureOrigin) { |
| 274 TestDurablePermissionContext permission_context(profile()); | 220 TestDurablePermissionContext permission_context(profile()); |
| 275 GURL url("http://www.google.com"); | 221 GURL url("http://www.google.com"); |
| 276 | 222 |
| 277 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 223 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 278 permission_context.GetPermissionStatus(url, url)); | 224 permission_context.GetPermissionStatus(url, url)); |
| 279 } | 225 } |
| OLD | NEW |