forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Background Sync Permission Context
This is the second in a series of patches to add a content setting to Background Sync. For a detailed plan, refer to https://goo.gl/9U8fKh This change adds the permission context for background sync. No behavoiur change happens yet, as the permission is granted by default and no UI is exposed to the user. Additionally, it has not been hooked to the Background Sync Manager. Original, 'big picture' patch: crrev.com/1673303002 BUG=564052 TEST=BackgroundSyncPermissionContextTest.* Review URL: https://codereview.chromium.org/1698323002 Cr-Commit-Position: refs/heads/master@{#378646}
- Loading branch information
Showing
17 changed files
with
285 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
chrome/browser/background_sync/background_sync_permission_context.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/background_sync/background_sync_permission_context.h" | ||
|
||
#include "base/logging.h" | ||
#include "components/content_settings/core/common/content_settings_types.h" | ||
#include "content/public/browser/permission_type.h" | ||
|
||
BackgroundSyncPermissionContext::BackgroundSyncPermissionContext( | ||
Profile* profile) | ||
: PermissionContextBase(profile, | ||
content::PermissionType::BACKGROUND_SYNC, | ||
CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC) {} | ||
|
||
void BackgroundSyncPermissionContext::CancelPermissionRequest( | ||
content::WebContents* web_contents, | ||
const PermissionRequestID& id) { | ||
// Background sync permission requests are resolved instantly without | ||
// prompting the user, there is no way to cancel them. | ||
NOTREACHED(); | ||
} | ||
|
||
void BackgroundSyncPermissionContext::DecidePermission( | ||
content::WebContents* web_contents, | ||
const PermissionRequestID& id, | ||
const GURL& requesting_origin, | ||
const GURL& embedding_origin, | ||
const BrowserPermissionCallback& callback) { | ||
// The user should never be prompted to authorize background sync. | ||
NOTREACHED(); | ||
} | ||
|
||
bool BackgroundSyncPermissionContext::IsRestrictedToSecureOrigins() const { | ||
return true; | ||
} |
41 changes: 41 additions & 0 deletions
41
chrome/browser/background_sync/background_sync_permission_context.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_PERMISSION_CONTEXT_H_ | ||
#define CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_PERMISSION_CONTEXT_H_ | ||
|
||
#include "base/macros.h" | ||
#include "chrome/browser/permissions/permission_context_base.h" | ||
|
||
class Profile; | ||
|
||
// Manages user permissions for background sync. The context is scoped to the | ||
// requesting origin, which should always be equal to the top-level origin as | ||
// background syncs can only be requested from top-level pages. | ||
// The permission status is ALLOW by default and can be changed globally or on a | ||
// per-site basis from the content settings page. The user is not prompted for | ||
// permission. | ||
// TODO(nsatragno): actually implement the UI to allow changing the setting. | ||
class BackgroundSyncPermissionContext : public PermissionContextBase { | ||
public: | ||
explicit BackgroundSyncPermissionContext(Profile* profile); | ||
~BackgroundSyncPermissionContext() override = default; | ||
|
||
// PermissionContextBase: | ||
void CancelPermissionRequest(content::WebContents* web_contents, | ||
const PermissionRequestID& id) override; | ||
|
||
private: | ||
// PermissionContextBase: | ||
void DecidePermission(content::WebContents* web_contents, | ||
const PermissionRequestID& id, | ||
const GURL& requesting_origin, | ||
const GURL& embedding_origin, | ||
const BrowserPermissionCallback& callback) override; | ||
bool IsRestrictedToSecureOrigins() const override; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncPermissionContext); | ||
}; | ||
|
||
#endif // CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_PERMISSION_CONTEXT_H_ |
32 changes: 32 additions & 0 deletions
32
chrome/browser/background_sync/background_sync_permission_context_factory.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/background_sync/background_sync_permission_context_factory.h" | ||
|
||
#include "chrome/browser/background_sync/background_sync_permission_context.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "components/keyed_service/content/browser_context_dependency_manager.h" | ||
|
||
// static | ||
BackgroundSyncPermissionContext* | ||
BackgroundSyncPermissionContextFactory::GetForProfile(Profile* profile) { | ||
return static_cast<BackgroundSyncPermissionContext*>( | ||
GetInstance()->GetServiceForBrowserContext(profile, true /* create */)); | ||
} | ||
|
||
// static | ||
BackgroundSyncPermissionContextFactory* | ||
BackgroundSyncPermissionContextFactory::GetInstance() { | ||
return base::Singleton<BackgroundSyncPermissionContextFactory>::get(); | ||
} | ||
|
||
BackgroundSyncPermissionContextFactory::BackgroundSyncPermissionContextFactory() | ||
: PermissionContextFactoryBase( | ||
"BackgroundSyncPermissionContext", | ||
BrowserContextDependencyManager::GetInstance()) {} | ||
|
||
KeyedService* BackgroundSyncPermissionContextFactory::BuildServiceInstanceFor( | ||
content::BrowserContext* profile) const { | ||
return new BackgroundSyncPermissionContext(static_cast<Profile*>(profile)); | ||
} |
35 changes: 35 additions & 0 deletions
35
chrome/browser/background_sync/background_sync_permission_context_factory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_PERMISSION_CONTEXT_FACTORY_H_ | ||
#define CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_PERMISSION_CONTEXT_FACTORY_H_ | ||
|
||
#include "base/macros.h" | ||
#include "base/memory/singleton.h" | ||
#include "chrome/browser/permissions/permission_context_factory_base.h" | ||
|
||
class BackgroundSyncPermissionContext; | ||
class Profile; | ||
|
||
class BackgroundSyncPermissionContextFactory | ||
: public PermissionContextFactoryBase { | ||
public: | ||
static BackgroundSyncPermissionContext* GetForProfile(Profile* profile); | ||
static BackgroundSyncPermissionContextFactory* GetInstance(); | ||
|
||
private: | ||
friend struct base::DefaultSingletonTraits< | ||
BackgroundSyncPermissionContextFactory>; | ||
|
||
BackgroundSyncPermissionContextFactory(); | ||
~BackgroundSyncPermissionContextFactory() override = default; | ||
|
||
// BrowserContextKeyedBaseFactory methods: | ||
KeyedService* BuildServiceInstanceFor( | ||
content::BrowserContext* profile) const override; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncPermissionContextFactory); | ||
}; | ||
|
||
#endif // CHROME_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_PERMISSION_CONTEXT_FACTORY_H_ |
103 changes: 103 additions & 0 deletions
103
chrome/browser/background_sync/background_sync_permission_context_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2016 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/background_sync/background_sync_permission_context.h" | ||
|
||
#include <string> | ||
|
||
#include "base/bind.h" | ||
#include "base/macros.h" | ||
#include "base/run_loop.h" | ||
#include "chrome/browser/content_settings/host_content_settings_map_factory.h" | ||
#include "chrome/browser/permissions/permission_request_id.h" | ||
#include "chrome/test/base/chrome_render_view_host_test_harness.h" | ||
#include "chrome/test/base/testing_profile.h" | ||
#include "components/content_settings/core/browser/host_content_settings_map.h" | ||
#include "components/content_settings/core/common/content_settings.h" | ||
#include "components/content_settings/core/common/content_settings_pattern.h" | ||
#include "components/content_settings/core/common/content_settings_types.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "content/public/test/mock_render_process_host.h" | ||
#include "content/public/test/web_contents_tester.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
class BackgroundSyncPermissionContextTest | ||
: public ChromeRenderViewHostTestHarness { | ||
protected: | ||
BackgroundSyncPermissionContextTest() = default; | ||
|
||
~BackgroundSyncPermissionContextTest() override = default; | ||
|
||
void NavigateAndRequestPermission( | ||
const GURL& url, | ||
BackgroundSyncPermissionContext* permission_context) { | ||
content::WebContentsTester::For(web_contents())->NavigateAndCommit(url); | ||
|
||
base::RunLoop run_loop; | ||
|
||
const PermissionRequestID id( | ||
web_contents()->GetRenderProcessHost()->GetID(), | ||
web_contents()->GetMainFrame()->GetRoutingID(), -1 /* request_id */); | ||
permission_context->RequestPermission( | ||
web_contents(), id, url, | ||
base::Bind( | ||
&BackgroundSyncPermissionContextTest::TrackPermissionDecision, | ||
base::Unretained(this), run_loop.QuitClosure())); | ||
|
||
run_loop.Run(); | ||
} | ||
|
||
void TrackPermissionDecision(base::Closure done_closure, | ||
ContentSetting content_setting) { | ||
permission_granted_ = content_setting == CONTENT_SETTING_ALLOW; | ||
done_closure.Run(); | ||
} | ||
|
||
bool permission_granted() const { return permission_granted_; } | ||
|
||
private: | ||
bool permission_granted_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncPermissionContextTest); | ||
}; | ||
|
||
// Background sync permission should be allowed by default for a secure origin. | ||
TEST_F(BackgroundSyncPermissionContextTest, TestSecureRequestingUrl) { | ||
GURL url("https://www.example.com"); | ||
BackgroundSyncPermissionContext permission_context(profile()); | ||
|
||
NavigateAndRequestPermission(url, &permission_context); | ||
|
||
EXPECT_TRUE(permission_granted()); | ||
} | ||
|
||
// Background sync permission should be denied for an insecure origin. | ||
TEST_F(BackgroundSyncPermissionContextTest, TestInsecureRequestingUrl) { | ||
GURL url("http://example.com"); | ||
BackgroundSyncPermissionContext permission_context(profile()); | ||
|
||
NavigateAndRequestPermission(url, &permission_context); | ||
|
||
EXPECT_FALSE(permission_granted()); | ||
} | ||
|
||
// Tests that blocking one origin does not affect the others. | ||
TEST_F(BackgroundSyncPermissionContextTest, TestBlockOrigin) { | ||
GURL url1("https://www.example1.com"); | ||
GURL url2("https://www.example2.com"); | ||
BackgroundSyncPermissionContext permission_context(profile()); | ||
HostContentSettingsMapFactory::GetForProfile(profile())->SetContentSetting( | ||
ContentSettingsPattern::FromURLNoWildcard(url1), | ||
ContentSettingsPattern::FromURLNoWildcard(url1), | ||
CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC, std::string(), | ||
CONTENT_SETTING_BLOCK); | ||
|
||
NavigateAndRequestPermission(url1, &permission_context); | ||
|
||
EXPECT_FALSE(permission_granted()); | ||
|
||
NavigateAndRequestPermission(url2, &permission_context); | ||
|
||
EXPECT_TRUE(permission_granted()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.