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/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 11 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
| 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 13 #include "chrome/browser/permissions/permission_request_id.h" |
| 14 #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/testing_profile.h" |
| 17 #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/host_content_settings_map.h" |
| 20 #include "content/public/browser/permission_manager.h" |
| 21 #include "content/public/browser/render_process_host.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
9 | 23 |
10 using bookmarks::BookmarkModel; | 24 using bookmarks::BookmarkModel; |
11 | 25 |
| 26 namespace { |
| 27 |
| 28 void DoNothing(ContentSetting content_setting) {} |
| 29 |
| 30 class TestDurablePermissionContext : public DurableStoragePermissionContext { |
| 31 public: |
| 32 explicit TestDurablePermissionContext(Profile* profile) |
| 33 : DurableStoragePermissionContext(profile), |
| 34 permission_set_count_(0), |
| 35 last_permission_set_persisted_(false), |
| 36 last_permission_set_setting_(CONTENT_SETTING_DEFAULT) {} |
| 37 |
| 38 int permission_set_count() const { return permission_set_count_; } |
| 39 bool last_permission_set_persisted() const { |
| 40 return last_permission_set_persisted_; |
| 41 } |
| 42 ContentSetting last_permission_set_setting() const { |
| 43 return last_permission_set_setting_; |
| 44 } |
| 45 |
| 46 ContentSetting GetContentSettingFromMap(const GURL& url_a, |
| 47 const GURL& url_b) { |
| 48 return HostContentSettingsMapFactory::GetForProfile(profile()) |
| 49 ->GetContentSetting(url_a.GetOrigin(), url_b.GetOrigin(), |
| 50 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, |
| 51 std::string()); |
| 52 } |
| 53 |
| 54 private: |
| 55 // NotificationPermissionContext: |
| 56 void NotifyPermissionSet(const PermissionRequestID& id, |
| 57 const GURL& requesting_origin, |
| 58 const GURL& embedder_origin, |
| 59 const BrowserPermissionCallback& callback, |
| 60 bool persist, |
| 61 ContentSetting content_setting) override { |
| 62 permission_set_count_++; |
| 63 last_permission_set_persisted_ = persist; |
| 64 last_permission_set_setting_ = content_setting; |
| 65 DurableStoragePermissionContext::NotifyPermissionSet( |
| 66 id, requesting_origin, embedder_origin, callback, persist, |
| 67 content_setting); |
| 68 } |
| 69 |
| 70 int permission_set_count_; |
| 71 bool last_permission_set_persisted_; |
| 72 ContentSetting last_permission_set_setting_; |
| 73 }; |
| 74 |
| 75 } // namespace |
| 76 |
12 class BookmarksOriginTest : public ::testing::Test { | 77 class BookmarksOriginTest : public ::testing::Test { |
13 protected: | 78 protected: |
14 static std::vector<BookmarkModel::URLAndTitle> MakeBookmarks( | 79 static std::vector<BookmarkModel::URLAndTitle> MakeBookmarks( |
15 const std::string urls[], | 80 const std::string urls[], |
16 const int array_size) { | 81 const int array_size) { |
17 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 82 std::vector<BookmarkModel::URLAndTitle> bookmarks; |
18 for (int i = 0; i < array_size; ++i) { | 83 for (int i = 0; i < array_size; ++i) { |
19 BookmarkModel::URLAndTitle bookmark; | 84 BookmarkModel::URLAndTitle bookmark; |
20 bookmark.url = GURL(urls[i]); | 85 bookmark.url = GURL(urls[i]); |
21 EXPECT_TRUE(bookmark.url.is_valid()); | 86 EXPECT_TRUE(bookmark.url.is_valid()); |
(...skipping 20 matching lines...) Expand all Loading... |
42 std::string urls[] = { | 107 std::string urls[] = { |
43 "http://www.google.com/", | 108 "http://www.google.com/", |
44 "https://www.google.com/", | 109 "https://www.google.com/", |
45 }; | 110 }; |
46 std::vector<BookmarkModel::URLAndTitle> bookmarks = | 111 std::vector<BookmarkModel::URLAndTitle> bookmarks = |
47 MakeBookmarks(urls, arraysize(urls)); | 112 MakeBookmarks(urls, arraysize(urls)); |
48 GURL looking_for("https://dogs.com"); | 113 GURL looking_for("https://dogs.com"); |
49 EXPECT_FALSE(DurableStoragePermissionContext::IsOriginBookmarked( | 114 EXPECT_FALSE(DurableStoragePermissionContext::IsOriginBookmarked( |
50 bookmarks, looking_for)); | 115 bookmarks, looking_for)); |
51 } | 116 } |
| 117 |
| 118 class DurableStoragePermissionContextTest |
| 119 : public ChromeRenderViewHostTestHarness { |
| 120 protected: |
| 121 void SetUp() override { |
| 122 ChromeRenderViewHostTestHarness::SetUp(); |
| 123 HostContentSettingsMapFactory::GetForProfile(profile()) |
| 124 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_DURABLE_STORAGE); |
| 125 } |
| 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 }; |
| 140 |
| 141 TEST_F(DurableStoragePermissionContextTest, Bookmarked) { |
| 142 TestDurablePermissionContext permission_context(profile()); |
| 143 GURL url("https://www.google.com"); |
| 144 AddBookmark(url); |
| 145 NavigateAndCommit(url); |
| 146 |
| 147 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 148 web_contents()->GetMainFrame()->GetRoutingID(), |
| 149 -1); |
| 150 |
| 151 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 152 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 153 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 154 permission_context.last_permission_set_setting()); |
| 155 |
| 156 permission_context.DecidePermission(web_contents(), id, url, url, |
| 157 true /* user_gesture */, |
| 158 base::Bind(&DoNothing)); |
| 159 // Success. |
| 160 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 161 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 162 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 163 permission_context.last_permission_set_setting()); |
| 164 } |
| 165 |
| 166 TEST_F(DurableStoragePermissionContextTest, BookmarkAndIncognitoMode) { |
| 167 TestDurablePermissionContext permission_context( |
| 168 profile()->GetOffTheRecordProfile()); |
| 169 GURL url("https://www.google.com"); |
| 170 AddBookmark(url); |
| 171 NavigateAndCommit(url); |
| 172 |
| 173 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 174 web_contents()->GetMainFrame()->GetRoutingID(), |
| 175 -1); |
| 176 |
| 177 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 178 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 179 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 180 permission_context.last_permission_set_setting()); |
| 181 |
| 182 permission_context.DecidePermission(web_contents(), id, url, url, |
| 183 true /* user_gesture */, |
| 184 base::Bind(&DoNothing)); |
| 185 // Success. |
| 186 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 187 EXPECT_TRUE(permission_context.last_permission_set_persisted()); |
| 188 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 189 permission_context.last_permission_set_setting()); |
| 190 } |
| 191 |
| 192 TEST_F(DurableStoragePermissionContextTest, NoBookmark) { |
| 193 TestDurablePermissionContext permission_context(profile()); |
| 194 GURL url("https://www.google.com"); |
| 195 NavigateAndCommit(url); |
| 196 |
| 197 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 198 web_contents()->GetMainFrame()->GetRoutingID(), |
| 199 -1); |
| 200 |
| 201 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 202 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 203 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 204 permission_context.last_permission_set_setting()); |
| 205 |
| 206 permission_context.DecidePermission(web_contents(), id, url, url, |
| 207 true /* user_gesture */, |
| 208 base::Bind(&DoNothing)); |
| 209 |
| 210 // We shouldn't be granted. |
| 211 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 212 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
| 213 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 214 permission_context.last_permission_set_setting()); |
| 215 } |
| 216 |
| 217 TEST_F(DurableStoragePermissionContextTest, CookiesNotAllowed) { |
| 218 TestDurablePermissionContext permission_context(profile()); |
| 219 GURL url("https://www.google.com"); |
| 220 AddBookmark(url); |
| 221 NavigateAndCommit(url); |
| 222 |
| 223 scoped_refptr<content_settings::CookieSettings> cookie_settings = |
| 224 CookieSettingsFactory::GetForProfile(profile()); |
| 225 |
| 226 cookie_settings->SetCookieSetting(url, CONTENT_SETTING_BLOCK); |
| 227 |
| 228 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 229 web_contents()->GetMainFrame()->GetRoutingID(), |
| 230 -1); |
| 231 |
| 232 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 233 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 234 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 235 permission_context.last_permission_set_setting()); |
| 236 |
| 237 permission_context.DecidePermission(web_contents(), id, url, url, |
| 238 true /* user_gesture */, |
| 239 base::Bind(&DoNothing)); |
| 240 // We shouldn't be granted. |
| 241 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 242 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
| 243 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 244 permission_context.last_permission_set_setting()); |
| 245 } |
| 246 |
| 247 TEST_F(DurableStoragePermissionContextTest, EmbeddedFrame) { |
| 248 TestDurablePermissionContext permission_context(profile()); |
| 249 GURL url("https://www.google.com"); |
| 250 GURL requesting_url("https://www.youtube.com"); |
| 251 AddBookmark(url); |
| 252 NavigateAndCommit(url); |
| 253 |
| 254 const PermissionRequestID id(web_contents()->GetRenderProcessHost()->GetID(), |
| 255 web_contents()->GetMainFrame()->GetRoutingID(), |
| 256 -1); |
| 257 |
| 258 ASSERT_EQ(0, permission_context.permission_set_count()); |
| 259 ASSERT_FALSE(permission_context.last_permission_set_persisted()); |
| 260 ASSERT_EQ(CONTENT_SETTING_DEFAULT, |
| 261 permission_context.last_permission_set_setting()); |
| 262 |
| 263 permission_context.DecidePermission(web_contents(), id, requesting_url, url, |
| 264 true /* user_gesture */, |
| 265 base::Bind(&DoNothing)); |
| 266 // We shouldn't be granted. |
| 267 EXPECT_EQ(1, permission_context.permission_set_count()); |
| 268 EXPECT_FALSE(permission_context.last_permission_set_persisted()); |
| 269 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 270 permission_context.last_permission_set_setting()); |
| 271 } |
| 272 |
| 273 TEST_F(DurableStoragePermissionContextTest, NonsecureOrigin) { |
| 274 TestDurablePermissionContext permission_context(profile()); |
| 275 GURL url("http://www.google.com"); |
| 276 |
| 277 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 278 permission_context.GetPermissionStatus(url, url)); |
| 279 } |
OLD | NEW |