Commit 78525669 authored by Carlos Knippschild's avatar Carlos Knippschild Committed by Commit Bot

Clarify offline pages policy for tab restriction

Offline Pages namespaces can be configured to only allow its pages to be
presented in their original tabs. This is enforced using an undeclared
convention that pages for these namespaces will store the tab id value
in the ClientId::id field for their pages. This change makes this
convention more explicit.

Bug: 879801
Change-Id: I3ab3d871abb41d6de9112c55d8b3d8b6491c7386
Reviewed-on: https://chromium-review.googlesource.com/1200389
Commit-Queue: Carlos Knippschild <carlosk@chromium.org>
Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589369}
parent d03e9be2
...@@ -59,15 +59,17 @@ class OfflinePageComparer { ...@@ -59,15 +59,17 @@ class OfflinePageComparer {
void OnGetPagesByURLDone( void OnGetPagesByURLDone(
const GURL& url, const GURL& url,
int tab_id, int tab_id,
const std::vector<std::string>& namespaces_to_show_in_original_tab, const std::vector<std::string>& namespaces_restricted_to_tab_from_client_id,
base::OnceCallback<void(const std::vector<OfflinePageItem>&)> callback, base::OnceCallback<void(const std::vector<OfflinePageItem>&)> callback,
const MultipleOfflinePageItemResult& pages) { const MultipleOfflinePageItemResult& pages) {
std::vector<OfflinePageItem> selected_pages; std::vector<OfflinePageItem> selected_pages;
std::string tab_id_str = base::IntToString(tab_id); std::string tab_id_str = base::IntToString(tab_id);
// Exclude pages whose tab id does not match. // Exclude pages whose tab id does not match.
// Note: For this restriction to work offline pages saved to tab-bound
// namespaces must have the assigned tab id set to their ClientId::id field.
for (const auto& page : pages) { for (const auto& page : pages) {
if (base::ContainsValue(namespaces_to_show_in_original_tab, if (base::ContainsValue(namespaces_restricted_to_tab_from_client_id,
page.client_id.name_space) && page.client_id.name_space) &&
page.client_id.id != tab_id_str) { page.client_id.id != tab_id_str) {
continue; continue;
...@@ -215,7 +217,7 @@ void OfflinePageUtils::SelectPagesForURL( ...@@ -215,7 +217,7 @@ void OfflinePageUtils::SelectPagesForURL(
offline_page_model->GetPagesByURL( offline_page_model->GetPagesByURL(
url, base::BindOnce(&OnGetPagesByURLDone, url, tab_id, url, base::BindOnce(&OnGetPagesByURLDone, url, tab_id,
offline_page_model->GetPolicyController() offline_page_model->GetPolicyController()
->GetNamespacesRestrictedToOriginalTab(), ->GetNamespacesRestrictedToTabFromClientId(),
std::move(callback))); std::move(callback)));
} }
......
...@@ -27,7 +27,7 @@ ClientPolicyController::ClientPolicyController() { ...@@ -27,7 +27,7 @@ ClientPolicyController::ClientPolicyController() {
kUnlimitedPages, kUnlimitedPages) kUnlimitedPages, kUnlimitedPages)
.SetExpirePeriod(base::TimeDelta::FromDays(30)) .SetExpirePeriod(base::TimeDelta::FromDays(30))
.SetIsSupportedByRecentTabs(true) .SetIsSupportedByRecentTabs(true)
.SetIsOnlyShownInOriginalTab(true) .SetIsRestrictedToTabFromClientId(true)
.Build())); .Build()));
policies_.insert(std::make_pair( policies_.insert(std::make_pair(
kAsyncNamespace, kAsyncNamespace,
...@@ -89,7 +89,7 @@ ClientPolicyController::ClientPolicyController() { ...@@ -89,7 +89,7 @@ ClientPolicyController::ClientPolicyController() {
kUnlimitedPages, 1) kUnlimitedPages, 1)
.SetIsRemovedOnCacheReset(true) .SetIsRemovedOnCacheReset(true)
.SetExpirePeriod(base::TimeDelta::FromHours(1)) .SetExpirePeriod(base::TimeDelta::FromHours(1))
.SetIsOnlyShownInOriginalTab(true) .SetIsRestrictedToTabFromClientId(true)
.Build())); .Build()));
// Fallback policy. // Fallback policy.
...@@ -204,23 +204,25 @@ ClientPolicyController::GetNamespacesShownAsRecentlyVisitedSite() const { ...@@ -204,23 +204,25 @@ ClientPolicyController::GetNamespacesShownAsRecentlyVisitedSite() const {
return *recent_tab_namespace_cache_; return *recent_tab_namespace_cache_;
} }
bool ClientPolicyController::IsRestrictedToOriginalTab( bool ClientPolicyController::IsRestrictedToTabFromClientId(
const std::string& name_space) const { const std::string& name_space) const {
return GetPolicy(name_space).feature_policy.only_shown_in_original_tab; return GetPolicy(name_space)
.feature_policy.is_restricted_to_tab_from_client_id;
} }
const std::vector<std::string>& const std::vector<std::string>&
ClientPolicyController::GetNamespacesRestrictedToOriginalTab() const { ClientPolicyController::GetNamespacesRestrictedToTabFromClientId() const {
if (show_in_original_tab_cache_) if (restricted_to_tab_from_client_id_cache_)
return *show_in_original_tab_cache_; return *restricted_to_tab_from_client_id_cache_;
show_in_original_tab_cache_ = std::make_unique<std::vector<std::string>>(); restricted_to_tab_from_client_id_cache_ =
std::make_unique<std::vector<std::string>>();
for (const auto& policy_item : policies_) { for (const auto& policy_item : policies_) {
if (policy_item.second.feature_policy.only_shown_in_original_tab) if (policy_item.second.feature_policy.is_restricted_to_tab_from_client_id)
show_in_original_tab_cache_->emplace_back(policy_item.first); restricted_to_tab_from_client_id_cache_->emplace_back(policy_item.first);
} }
return *show_in_original_tab_cache_; return *restricted_to_tab_from_client_id_cache_;
} }
bool ClientPolicyController::IsDisabledWhenPrefetchDisabled( bool ClientPolicyController::IsDisabledWhenPrefetchDisabled(
......
...@@ -57,10 +57,13 @@ class ClientPolicyController { ...@@ -57,10 +57,13 @@ class ClientPolicyController {
const std::vector<std::string>& GetNamespacesShownAsRecentlyVisitedSite() const std::vector<std::string>& GetNamespacesShownAsRecentlyVisitedSite()
const; const;
// Returns whether pages for |name_space| should never be shown outside the // Returns whether pages for |name_space| should only be opened in a
// tab they were generated in. // specifically assigned tab.
bool IsRestrictedToOriginalTab(const std::string& name_space) const; // Note: For this restriction to work offline pages saved to this namespace
const std::vector<std::string>& GetNamespacesRestrictedToOriginalTab() const; // must have the respective tab id set to their ClientId::id field.
bool IsRestrictedToTabFromClientId(const std::string& name_space) const;
const std::vector<std::string>& GetNamespacesRestrictedToTabFromClientId()
const;
bool IsDisabledWhenPrefetchDisabled(const std::string& name_space) const; bool IsDisabledWhenPrefetchDisabled(const std::string& name_space) const;
const std::vector<std::string>& GetNamespacesDisabledWhenPrefetchDisabled() const std::vector<std::string>& GetNamespacesDisabledWhenPrefetchDisabled()
...@@ -89,7 +92,8 @@ class ClientPolicyController { ...@@ -89,7 +92,8 @@ class ClientPolicyController {
mutable std::unique_ptr<std::vector<std::string>> mutable std::unique_ptr<std::vector<std::string>>
user_requested_download_namespace_cache_; user_requested_download_namespace_cache_;
mutable std::unique_ptr<std::vector<std::string>> recent_tab_namespace_cache_; mutable std::unique_ptr<std::vector<std::string>> recent_tab_namespace_cache_;
mutable std::unique_ptr<std::vector<std::string>> show_in_original_tab_cache_; mutable std::unique_ptr<std::vector<std::string>>
restricted_to_tab_from_client_id_cache_;
mutable std::unique_ptr<std::vector<std::string>> mutable std::unique_ptr<std::vector<std::string>>
disabled_when_prefetch_disabled_cache_; disabled_when_prefetch_disabled_cache_;
......
...@@ -35,7 +35,8 @@ class ClientPolicyControllerTest : public testing::Test { ...@@ -35,7 +35,8 @@ class ClientPolicyControllerTest : public testing::Test {
void ExpectUserRequestedDownloadSupport(std::string name_space, void ExpectUserRequestedDownloadSupport(std::string name_space,
bool expectation); bool expectation);
void ExpectRecentTab(std::string name_space, bool expectation); void ExpectRecentTab(std::string name_space, bool expectation);
void ExpectOnlyOriginalTab(std::string name_space, bool expectation); void ExpectRestrictedToTabFromClientId(std::string name_space,
bool expectation);
void ExpectDisabledWhenPrefetchDisabled(std::string name_space, void ExpectDisabledWhenPrefetchDisabled(std::string name_space,
bool expectation); bool expectation);
...@@ -106,19 +107,21 @@ void ClientPolicyControllerTest::ExpectRecentTab(std::string name_space, ...@@ -106,19 +107,21 @@ void ClientPolicyControllerTest::ExpectRecentTab(std::string name_space,
" a recently visited site."; " a recently visited site.";
} }
void ClientPolicyControllerTest::ExpectOnlyOriginalTab(std::string name_space, void ClientPolicyControllerTest::ExpectRestrictedToTabFromClientId(
bool expectation) { std::string name_space,
bool expectation) {
std::vector<std::string> cache = std::vector<std::string> cache =
controller()->GetNamespacesRestrictedToOriginalTab(); controller()->GetNamespacesRestrictedToTabFromClientId();
auto result = std::find(cache.begin(), cache.end(), name_space); auto result = std::find(cache.begin(), cache.end(), name_space);
EXPECT_EQ(expectation, result != cache.end()) EXPECT_EQ(expectation, result != cache.end())
<< "Namespace " << name_space << "Namespace " << name_space
<< " had incorrect restriction when getting namespaces restricted to" << " had incorrect restriction when getting namespaces restricted to"
" the original tab"; " the tab from the client id field";
EXPECT_EQ(expectation, controller()->IsRestrictedToOriginalTab(name_space)) EXPECT_EQ(expectation,
controller()->IsRestrictedToTabFromClientId(name_space))
<< "Namespace " << name_space << "Namespace " << name_space
<< " had incorrect restriction when directly checking if the namespace" << " had incorrect restriction when directly checking if the namespace"
" is restricted to the original tab"; " is restricted to the tab from the client id field";
} }
void ClientPolicyControllerTest::ExpectDisabledWhenPrefetchDisabled( void ClientPolicyControllerTest::ExpectDisabledWhenPrefetchDisabled(
...@@ -147,7 +150,7 @@ TEST_F(ClientPolicyControllerTest, FallbackTest) { ...@@ -147,7 +150,7 @@ TEST_F(ClientPolicyControllerTest, FallbackTest) {
ExpectDownloadSupport(kUndefinedNamespace, false); ExpectDownloadSupport(kUndefinedNamespace, false);
ExpectUserRequestedDownloadSupport(kUndefinedNamespace, false); ExpectUserRequestedDownloadSupport(kUndefinedNamespace, false);
ExpectRecentTab(kUndefinedNamespace, false); ExpectRecentTab(kUndefinedNamespace, false);
ExpectOnlyOriginalTab(kUndefinedNamespace, false); ExpectRestrictedToTabFromClientId(kUndefinedNamespace, false);
ExpectDisabledWhenPrefetchDisabled(kUndefinedNamespace, false); ExpectDisabledWhenPrefetchDisabled(kUndefinedNamespace, false);
} }
...@@ -160,7 +163,7 @@ TEST_F(ClientPolicyControllerTest, CheckBookmarkDefined) { ...@@ -160,7 +163,7 @@ TEST_F(ClientPolicyControllerTest, CheckBookmarkDefined) {
ExpectDownloadSupport(kBookmarkNamespace, false); ExpectDownloadSupport(kBookmarkNamespace, false);
ExpectUserRequestedDownloadSupport(kBookmarkNamespace, false); ExpectUserRequestedDownloadSupport(kBookmarkNamespace, false);
ExpectRecentTab(kBookmarkNamespace, false); ExpectRecentTab(kBookmarkNamespace, false);
ExpectOnlyOriginalTab(kBookmarkNamespace, false); ExpectRestrictedToTabFromClientId(kBookmarkNamespace, false);
ExpectDisabledWhenPrefetchDisabled(kBookmarkNamespace, false); ExpectDisabledWhenPrefetchDisabled(kBookmarkNamespace, false);
} }
...@@ -173,7 +176,7 @@ TEST_F(ClientPolicyControllerTest, CheckLastNDefined) { ...@@ -173,7 +176,7 @@ TEST_F(ClientPolicyControllerTest, CheckLastNDefined) {
ExpectDownloadSupport(kLastNNamespace, false); ExpectDownloadSupport(kLastNNamespace, false);
ExpectUserRequestedDownloadSupport(kLastNNamespace, false); ExpectUserRequestedDownloadSupport(kLastNNamespace, false);
ExpectRecentTab(kLastNNamespace, true); ExpectRecentTab(kLastNNamespace, true);
ExpectOnlyOriginalTab(kLastNNamespace, true); ExpectRestrictedToTabFromClientId(kLastNNamespace, true);
ExpectDisabledWhenPrefetchDisabled(kLastNNamespace, false); ExpectDisabledWhenPrefetchDisabled(kLastNNamespace, false);
} }
...@@ -186,7 +189,7 @@ TEST_F(ClientPolicyControllerTest, CheckAsyncDefined) { ...@@ -186,7 +189,7 @@ TEST_F(ClientPolicyControllerTest, CheckAsyncDefined) {
ExpectDownloadSupport(kAsyncNamespace, true); ExpectDownloadSupport(kAsyncNamespace, true);
ExpectUserRequestedDownloadSupport(kAsyncNamespace, true); ExpectUserRequestedDownloadSupport(kAsyncNamespace, true);
ExpectRecentTab(kAsyncNamespace, false); ExpectRecentTab(kAsyncNamespace, false);
ExpectOnlyOriginalTab(kAsyncNamespace, false); ExpectRestrictedToTabFromClientId(kAsyncNamespace, false);
ExpectDisabledWhenPrefetchDisabled(kAsyncNamespace, false); ExpectDisabledWhenPrefetchDisabled(kAsyncNamespace, false);
} }
...@@ -199,7 +202,7 @@ TEST_F(ClientPolicyControllerTest, CheckCCTDefined) { ...@@ -199,7 +202,7 @@ TEST_F(ClientPolicyControllerTest, CheckCCTDefined) {
ExpectDownloadSupport(kCCTNamespace, false); ExpectDownloadSupport(kCCTNamespace, false);
ExpectUserRequestedDownloadSupport(kCCTNamespace, false); ExpectUserRequestedDownloadSupport(kCCTNamespace, false);
ExpectRecentTab(kCCTNamespace, false); ExpectRecentTab(kCCTNamespace, false);
ExpectOnlyOriginalTab(kCCTNamespace, false); ExpectRestrictedToTabFromClientId(kCCTNamespace, false);
ExpectDisabledWhenPrefetchDisabled(kCCTNamespace, true); ExpectDisabledWhenPrefetchDisabled(kCCTNamespace, true);
} }
...@@ -212,7 +215,7 @@ TEST_F(ClientPolicyControllerTest, CheckDownloadDefined) { ...@@ -212,7 +215,7 @@ TEST_F(ClientPolicyControllerTest, CheckDownloadDefined) {
ExpectDownloadSupport(kDownloadNamespace, true); ExpectDownloadSupport(kDownloadNamespace, true);
ExpectUserRequestedDownloadSupport(kDownloadNamespace, true); ExpectUserRequestedDownloadSupport(kDownloadNamespace, true);
ExpectRecentTab(kDownloadNamespace, false); ExpectRecentTab(kDownloadNamespace, false);
ExpectOnlyOriginalTab(kDownloadNamespace, false); ExpectRestrictedToTabFromClientId(kDownloadNamespace, false);
ExpectDisabledWhenPrefetchDisabled(kDownloadNamespace, false); ExpectDisabledWhenPrefetchDisabled(kDownloadNamespace, false);
} }
...@@ -226,7 +229,7 @@ TEST_F(ClientPolicyControllerTest, CheckNTPSuggestionsDefined) { ...@@ -226,7 +229,7 @@ TEST_F(ClientPolicyControllerTest, CheckNTPSuggestionsDefined) {
ExpectDownloadSupport(kNTPSuggestionsNamespace, true); ExpectDownloadSupport(kNTPSuggestionsNamespace, true);
ExpectUserRequestedDownloadSupport(kNTPSuggestionsNamespace, true); ExpectUserRequestedDownloadSupport(kNTPSuggestionsNamespace, true);
ExpectRecentTab(kNTPSuggestionsNamespace, false); ExpectRecentTab(kNTPSuggestionsNamespace, false);
ExpectOnlyOriginalTab(kNTPSuggestionsNamespace, false); ExpectRestrictedToTabFromClientId(kNTPSuggestionsNamespace, false);
ExpectDisabledWhenPrefetchDisabled(kNTPSuggestionsNamespace, false); ExpectDisabledWhenPrefetchDisabled(kNTPSuggestionsNamespace, false);
} }
...@@ -240,7 +243,7 @@ TEST_F(ClientPolicyControllerTest, CheckSuggestedArticlesDefined) { ...@@ -240,7 +243,7 @@ TEST_F(ClientPolicyControllerTest, CheckSuggestedArticlesDefined) {
ExpectDownloadSupport(kSuggestedArticlesNamespace, false); ExpectDownloadSupport(kSuggestedArticlesNamespace, false);
ExpectUserRequestedDownloadSupport(kSuggestedArticlesNamespace, false); ExpectUserRequestedDownloadSupport(kSuggestedArticlesNamespace, false);
ExpectRecentTab(kSuggestedArticlesNamespace, false); ExpectRecentTab(kSuggestedArticlesNamespace, false);
ExpectOnlyOriginalTab(kSuggestedArticlesNamespace, false); ExpectRestrictedToTabFromClientId(kSuggestedArticlesNamespace, false);
ExpectDisabledWhenPrefetchDisabled(kSuggestedArticlesNamespace, true); ExpectDisabledWhenPrefetchDisabled(kSuggestedArticlesNamespace, true);
} }
...@@ -254,7 +257,7 @@ TEST_F(ClientPolicyControllerTest, CheckLivePageSharingDefined) { ...@@ -254,7 +257,7 @@ TEST_F(ClientPolicyControllerTest, CheckLivePageSharingDefined) {
ExpectDownloadSupport(kLivePageSharingNamespace, false); ExpectDownloadSupport(kLivePageSharingNamespace, false);
ExpectUserRequestedDownloadSupport(kLivePageSharingNamespace, false); ExpectUserRequestedDownloadSupport(kLivePageSharingNamespace, false);
ExpectRecentTab(kLivePageSharingNamespace, false); ExpectRecentTab(kLivePageSharingNamespace, false);
ExpectOnlyOriginalTab(kLivePageSharingNamespace, true); ExpectRestrictedToTabFromClientId(kLivePageSharingNamespace, true);
ExpectDisabledWhenPrefetchDisabled(kLivePageSharingNamespace, false); ExpectDisabledWhenPrefetchDisabled(kLivePageSharingNamespace, false);
} }
......
...@@ -53,8 +53,10 @@ struct FeaturePolicy { ...@@ -53,8 +53,10 @@ struct FeaturePolicy {
bool is_user_requested_download; bool is_user_requested_download;
// Whether pages are shown in recent tabs ui. // Whether pages are shown in recent tabs ui.
bool is_supported_by_recent_tabs; bool is_supported_by_recent_tabs;
// Whether pages should only be viewed in the tab they were generated in. // Whether pages can only be viewed in a specific tab. Pages controlled by
bool only_shown_in_original_tab; // this policy must have their ClientId::id field set to their assigned tab's
// id.
bool is_restricted_to_tab_from_client_id;
// Whether pages are removed on user-initiated cache reset. Defaults to true. // Whether pages are removed on user-initiated cache reset. Defaults to true.
bool is_removed_on_cache_reset; bool is_removed_on_cache_reset;
// Whether the namespace should be disabled if prefetching-related preferences // Whether the namespace should be disabled if prefetching-related preferences
...@@ -69,7 +71,7 @@ struct FeaturePolicy { ...@@ -69,7 +71,7 @@ struct FeaturePolicy {
: is_supported_by_download(false), : is_supported_by_download(false),
is_user_requested_download(false), is_user_requested_download(false),
is_supported_by_recent_tabs(false), is_supported_by_recent_tabs(false),
only_shown_in_original_tab(false), is_restricted_to_tab_from_client_id(false),
is_removed_on_cache_reset(true), is_removed_on_cache_reset(true),
disabled_when_prefetch_disabled(false), disabled_when_prefetch_disabled(false),
is_suggested(false), is_suggested(false),
...@@ -157,10 +159,10 @@ class OfflinePageClientPolicyBuilder { ...@@ -157,10 +159,10 @@ class OfflinePageClientPolicyBuilder {
return *this; return *this;
} }
OfflinePageClientPolicyBuilder& SetIsOnlyShownInOriginalTab( OfflinePageClientPolicyBuilder& SetIsRestrictedToTabFromClientId(
const bool only_shown_in_original_tab) { const bool is_restricted_to_tab_from_client_id) {
policy_.feature_policy.only_shown_in_original_tab = policy_.feature_policy.is_restricted_to_tab_from_client_id =
only_shown_in_original_tab; is_restricted_to_tab_from_client_id;
return *this; return *this;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment