OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/offline_pages/client_policy_controller.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "base/time/time.h" | |
11 #include "components/offline_pages/client_namespace_constants.h" | |
12 | |
13 using LifetimeType = offline_pages::LifetimePolicy::LifetimeType; | |
14 | |
15 namespace offline_pages { | |
16 | |
17 ClientPolicyController::ClientPolicyController() { | |
18 policies_.clear(); | |
19 // Manually defining client policies for bookmark and last_n. | |
20 policies_.insert(std::make_pair( | |
21 kBookmarkNamespace, | |
22 MakePolicy(kBookmarkNamespace, LifetimeType::TEMPORARY, | |
23 base::TimeDelta::FromDays(7), kUnlimitedPages, 1))); | |
24 policies_.insert(std::make_pair( | |
25 kLastNNamespace, | |
26 OfflinePageClientPolicyBuilder(kLastNNamespace, LifetimeType::TEMPORARY, | |
27 kUnlimitedPages, kUnlimitedPages) | |
28 .SetExpirePeriod(base::TimeDelta::FromDays(2)) | |
29 .SetIsSupportedByRecentTabs(true) | |
30 .SetIsOnlyShownInOriginalTab(true) | |
31 .Build())); | |
32 policies_.insert(std::make_pair( | |
33 kAsyncNamespace, | |
34 OfflinePageClientPolicyBuilder(kAsyncNamespace, LifetimeType::PERSISTENT, | |
35 kUnlimitedPages, kUnlimitedPages) | |
36 .SetIsSupportedByDownload(true) | |
37 .SetIsRemovedOnCacheReset(false) | |
38 .Build())); | |
39 policies_.insert(std::make_pair( | |
40 kCCTNamespace, | |
41 MakePolicy(kCCTNamespace, LifetimeType::TEMPORARY, | |
42 base::TimeDelta::FromDays(2), kUnlimitedPages, 1))); | |
43 policies_.insert(std::make_pair( | |
44 kDownloadNamespace, OfflinePageClientPolicyBuilder( | |
45 kDownloadNamespace, LifetimeType::PERSISTENT, | |
46 kUnlimitedPages, kUnlimitedPages) | |
47 .SetIsRemovedOnCacheReset(false) | |
48 .SetIsSupportedByDownload(true) | |
49 .Build())); | |
50 policies_.insert(std::make_pair( | |
51 kNTPSuggestionsNamespace, | |
52 OfflinePageClientPolicyBuilder(kNTPSuggestionsNamespace, | |
53 LifetimeType::PERSISTENT, kUnlimitedPages, | |
54 kUnlimitedPages) | |
55 .SetIsSupportedByDownload(true) | |
56 .Build())); | |
57 | |
58 // Fallback policy. | |
59 policies_.insert(std::make_pair( | |
60 kDefaultNamespace, MakePolicy(kDefaultNamespace, LifetimeType::TEMPORARY, | |
61 base::TimeDelta::FromDays(1), 10, 1))); | |
62 } | |
63 | |
64 ClientPolicyController::~ClientPolicyController() {} | |
65 | |
66 // static | |
67 const OfflinePageClientPolicy ClientPolicyController::MakePolicy( | |
68 const std::string& name_space, | |
69 LifetimeType lifetime_type, | |
70 const base::TimeDelta& expire_period, | |
71 size_t page_limit, | |
72 size_t pages_allowed_per_url) { | |
73 return OfflinePageClientPolicyBuilder(name_space, lifetime_type, page_limit, | |
74 pages_allowed_per_url) | |
75 .SetExpirePeriod(expire_period) | |
76 .Build(); | |
77 } | |
78 | |
79 const OfflinePageClientPolicy& ClientPolicyController::GetPolicy( | |
80 const std::string& name_space) const { | |
81 const auto& iter = policies_.find(name_space); | |
82 if (iter != policies_.end()) | |
83 return iter->second; | |
84 // Fallback when the namespace isn't defined. | |
85 return policies_.at(kDefaultNamespace); | |
86 } | |
87 | |
88 std::vector<std::string> ClientPolicyController::GetAllNamespaces() const { | |
89 std::vector<std::string> result; | |
90 for (const auto& policy_item : policies_) | |
91 result.emplace_back(policy_item.first); | |
92 | |
93 return result; | |
94 } | |
95 | |
96 bool ClientPolicyController::IsRemovedOnCacheReset( | |
97 const std::string& name_space) const { | |
98 return GetPolicy(name_space).feature_policy.is_removed_on_cache_reset; | |
99 } | |
100 | |
101 bool ClientPolicyController::IsSupportedByDownload( | |
102 const std::string& name_space) const { | |
103 return GetPolicy(name_space).feature_policy.is_supported_by_download; | |
104 } | |
105 | |
106 const std::vector<std::string>& | |
107 ClientPolicyController::GetNamespacesSupportedByDownload() const { | |
108 if (download_namespace_cache_) | |
109 return *download_namespace_cache_; | |
110 | |
111 download_namespace_cache_ = base::MakeUnique<std::vector<std::string>>(); | |
112 for (const auto& policy_item : policies_) { | |
113 if (policy_item.second.feature_policy.is_supported_by_download) | |
114 download_namespace_cache_->emplace_back(policy_item.first); | |
115 } | |
116 return *download_namespace_cache_; | |
117 } | |
118 | |
119 bool ClientPolicyController::IsShownAsRecentlyVisitedSite( | |
120 const std::string& name_space) const { | |
121 return GetPolicy(name_space).feature_policy.is_supported_by_recent_tabs; | |
122 } | |
123 | |
124 const std::vector<std::string>& | |
125 ClientPolicyController::GetNamespacesShownAsRecentlyVisitedSite() const { | |
126 if (recent_tab_namespace_cache_) | |
127 return *recent_tab_namespace_cache_; | |
128 | |
129 recent_tab_namespace_cache_ = base::MakeUnique<std::vector<std::string>>(); | |
130 for (const auto& policy_item : policies_) { | |
131 if (policy_item.second.feature_policy.is_supported_by_recent_tabs) | |
132 recent_tab_namespace_cache_->emplace_back(policy_item.first); | |
133 } | |
134 | |
135 return *recent_tab_namespace_cache_; | |
136 } | |
137 | |
138 bool ClientPolicyController::IsRestrictedToOriginalTab( | |
139 const std::string& name_space) const { | |
140 return GetPolicy(name_space).feature_policy.only_shown_in_original_tab; | |
141 } | |
142 | |
143 const std::vector<std::string>& | |
144 ClientPolicyController::GetNamespacesRestrictedToOriginalTab() const { | |
145 if (show_in_original_tab_cache_) | |
146 return *show_in_original_tab_cache_; | |
147 | |
148 show_in_original_tab_cache_ = base::MakeUnique<std::vector<std::string>>(); | |
149 for (const auto& policy_item : policies_) { | |
150 if (policy_item.second.feature_policy.only_shown_in_original_tab) | |
151 show_in_original_tab_cache_->emplace_back(policy_item.first); | |
152 } | |
153 | |
154 return *show_in_original_tab_cache_; | |
155 } | |
156 | |
157 void ClientPolicyController::AddPolicyForTest( | |
158 const std::string& name_space, | |
159 const OfflinePageClientPolicyBuilder& builder) { | |
160 policies_.insert(std::make_pair(name_space, builder.Build())); | |
161 } | |
162 | |
163 } // namespace offline_pages | |
OLD | NEW |