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/ui/webui/supervised_user_internals_message_handler.h" | 5 #include "chrome/browser/ui/webui/supervised_user_internals_message_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/signin/account_tracker_service_factory.h" | 17 #include "chrome/browser/signin/account_tracker_service_factory.h" |
18 #include "chrome/browser/supervised_user/child_accounts/child_account_service.h" | 18 #include "chrome/browser/supervised_user/child_accounts/child_account_service.h" |
19 #include "chrome/browser/supervised_user/supervised_user_service.h" | 19 #include "chrome/browser/supervised_user/supervised_user_service.h" |
20 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 20 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
21 #include "chrome/browser/supervised_user/supervised_user_settings_service.h" | 21 #include "chrome/browser/supervised_user/supervised_user_settings_service.h" |
22 #include "chrome/browser/supervised_user/supervised_user_settings_service_factor
y.h" | 22 #include "chrome/browser/supervised_user/supervised_user_settings_service_factor
y.h" |
| 23 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" |
23 #include "chrome/common/channel_info.h" | 24 #include "chrome/common/channel_info.h" |
24 #include "components/signin/core/browser/account_tracker_service.h" | 25 #include "components/signin/core/browser/account_tracker_service.h" |
25 #include "components/supervised_user_error_page/supervised_user_error_page.h" | 26 #include "components/supervised_user_error_page/supervised_user_error_page.h" |
26 #include "components/url_formatter/url_fixer.h" | 27 #include "components/url_formatter/url_fixer.h" |
27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/web_ui.h" | 29 #include "content/public/browser/web_ui.h" |
29 | 30 |
30 using content::BrowserThread; | 31 using content::BrowserThread; |
31 | 32 |
32 namespace { | 33 namespace { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 return "Whitelist"; | 108 return "Whitelist"; |
108 case supervised_user_error_page::NOT_SIGNED_IN: | 109 case supervised_user_error_page::NOT_SIGNED_IN: |
109 // Should never happen, only used for requests from WebView | 110 // Should never happen, only used for requests from WebView |
110 NOTREACHED(); | 111 NOTREACHED(); |
111 } | 112 } |
112 return "Unknown/invalid"; | 113 return "Unknown/invalid"; |
113 } | 114 } |
114 | 115 |
115 } // namespace | 116 } // namespace |
116 | 117 |
117 // Helper class that lives on the IO thread, listens to the | |
118 // SupervisedUserURLFilter there, and posts results back to the UI thread. | |
119 class SupervisedUserInternalsMessageHandler::IOThreadHelper | |
120 : public base::RefCountedThreadSafe<IOThreadHelper, | |
121 BrowserThread::DeleteOnIOThread>, | |
122 public SupervisedUserURLFilter::Observer { | |
123 public: | |
124 using OnURLCheckedCallback = | |
125 base::Callback<void(const GURL&, | |
126 SupervisedUserURLFilter::FilteringBehavior, | |
127 supervised_user_error_page::FilteringBehaviorReason, | |
128 bool uncertain)>; | |
129 | |
130 IOThreadHelper(scoped_refptr<const SupervisedUserURLFilter> filter, | |
131 const OnURLCheckedCallback& callback) | |
132 : filter_(filter), callback_(callback) { | |
133 BrowserThread::PostTask(BrowserThread::IO, | |
134 FROM_HERE, | |
135 base::Bind(&IOThreadHelper::InitOnIOThread, this)); | |
136 } | |
137 | |
138 private: | |
139 friend class base::DeleteHelper<IOThreadHelper>; | |
140 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; | |
141 virtual ~IOThreadHelper() { | |
142 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
143 filter_->RemoveObserver(this); | |
144 } | |
145 | |
146 // SupervisedUserURLFilter::Observer: | |
147 void OnSiteListUpdated() override {} | |
148 void OnURLChecked(const GURL& url, | |
149 SupervisedUserURLFilter::FilteringBehavior behavior, | |
150 supervised_user_error_page::FilteringBehaviorReason reason, | |
151 bool uncertain) override { | |
152 BrowserThread::PostTask(BrowserThread::UI, | |
153 FROM_HERE, | |
154 base::Bind(callback_, | |
155 url, behavior, reason, uncertain)); | |
156 } | |
157 | |
158 void InitOnIOThread() { | |
159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
160 filter_->AddObserver(this); | |
161 } | |
162 | |
163 scoped_refptr<const SupervisedUserURLFilter> filter_; | |
164 OnURLCheckedCallback callback_; | |
165 | |
166 DISALLOW_COPY_AND_ASSIGN(IOThreadHelper); | |
167 }; | |
168 | |
169 SupervisedUserInternalsMessageHandler::SupervisedUserInternalsMessageHandler() | 118 SupervisedUserInternalsMessageHandler::SupervisedUserInternalsMessageHandler() |
170 : weak_factory_(this) { | 119 : is_watching_filter_(false), weak_factory_(this) {} |
171 } | |
172 | 120 |
173 SupervisedUserInternalsMessageHandler:: | 121 SupervisedUserInternalsMessageHandler:: |
174 ~SupervisedUserInternalsMessageHandler() { | 122 ~SupervisedUserInternalsMessageHandler() { |
| 123 if (is_watching_filter_) |
| 124 GetSupervisedUserService()->GetURLFilter()->RemoveObserver(this); |
175 } | 125 } |
176 | 126 |
177 void SupervisedUserInternalsMessageHandler::RegisterMessages() { | 127 void SupervisedUserInternalsMessageHandler::RegisterMessages() { |
178 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
179 | 129 |
180 web_ui()->RegisterMessageCallback("registerForEvents", | 130 web_ui()->RegisterMessageCallback("registerForEvents", |
181 base::Bind(&SupervisedUserInternalsMessageHandler:: | 131 base::Bind(&SupervisedUserInternalsMessageHandler:: |
182 HandleRegisterForEvents, | 132 HandleRegisterForEvents, |
183 base::Unretained(this))); | 133 base::Unretained(this))); |
184 | 134 |
(...skipping 13 matching lines...) Expand all Loading... |
198 SupervisedUserService* | 148 SupervisedUserService* |
199 SupervisedUserInternalsMessageHandler::GetSupervisedUserService() { | 149 SupervisedUserInternalsMessageHandler::GetSupervisedUserService() { |
200 Profile* profile = Profile::FromWebUI(web_ui()); | 150 Profile* profile = Profile::FromWebUI(web_ui()); |
201 return SupervisedUserServiceFactory::GetForProfile( | 151 return SupervisedUserServiceFactory::GetForProfile( |
202 profile->GetOriginalProfile()); | 152 profile->GetOriginalProfile()); |
203 } | 153 } |
204 | 154 |
205 void SupervisedUserInternalsMessageHandler::HandleRegisterForEvents( | 155 void SupervisedUserInternalsMessageHandler::HandleRegisterForEvents( |
206 const base::ListValue* args) { | 156 const base::ListValue* args) { |
207 DCHECK(args->empty()); | 157 DCHECK(args->empty()); |
| 158 if (is_watching_filter_) |
| 159 return; |
208 | 160 |
209 if (!io_thread_helper_.get()) { | 161 is_watching_filter_ = true; |
210 io_thread_helper_ = new IOThreadHelper( | 162 GetSupervisedUserService()->GetURLFilter()->AddObserver(this); |
211 GetSupervisedUserService()->GetURLFilterForIOThread(), | |
212 base::Bind(&SupervisedUserInternalsMessageHandler::OnURLChecked, | |
213 weak_factory_.GetWeakPtr())); | |
214 } | |
215 } | 163 } |
216 | 164 |
217 void SupervisedUserInternalsMessageHandler::HandleGetBasicInfo( | 165 void SupervisedUserInternalsMessageHandler::HandleGetBasicInfo( |
218 const base::ListValue* args) { | 166 const base::ListValue* args) { |
219 SendBasicInfo(); | 167 SendBasicInfo(); |
220 } | 168 } |
221 | 169 |
222 void SupervisedUserInternalsMessageHandler::HandleTryURL( | 170 void SupervisedUserInternalsMessageHandler::HandleTryURL( |
223 const base::ListValue* args) { | 171 const base::ListValue* args) { |
224 DCHECK_EQ(1u, args->GetSize()); | 172 DCHECK_EQ(1u, args->GetSize()); |
225 std::string url_str; | 173 std::string url_str; |
226 if (!args->GetString(0, &url_str)) | 174 if (!args->GetString(0, &url_str)) |
227 return; | 175 return; |
228 | 176 |
229 GURL url = url_formatter::FixupURL(url_str, std::string()); | 177 GURL url = url_formatter::FixupURL(url_str, std::string()); |
230 if (!url.is_valid()) | 178 if (!url.is_valid()) |
231 return; | 179 return; |
232 | 180 |
233 SupervisedUserURLFilter* filter = | 181 SupervisedUserURLFilter* filter = GetSupervisedUserService()->GetURLFilter(); |
234 GetSupervisedUserService()->GetURLFilterForUIThread(); | |
235 std::map<std::string, base::string16> whitelists = | 182 std::map<std::string, base::string16> whitelists = |
236 filter->GetMatchingWhitelistTitles(url); | 183 filter->GetMatchingWhitelistTitles(url); |
237 filter->GetFilteringBehaviorForURLWithAsyncChecks( | 184 filter->GetFilteringBehaviorForURLWithAsyncChecks( |
238 url, base::Bind(&SupervisedUserInternalsMessageHandler::OnTryURLResult, | 185 url, base::Bind(&SupervisedUserInternalsMessageHandler::OnTryURLResult, |
239 weak_factory_.GetWeakPtr(), whitelists)); | 186 weak_factory_.GetWeakPtr(), whitelists)); |
240 } | 187 } |
241 | 188 |
242 void SupervisedUserInternalsMessageHandler::SendBasicInfo() { | 189 void SupervisedUserInternalsMessageHandler::SendBasicInfo() { |
243 std::unique_ptr<base::ListValue> section_list(new base::ListValue); | 190 std::unique_ptr<base::ListValue> section_list(new base::ListValue); |
244 | 191 |
245 base::ListValue* section_general = AddSection(section_list.get(), "General"); | 192 base::ListValue* section_general = AddSection(section_list.get(), "General"); |
246 AddSectionEntry(section_general, "Chrome version", | 193 AddSectionEntry(section_general, "Chrome version", |
247 chrome::GetVersionString()); | 194 chrome::GetVersionString()); |
248 AddSectionEntry(section_general, "Child detection enabled", | 195 AddSectionEntry(section_general, "Child detection enabled", |
249 ChildAccountService::IsChildAccountDetectionEnabled()); | 196 ChildAccountService::IsChildAccountDetectionEnabled()); |
250 | 197 |
251 Profile* profile = Profile::FromWebUI(web_ui()); | 198 Profile* profile = Profile::FromWebUI(web_ui()); |
252 | 199 |
253 base::ListValue* section_profile = AddSection(section_list.get(), "Profile"); | 200 base::ListValue* section_profile = AddSection(section_list.get(), "Profile"); |
254 AddSectionEntry(section_profile, "Account", profile->GetProfileUserName()); | 201 AddSectionEntry(section_profile, "Account", profile->GetProfileUserName()); |
255 AddSectionEntry(section_profile, "Legacy Supervised", | 202 AddSectionEntry(section_profile, "Legacy Supervised", |
256 profile->IsLegacySupervised()); | 203 profile->IsLegacySupervised()); |
257 AddSectionEntry(section_profile, "Child", profile->IsChild()); | 204 AddSectionEntry(section_profile, "Child", profile->IsChild()); |
258 | 205 |
259 SupervisedUserURLFilter* filter = | 206 SupervisedUserURLFilter* filter = GetSupervisedUserService()->GetURLFilter(); |
260 GetSupervisedUserService()->GetURLFilterForUIThread(); | |
261 | 207 |
262 base::ListValue* section_filter = AddSection(section_list.get(), "Filter"); | 208 base::ListValue* section_filter = AddSection(section_list.get(), "Filter"); |
263 AddSectionEntry(section_filter, "Blacklist active", filter->HasBlacklist()); | 209 AddSectionEntry(section_filter, "Blacklist active", filter->HasBlacklist()); |
264 AddSectionEntry(section_filter, "Online checks active", | 210 AddSectionEntry(section_filter, "Online checks active", |
265 filter->HasAsyncURLChecker()); | 211 filter->HasAsyncURLChecker()); |
266 AddSectionEntry(section_filter, "Default behavior", | 212 AddSectionEntry(section_filter, "Default behavior", |
267 FilteringBehaviorToString( | 213 FilteringBehaviorToString( |
268 filter->GetDefaultFilteringBehavior())); | 214 filter->GetDefaultFilteringBehavior())); |
269 | 215 |
270 AccountTrackerService* account_tracker = | 216 AccountTrackerService* account_tracker = |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 base::DictionaryValue result; | 266 base::DictionaryValue result; |
321 result.SetString("allowResult", | 267 result.SetString("allowResult", |
322 FilteringBehaviorToString(behavior, uncertain)); | 268 FilteringBehaviorToString(behavior, uncertain)); |
323 result.SetBoolean("manual", reason == supervised_user_error_page::MANUAL && | 269 result.SetBoolean("manual", reason == supervised_user_error_page::MANUAL && |
324 behavior == SupervisedUserURLFilter::ALLOW); | 270 behavior == SupervisedUserURLFilter::ALLOW); |
325 result.SetString("whitelists", whitelists_str); | 271 result.SetString("whitelists", whitelists_str); |
326 web_ui()->CallJavascriptFunctionUnsafe( | 272 web_ui()->CallJavascriptFunctionUnsafe( |
327 "chrome.supervised_user_internals.receiveTryURLResult", result); | 273 "chrome.supervised_user_internals.receiveTryURLResult", result); |
328 } | 274 } |
329 | 275 |
| 276 void SupervisedUserInternalsMessageHandler::OnSiteListUpdated() {} |
| 277 |
330 void SupervisedUserInternalsMessageHandler::OnURLChecked( | 278 void SupervisedUserInternalsMessageHandler::OnURLChecked( |
331 const GURL& url, | 279 const GURL& url, |
332 SupervisedUserURLFilter::FilteringBehavior behavior, | 280 SupervisedUserURLFilter::FilteringBehavior behavior, |
333 supervised_user_error_page::FilteringBehaviorReason reason, | 281 supervised_user_error_page::FilteringBehaviorReason reason, |
334 bool uncertain) { | 282 bool uncertain) { |
335 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 283 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
336 base::DictionaryValue result; | 284 base::DictionaryValue result; |
337 result.SetString("url", url.possibly_invalid_spec()); | 285 result.SetString("url", url.possibly_invalid_spec()); |
338 result.SetString("result", FilteringBehaviorToString(behavior, uncertain)); | 286 result.SetString("result", FilteringBehaviorToString(behavior, uncertain)); |
339 result.SetString("reason", FilteringBehaviorReasonToString(reason)); | 287 result.SetString("reason", FilteringBehaviorReasonToString(reason)); |
340 web_ui()->CallJavascriptFunctionUnsafe( | 288 web_ui()->CallJavascriptFunctionUnsafe( |
341 "chrome.supervised_user_internals.receiveFilteringResult", result); | 289 "chrome.supervised_user_internals.receiveFilteringResult", result); |
342 } | 290 } |
OLD | NEW |