OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/supervised_user/supervised_user_interstitial.h" | 5 #include "chrome/browser/supervised_user/supervised_user_interstitial.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 #include "chrome/browser/ui/browser_finder.h" | 46 #include "chrome/browser/ui/browser_finder.h" |
47 #include "chrome/browser/ui/chrome_pages.h" | 47 #include "chrome/browser/ui/chrome_pages.h" |
48 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 48 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
49 #endif | 49 #endif |
50 | 50 |
51 using content::BrowserThread; | 51 using content::BrowserThread; |
52 using content::WebContents; | 52 using content::WebContents; |
53 | 53 |
54 namespace { | 54 namespace { |
55 | 55 |
56 static const int kAvatarSize1x = 45; | |
57 static const int kAvatarSize2x = 90; | |
58 | |
59 std::string BuildAvatarImageUrl(const std::string& url, int size) { | |
60 std::string result = url; | |
61 size_t slash = result.rfind('/'); | |
62 if (slash != std::string::npos) | |
63 result.insert(slash, "/s" + base::IntToString(size)); | |
64 return result; | |
65 } | |
66 | |
67 class TabCloser : public content::WebContentsUserData<TabCloser> { | 56 class TabCloser : public content::WebContentsUserData<TabCloser> { |
68 public: | 57 public: |
69 static void MaybeClose(WebContents* web_contents) { | 58 static void MaybeClose(WebContents* web_contents) { |
70 // Close the tab if there is no history entry to go back to and there is a | 59 // Close the tab if there is no history entry to go back to and there is a |
71 // browser for the tab (which is not the case for example in a <webview>). | 60 // browser for the tab (which is not the case for example in a <webview>). |
72 if (!web_contents->GetController().IsInitialBlankNavigation()) | 61 if (!web_contents->GetController().IsInitialBlankNavigation()) |
73 return; | 62 return; |
74 | 63 |
75 #if !defined(OS_ANDROID) | 64 #if !defined(OS_ANDROID) |
76 if (!chrome::FindBrowserWithWebContents(web_contents)) | 65 if (!chrome::FindBrowserWithWebContents(web_contents)) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser); | 106 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser); |
118 | 107 |
119 content::InterstitialPageDelegate::TypeID | 108 content::InterstitialPageDelegate::TypeID |
120 SupervisedUserInterstitial::kTypeForTesting = | 109 SupervisedUserInterstitial::kTypeForTesting = |
121 &SupervisedUserInterstitial::kTypeForTesting; | 110 &SupervisedUserInterstitial::kTypeForTesting; |
122 | 111 |
123 // static | 112 // static |
124 void SupervisedUserInterstitial::Show( | 113 void SupervisedUserInterstitial::Show( |
125 WebContents* web_contents, | 114 WebContents* web_contents, |
126 const GURL& url, | 115 const GURL& url, |
127 SupervisedUserURLFilter::FilteringBehaviorReason reason, | 116 supervised_user_error_page::FilteringBehaviorReason reason, |
128 const base::Callback<void(bool)>& callback) { | 117 const base::Callback<void(bool)>& callback) { |
129 SupervisedUserInterstitial* interstitial = | 118 SupervisedUserInterstitial* interstitial = |
130 new SupervisedUserInterstitial(web_contents, url, reason, callback); | 119 new SupervisedUserInterstitial(web_contents, url, reason, callback); |
131 | 120 |
132 // If Init() does not complete fully, immediately delete the interstitial. | 121 // If Init() does not complete fully, immediately delete the interstitial. |
133 if (!interstitial->Init()) | 122 if (!interstitial->Init()) |
134 delete interstitial; | 123 delete interstitial; |
135 // Otherwise |interstitial_page_| is responsible for deleting it. | 124 // Otherwise |interstitial_page_| is responsible for deleting it. |
136 } | 125 } |
137 | 126 |
138 SupervisedUserInterstitial::SupervisedUserInterstitial( | 127 SupervisedUserInterstitial::SupervisedUserInterstitial( |
139 WebContents* web_contents, | 128 WebContents* web_contents, |
140 const GURL& url, | 129 const GURL& url, |
141 SupervisedUserURLFilter::FilteringBehaviorReason reason, | 130 supervised_user_error_page::FilteringBehaviorReason reason, |
142 const base::Callback<void(bool)>& callback) | 131 const base::Callback<void(bool)>& callback) |
143 : web_contents_(web_contents), | 132 : web_contents_(web_contents), |
144 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), | 133 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), |
145 interstitial_page_(NULL), | 134 interstitial_page_(NULL), |
146 url_(url), | 135 url_(url), |
147 reason_(reason), | 136 reason_(reason), |
148 callback_(callback), | 137 callback_(callback), |
149 weak_ptr_factory_(this) {} | 138 weak_ptr_factory_(this) {} |
150 | 139 |
151 SupervisedUserInterstitial::~SupervisedUserInterstitial() { | 140 SupervisedUserInterstitial::~SupervisedUserInterstitial() { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 interstitial_page_ = | 183 interstitial_page_ = |
195 content::InterstitialPage::Create(web_contents_, true, url_, this); | 184 content::InterstitialPage::Create(web_contents_, true, url_, this); |
196 interstitial_page_->Show(); | 185 interstitial_page_->Show(); |
197 | 186 |
198 return true; | 187 return true; |
199 } | 188 } |
200 | 189 |
201 // static | 190 // static |
202 std::string SupervisedUserInterstitial::GetHTMLContents( | 191 std::string SupervisedUserInterstitial::GetHTMLContents( |
203 Profile* profile, | 192 Profile* profile, |
204 SupervisedUserURLFilter::FilteringBehaviorReason reason) { | 193 supervised_user_error_page::FilteringBehaviorReason reason) { |
205 base::DictionaryValue strings; | 194 bool is_child_account = profile->IsChild(); |
206 strings.SetString("blockPageTitle", | |
207 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE)); | |
208 | 195 |
209 SupervisedUserService* supervised_user_service = | 196 SupervisedUserService* supervised_user_service = |
210 SupervisedUserServiceFactory::GetForProfile(profile); | 197 SupervisedUserServiceFactory::GetForProfile(profile); |
211 | 198 |
212 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled(); | |
213 strings.SetBoolean("allowAccessRequests", allow_access_requests); | |
214 | |
215 std::string profile_image_url = profile->GetPrefs()->GetString( | |
216 prefs::kSupervisedUserCustodianProfileImageURL); | |
217 strings.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url, | |
218 kAvatarSize1x)); | |
219 strings.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url, | |
220 kAvatarSize2x)); | |
221 | |
222 std::string profile_image_url2 = profile->GetPrefs()->GetString( | |
223 prefs::kSupervisedUserSecondCustodianProfileImageURL); | |
224 strings.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2, | |
225 kAvatarSize1x)); | |
226 strings.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2, | |
227 kAvatarSize2x)); | |
228 | |
229 bool is_child_account = profile->IsChild(); | |
230 | |
231 base::string16 custodian = | 199 base::string16 custodian = |
232 base::UTF8ToUTF16(supervised_user_service->GetCustodianName()); | 200 base::UTF8ToUTF16(supervised_user_service->GetCustodianName()); |
233 base::string16 second_custodian = | 201 base::string16 second_custodian = |
234 base::UTF8ToUTF16(supervised_user_service->GetSecondCustodianName()); | 202 base::UTF8ToUTF16(supervised_user_service->GetSecondCustodianName()); |
235 base::string16 custodian_email = | 203 base::string16 custodian_email = |
236 base::UTF8ToUTF16(supervised_user_service->GetCustodianEmailAddress()); | 204 base::UTF8ToUTF16(supervised_user_service->GetCustodianEmailAddress()); |
237 base::string16 second_custodian_email = base::UTF8ToUTF16( | 205 base::string16 second_custodian_email = base::UTF8ToUTF16( |
238 supervised_user_service->GetSecondCustodianEmailAddress()); | 206 supervised_user_service->GetSecondCustodianEmailAddress()); |
239 strings.SetString("custodianName", custodian); | 207 std::string profile_image_url = profile->GetPrefs()->GetString( |
240 strings.SetString("custodianEmail", custodian_email); | 208 prefs::kSupervisedUserCustodianProfileImageURL); |
241 strings.SetString("secondCustodianName", second_custodian); | 209 std::string profile_image_url2 = profile->GetPrefs()->GetString( |
242 strings.SetString("secondCustodianEmail", second_custodian_email); | 210 prefs::kSupervisedUserSecondCustodianProfileImageURL); |
243 | 211 |
244 base::string16 block_message; | 212 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled(); |
245 if (allow_access_requests) { | |
246 if (is_child_account) { | |
247 block_message = l10n_util::GetStringUTF16( | |
248 second_custodian.empty() | |
249 ? IDS_CHILD_BLOCK_INTERSTITIAL_MESSAGE_SINGLE_PARENT | |
250 : IDS_CHILD_BLOCK_INTERSTITIAL_MESSAGE_MULTI_PARENT); | |
251 } else { | |
252 block_message = l10n_util::GetStringFUTF16( | |
253 IDS_BLOCK_INTERSTITIAL_MESSAGE, custodian); | |
254 } | |
255 } else { | |
256 block_message = l10n_util::GetStringUTF16( | |
257 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED); | |
258 } | |
259 strings.SetString("blockPageMessage", block_message); | |
260 strings.SetString("blockReasonMessage", l10n_util::GetStringUTF16( | |
261 SupervisedUserURLFilter::GetBlockMessageID( | |
262 reason, is_child_account, second_custodian.empty()))); | |
263 strings.SetString("blockReasonHeader", l10n_util::GetStringUTF16( | |
264 SupervisedUserURLFilter::GetBlockHeaderID(reason))); | |
265 bool show_feedback = false; | |
266 #if defined(GOOGLE_CHROME_BUILD) | |
267 show_feedback = | |
268 is_child_account && SupervisedUserURLFilter::ReasonIsAutomatic(reason); | |
269 #endif | |
270 strings.SetBoolean("showFeedbackLink", show_feedback); | |
271 strings.SetString("feedbackLink", | |
272 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_SEND_FEEDBACK)); | |
273 | 213 |
274 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON)); | 214 return supervised_user_error_page::BuildHtml( |
275 strings.SetString("requestAccessButton", l10n_util::GetStringUTF16( | 215 allow_access_requests, profile_image_url, profile_image_url2, custodian, |
276 IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON)); | 216 custodian_email, second_custodian, second_custodian_email, |
277 | 217 is_child_account, reason, g_browser_process->GetApplicationLocale()); |
278 strings.SetString("showDetailsLink", l10n_util::GetStringUTF16( | |
279 IDS_BLOCK_INTERSTITIAL_SHOW_DETAILS)); | |
280 strings.SetString("hideDetailsLink", l10n_util::GetStringUTF16( | |
281 IDS_BLOCK_INTERSTITIAL_HIDE_DETAILS)); | |
282 | |
283 base::string16 request_sent_message; | |
284 base::string16 request_failed_message; | |
285 if (is_child_account) { | |
286 if (second_custodian.empty()) { | |
287 request_sent_message = l10n_util::GetStringUTF16( | |
288 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE_SINGLE_PARENT); | |
289 request_failed_message = l10n_util::GetStringUTF16( | |
290 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_FAILED_MESSAGE_SINGLE_PARENT); | |
291 } else { | |
292 request_sent_message = l10n_util::GetStringUTF16( | |
293 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE_MULTI_PARENT); | |
294 request_failed_message = l10n_util::GetStringUTF16( | |
295 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_FAILED_MESSAGE_MULTI_PARENT); | |
296 } | |
297 } else { | |
298 request_sent_message = l10n_util::GetStringFUTF16( | |
299 IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE, custodian); | |
300 request_failed_message = l10n_util::GetStringFUTF16( | |
301 IDS_BLOCK_INTERSTITIAL_REQUEST_FAILED_MESSAGE, custodian); | |
302 } | |
303 strings.SetString("requestSentMessage", request_sent_message); | |
304 strings.SetString("requestFailedMessage", request_failed_message); | |
305 | |
306 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | |
307 webui::SetLoadTimeDataDefaults(app_locale, &strings); | |
308 | |
309 std::string html = | |
310 ResourceBundle::GetSharedInstance() | |
311 .GetRawDataResource(IDR_SUPERVISED_USER_BLOCK_INTERSTITIAL_HTML) | |
312 .as_string(); | |
313 webui::AppendWebUiCssTextDefaults(&html); | |
314 | |
315 return webui::GetI18nTemplateHtml(html, &strings); | |
316 } | 218 } |
317 | 219 |
318 std::string SupervisedUserInterstitial::GetHTMLContents() { | 220 std::string SupervisedUserInterstitial::GetHTMLContents() { |
319 return GetHTMLContents(profile_, reason_); | 221 return GetHTMLContents(profile_, reason_); |
320 } | 222 } |
321 | 223 |
322 void SupervisedUserInterstitial::CommandReceived(const std::string& command) { | 224 void SupervisedUserInterstitial::CommandReceived(const std::string& command) { |
323 // For use in histograms. | 225 // For use in histograms. |
324 enum Commands { | 226 enum Commands { |
325 PREVIEW, | 227 PREVIEW, |
(...skipping 30 matching lines...) Expand all Loading... |
356 weak_ptr_factory_.GetWeakPtr())); | 258 weak_ptr_factory_.GetWeakPtr())); |
357 return; | 259 return; |
358 } | 260 } |
359 | 261 |
360 SupervisedUserService* supervised_user_service = | 262 SupervisedUserService* supervised_user_service = |
361 SupervisedUserServiceFactory::GetForProfile(profile_); | 263 SupervisedUserServiceFactory::GetForProfile(profile_); |
362 base::string16 second_custodian = | 264 base::string16 second_custodian = |
363 base::UTF8ToUTF16(supervised_user_service->GetSecondCustodianName()); | 265 base::UTF8ToUTF16(supervised_user_service->GetSecondCustodianName()); |
364 | 266 |
365 if (command == "\"feedback\"") { | 267 if (command == "\"feedback\"") { |
366 base::string16 reason = l10n_util::GetStringUTF16( | 268 base::string16 reason = |
367 SupervisedUserURLFilter::GetBlockMessageID( | 269 l10n_util::GetStringUTF16(supervised_user_error_page::GetBlockMessageID( |
368 reason_, true, second_custodian.empty())); | 270 reason_, true, second_custodian.empty())); |
369 std::string message = l10n_util::GetStringFUTF8( | 271 std::string message = l10n_util::GetStringFUTF8( |
370 IDS_BLOCK_INTERSTITIAL_DEFAULT_FEEDBACK_TEXT, reason); | 272 IDS_BLOCK_INTERSTITIAL_DEFAULT_FEEDBACK_TEXT, reason); |
371 #if BUILDFLAG(ANDROID_JAVA_UI) | 273 #if BUILDFLAG(ANDROID_JAVA_UI) |
372 ReportChildAccountFeedback(web_contents_, message, url_); | 274 ReportChildAccountFeedback(web_contents_, message, url_); |
373 #else | 275 #else |
374 chrome::ShowFeedbackPage(chrome::FindBrowserWithWebContents(web_contents_), | 276 chrome::ShowFeedbackPage(chrome::FindBrowserWithWebContents(web_contents_), |
375 message, std::string()); | 277 message, std::string()); |
376 #endif | 278 #endif |
377 return; | 279 return; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 supervised_user_service->RemoveObserver(this); | 331 supervised_user_service->RemoveObserver(this); |
430 | 332 |
431 if (!callback_.is_null()) | 333 if (!callback_.is_null()) |
432 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 334 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
433 base::Bind(callback_, continue_request)); | 335 base::Bind(callback_, continue_request)); |
434 | 336 |
435 // After this, the WebContents may be destroyed. Make sure we don't try to use | 337 // After this, the WebContents may be destroyed. Make sure we don't try to use |
436 // it again. | 338 // it again. |
437 web_contents_ = NULL; | 339 web_contents_ = NULL; |
438 } | 340 } |
OLD | NEW |