[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 5 | #include "chrome/browser/supervised_user/supervised_user_interstitial.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 6 | |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 9 | #include "base/memory/weak_ptr.h" |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 14 | #include "base/values.h" |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 15 | #include "build/build_config.h" |
dbeam | 622e9f3 | 2015-01-28 21:22:45 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 17 | #include "chrome/browser/infobars/infobar_service.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile.h" |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 19 | #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 20 | #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 21 | #include "chrome/common/features.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 22 | #include "chrome/common/pref_names.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 23 | #include "chrome/grit/generated_resources.h" |
[email protected] | 051655ad | 2014-04-18 15:09:41 | [diff] [blame] | 24 | #include "components/infobars/core/infobar.h" |
| 25 | #include "components/infobars/core/infobar_delegate.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 26 | #include "components/prefs/pref_service.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
| 28 | #include "content/public/browser/interstitial_page.h" |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 29 | #include "content/public/browser/navigation_controller.h" |
| 30 | #include "content/public/browser/navigation_details.h" |
| 31 | #include "content/public/browser/navigation_entry.h" |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 32 | #include "content/public/browser/render_frame_host.h" |
| 33 | #include "content/public/browser/render_view_host.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 34 | #include "content/public/browser/web_contents.h" |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 35 | #include "content/public/browser/web_contents_user_data.h" |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 36 | #include "content/public/browser/web_ui.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 37 | #include "grit/browser_resources.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 38 | #include "ui/base/l10n/l10n_util.h" |
| 39 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | cd67ed5 | 2013-10-15 01:22:13 | [diff] [blame] | 40 | #include "ui/base/webui/jstemplate_builder.h" |
| 41 | #include "ui/base/webui/web_ui_util.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 42 | |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 43 | #if BUILDFLAG(ANDROID_JAVA_UI) |
treib | 9dbb930 | 2015-01-08 14:42:39 | [diff] [blame] | 44 | #include "chrome/browser/supervised_user/child_accounts/child_account_feedback_reporter_android.h" |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 45 | #elif !defined(OS_ANDROID) |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 46 | #include "chrome/browser/ui/browser_finder.h" |
treib | 5dd8daf3 | 2014-12-18 17:36:37 | [diff] [blame] | 47 | #include "chrome/browser/ui/chrome_pages.h" |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 48 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 49 | #endif |
| 50 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 51 | using content::BrowserThread; |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 52 | using content::WebContents; |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 53 | |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 54 | namespace { |
| 55 | |
| 56 | static const int kAvatarSize1x = 45; |
| 57 | static const int kAvatarSize2x = 90; |
| 58 | |
treib | 885fee1 | 2014-08-29 08:07:58 | [diff] [blame] | 59 | std::string BuildAvatarImageUrl(const std::string& url, int size) { |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 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)); |
treib | 885fee1 | 2014-08-29 08:07:58 | [diff] [blame] | 64 | return result; |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 65 | } |
| 66 | |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 67 | class TabCloser : public content::WebContentsUserData<TabCloser> { |
bauerb | 34f34c5f | 2015-10-21 15:38:32 | [diff] [blame] | 68 | public: |
| 69 | static void MaybeClose(WebContents* web_contents) { |
| 70 | // 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>). |
| 72 | if (!web_contents->GetController().IsInitialBlankNavigation()) |
| 73 | return; |
| 74 | |
| 75 | #if !defined(OS_ANDROID) |
| 76 | if (!chrome::FindBrowserWithWebContents(web_contents)) |
| 77 | return; |
| 78 | #endif |
| 79 | TabCloser::CreateForWebContents(web_contents); |
| 80 | } |
| 81 | |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 82 | private: |
| 83 | friend class content::WebContentsUserData<TabCloser>; |
| 84 | |
| 85 | explicit TabCloser(WebContents* web_contents) |
| 86 | : web_contents_(web_contents), weak_ptr_factory_(this) { |
| 87 | BrowserThread::PostTask( |
| 88 | BrowserThread::UI, |
| 89 | FROM_HERE, |
| 90 | base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr())); |
| 91 | } |
dcheng | c072fff | 2014-10-21 11:39:05 | [diff] [blame] | 92 | ~TabCloser() override {} |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 93 | |
| 94 | void CloseTabImpl() { |
| 95 | // On Android, FindBrowserWithWebContents and TabStripModel don't exist. |
| 96 | #if !defined(OS_ANDROID) |
| 97 | Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
| 98 | DCHECK(browser); |
| 99 | TabStripModel* tab_strip = browser->tab_strip_model(); |
| 100 | DCHECK_NE(TabStripModel::kNoTab, |
| 101 | tab_strip->GetIndexOfWebContents(web_contents_)); |
| 102 | if (tab_strip->count() <= 1) { |
| 103 | // Don't close the last tab in the window. |
| 104 | web_contents_->RemoveUserData(UserDataKey()); |
| 105 | return; |
| 106 | } |
| 107 | #endif |
| 108 | web_contents_->Close(); |
| 109 | } |
| 110 | |
| 111 | WebContents* web_contents_; |
| 112 | base::WeakPtrFactory<TabCloser> weak_ptr_factory_; |
| 113 | }; |
| 114 | |
| 115 | } // namespace |
| 116 | |
| 117 | DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser); |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 118 | |
meacer | fc55bd5 | 2015-03-05 19:12:00 | [diff] [blame] | 119 | content::InterstitialPageDelegate::TypeID |
| 120 | SupervisedUserInterstitial::kTypeForTesting = |
| 121 | &SupervisedUserInterstitial::kTypeForTesting; |
| 122 | |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 123 | // static |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 124 | void SupervisedUserInterstitial::Show( |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 125 | WebContents* web_contents, |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 126 | const GURL& url, |
treib | 170c3f8 | 2014-12-10 19:13:25 | [diff] [blame] | 127 | SupervisedUserURLFilter::FilteringBehaviorReason reason, |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 128 | const base::Callback<void(bool)>& callback) { |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 129 | SupervisedUserInterstitial* interstitial = |
treib | 170c3f8 | 2014-12-10 19:13:25 | [diff] [blame] | 130 | new SupervisedUserInterstitial(web_contents, url, reason, callback); |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 131 | |
| 132 | // If Init() does not complete fully, immediately delete the interstitial. |
| 133 | if (!interstitial->Init()) |
| 134 | delete interstitial; |
| 135 | // Otherwise |interstitial_page_| is responsible for deleting it. |
| 136 | } |
| 137 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 138 | SupervisedUserInterstitial::SupervisedUserInterstitial( |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 139 | WebContents* web_contents, |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 140 | const GURL& url, |
treib | 170c3f8 | 2014-12-10 19:13:25 | [diff] [blame] | 141 | SupervisedUserURLFilter::FilteringBehaviorReason reason, |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 142 | const base::Callback<void(bool)>& callback) |
| 143 | : web_contents_(web_contents), |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 144 | profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 145 | interstitial_page_(NULL), |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 146 | url_(url), |
treib | 170c3f8 | 2014-12-10 19:13:25 | [diff] [blame] | 147 | reason_(reason), |
bauerb | 646019b1 | 2014-10-16 16:23:09 | [diff] [blame] | 148 | callback_(callback), |
| 149 | weak_ptr_factory_(this) {} |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 150 | |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 151 | SupervisedUserInterstitial::~SupervisedUserInterstitial() { |
| 152 | DCHECK(!web_contents_); |
| 153 | } |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 154 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 155 | bool SupervisedUserInterstitial::Init() { |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 156 | if (ShouldProceed()) { |
| 157 | // It can happen that the site was only allowed very recently and the URL |
| 158 | // filter on the IO thread had not been updated yet. Proceed with the |
| 159 | // request without showing the interstitial. |
| 160 | DispatchContinueRequest(true); |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 161 | return false; |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 164 | InfoBarService* service = InfoBarService::FromWebContents(web_contents_); |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 165 | if (service) { |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 166 | // Remove all the infobars which are attached to |web_contents_| and for |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 167 | // which ShouldExpire() returns true. |
| 168 | content::LoadCommittedDetails details; |
| 169 | // |details.is_in_page| is default false, and |details.is_main_frame| is |
| 170 | // default true. This results in is_navigation_to_different_page() returning |
| 171 | // true. |
| 172 | DCHECK(details.is_navigation_to_different_page()); |
| 173 | const content::NavigationController& controller = |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 174 | web_contents_->GetController(); |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 175 | details.entry = controller.GetActiveEntry(); |
| 176 | if (controller.GetLastCommittedEntry()) { |
| 177 | details.previous_entry_index = controller.GetLastCommittedEntryIndex(); |
| 178 | details.previous_url = controller.GetLastCommittedEntry()->GetURL(); |
| 179 | } |
| 180 | details.type = content::NAVIGATION_TYPE_NEW_PAGE; |
[email protected] | b44f1d3 | 2014-04-10 13:53:26 | [diff] [blame] | 181 | for (int i = service->infobar_count() - 1; i >= 0; --i) { |
[email protected] | 051655ad | 2014-04-18 15:09:41 | [diff] [blame] | 182 | infobars::InfoBar* infobar = service->infobar_at(i); |
[email protected] | 5daf1d9 | 2014-04-04 15:52:13 | [diff] [blame] | 183 | if (infobar->delegate()->ShouldExpire( |
| 184 | InfoBarService::NavigationDetailsFromLoadCommittedDetails( |
| 185 | details))) |
[email protected] | b44f1d3 | 2014-04-10 13:53:26 | [diff] [blame] | 186 | service->RemoveInfoBar(infobar); |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 190 | SupervisedUserService* supervised_user_service = |
| 191 | SupervisedUserServiceFactory::GetForProfile(profile_); |
| 192 | supervised_user_service->AddObserver(this); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 193 | |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 194 | interstitial_page_ = |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 195 | content::InterstitialPage::Create(web_contents_, true, url_, this); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 196 | interstitial_page_->Show(); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 197 | |
[email protected] | b8af2d1 | 2014-04-29 07:30:42 | [diff] [blame] | 198 | return true; |
| 199 | } |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 200 | |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 201 | // static |
| 202 | std::string SupervisedUserInterstitial::GetHTMLContents( |
| 203 | Profile* profile, |
| 204 | SupervisedUserURLFilter::FilteringBehaviorReason reason) { |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 205 | base::DictionaryValue strings; |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 206 | strings.SetString("blockPageTitle", |
| 207 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE)); |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 208 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 209 | SupervisedUserService* supervised_user_service = |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 210 | SupervisedUserServiceFactory::GetForProfile(profile); |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 211 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 212 | bool allow_access_requests = supervised_user_service->AccessRequestsEnabled(); |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 213 | strings.SetBoolean("allowAccessRequests", allow_access_requests); |
| 214 | |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 215 | std::string profile_image_url = profile->GetPrefs()->GetString( |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 216 | prefs::kSupervisedUserCustodianProfileImageURL); |
| 217 | strings.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url, |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 218 | kAvatarSize1x)); |
| 219 | strings.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url, |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 220 | kAvatarSize2x)); |
| 221 | |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 222 | std::string profile_image_url2 = profile->GetPrefs()->GetString( |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 223 | prefs::kSupervisedUserSecondCustodianProfileImageURL); |
| 224 | strings.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2, |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 225 | kAvatarSize1x)); |
| 226 | strings.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2, |
[email protected] | 95c47d0 | 2014-08-19 09:17:50 | [diff] [blame] | 227 | kAvatarSize2x)); |
| 228 | |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 229 | bool is_child_account = profile->IsChild(); |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 230 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 231 | base::string16 custodian = |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 232 | base::UTF8ToUTF16(supervised_user_service->GetCustodianName()); |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 233 | base::string16 second_custodian = |
| 234 | base::UTF8ToUTF16(supervised_user_service->GetSecondCustodianName()); |
atanasova | ca75173 | 2016-01-05 09:27:41 | [diff] [blame] | 235 | base::string16 custodian_email = |
| 236 | base::UTF8ToUTF16(supervised_user_service->GetCustodianEmailAddress()); |
| 237 | base::string16 second_custodian_email = base::UTF8ToUTF16( |
| 238 | supervised_user_service->GetSecondCustodianEmailAddress()); |
| 239 | strings.SetString("custodianName", custodian); |
| 240 | strings.SetString("custodianEmail", custodian_email); |
| 241 | strings.SetString("secondCustodianName", second_custodian); |
| 242 | strings.SetString("secondCustodianEmail", second_custodian_email); |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 243 | |
| 244 | base::string16 block_message; |
| 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); |
atanasova | ca75173 | 2016-01-05 09:27:41 | [diff] [blame] | 260 | strings.SetString("blockReasonMessage", l10n_util::GetStringUTF16( |
| 261 | SupervisedUserURLFilter::GetBlockMessageID( |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 262 | reason, is_child_account, second_custodian.empty()))); |
atanasova | ca75173 | 2016-01-05 09:27:41 | [diff] [blame] | 263 | strings.SetString("blockReasonHeader", l10n_util::GetStringUTF16( |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 264 | SupervisedUserURLFilter::GetBlockHeaderID(reason))); |
treib | 5dd8daf3 | 2014-12-18 17:36:37 | [diff] [blame] | 265 | bool show_feedback = false; |
treib | 9dbb930 | 2015-01-08 14:42:39 | [diff] [blame] | 266 | #if defined(GOOGLE_CHROME_BUILD) |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 267 | show_feedback = |
| 268 | is_child_account && SupervisedUserURLFilter::ReasonIsAutomatic(reason); |
treib | 5dd8daf3 | 2014-12-18 17:36:37 | [diff] [blame] | 269 | #endif |
| 270 | strings.SetBoolean("showFeedbackLink", show_feedback); |
| 271 | strings.SetString("feedbackLink", |
| 272 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_SEND_FEEDBACK)); |
| 273 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 274 | strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON)); |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 275 | strings.SetString("requestAccessButton", l10n_util::GetStringUTF16( |
atanasova | ca75173 | 2016-01-05 09:27:41 | [diff] [blame] | 276 | IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON)); |
| 277 | |
| 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)); |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 282 | |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 283 | base::string16 request_sent_message; |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 284 | base::string16 request_failed_message; |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 285 | if (is_child_account) { |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 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 | } |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 297 | } else { |
| 298 | request_sent_message = l10n_util::GetStringFUTF16( |
| 299 | IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE, custodian); |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 300 | request_failed_message = l10n_util::GetStringFUTF16( |
| 301 | IDS_BLOCK_INTERSTITIAL_REQUEST_FAILED_MESSAGE, custodian); |
treib | 5fc1aed | 2014-12-08 12:46:03 | [diff] [blame] | 302 | } |
| 303 | strings.SetString("requestSentMessage", request_sent_message); |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 304 | strings.SetString("requestFailedMessage", request_failed_message); |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 305 | |
dbeam | 622e9f3 | 2015-01-28 21:22:45 | [diff] [blame] | 306 | const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
| 307 | webui::SetLoadTimeDataDefaults(app_locale, &strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 308 | |
bauerb | d039918 | 2015-01-26 10:20:45 | [diff] [blame] | 309 | std::string html = |
| 310 | ResourceBundle::GetSharedInstance() |
| 311 | .GetRawDataResource(IDR_SUPERVISED_USER_BLOCK_INTERSTITIAL_HTML) |
| 312 | .as_string(); |
| 313 | webui::AppendWebUiCssTextDefaults(&html); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 314 | |
[email protected] | 2779e3a | 2013-01-22 18:40:21 | [diff] [blame] | 315 | return webui::GetI18nTemplateHtml(html, &strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 316 | } |
| 317 | |
aberent | ec7d1d1 | 2016-01-05 13:04:48 | [diff] [blame] | 318 | std::string SupervisedUserInterstitial::GetHTMLContents() { |
| 319 | return GetHTMLContents(profile_, reason_); |
| 320 | } |
| 321 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 322 | void SupervisedUserInterstitial::CommandReceived(const std::string& command) { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 323 | // For use in histograms. |
| 324 | enum Commands { |
| 325 | PREVIEW, |
| 326 | BACK, |
| 327 | NTP, |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 328 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 329 | HISTOGRAM_BOUNDING_VALUE |
| 330 | }; |
| 331 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 332 | if (command == "\"back\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 333 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
| 334 | BACK, |
| 335 | HISTOGRAM_BOUNDING_VALUE); |
treib | e7ccb7e | 2014-09-09 19:21:45 | [diff] [blame] | 336 | |
creis | f82a92ee | 2015-10-19 19:59:08 | [diff] [blame] | 337 | // The interstitial's reference to the WebContents will go away after the |
| 338 | // DontProceed call. |
| 339 | WebContents* web_contents = web_contents_; |
| 340 | DCHECK(web_contents->GetController().GetTransientEntry()); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 341 | interstitial_page_->DontProceed(); |
creis | f82a92ee | 2015-10-19 19:59:08 | [diff] [blame] | 342 | |
bauerb | 34f34c5f | 2015-10-21 15:38:32 | [diff] [blame] | 343 | TabCloser::MaybeClose(web_contents); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 347 | if (command == "\"request\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 348 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 349 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 350 | HISTOGRAM_BOUNDING_VALUE); |
[email protected] | ad7a89e | 2013-05-31 12:03:24 | [diff] [blame] | 351 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 352 | SupervisedUserService* supervised_user_service = |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 353 | SupervisedUserServiceFactory::GetForProfile(profile_); |
treib | 8ecc1eb5 | 2015-03-04 18:29:06 | [diff] [blame] | 354 | supervised_user_service->AddURLAccessRequest( |
bauerb | 646019b1 | 2014-10-16 16:23:09 | [diff] [blame] | 355 | url_, base::Bind(&SupervisedUserInterstitial::OnAccessRequestAdded, |
| 356 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | |
atanasova | ca75173 | 2016-01-05 09:27:41 | [diff] [blame] | 360 | SupervisedUserService* supervised_user_service = |
| 361 | SupervisedUserServiceFactory::GetForProfile(profile_); |
| 362 | base::string16 second_custodian = |
| 363 | base::UTF8ToUTF16(supervised_user_service->GetSecondCustodianName()); |
| 364 | |
treib | 5dd8daf3 | 2014-12-18 17:36:37 | [diff] [blame] | 365 | if (command == "\"feedback\"") { |
treib | d733ef8b | 2015-01-21 19:56:32 | [diff] [blame] | 366 | base::string16 reason = l10n_util::GetStringUTF16( |
atanasova | ca75173 | 2016-01-05 09:27:41 | [diff] [blame] | 367 | SupervisedUserURLFilter::GetBlockMessageID( |
| 368 | reason_, true, second_custodian.empty())); |
treib | d733ef8b | 2015-01-21 19:56:32 | [diff] [blame] | 369 | std::string message = l10n_util::GetStringFUTF8( |
| 370 | IDS_BLOCK_INTERSTITIAL_DEFAULT_FEEDBACK_TEXT, reason); |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 371 | #if BUILDFLAG(ANDROID_JAVA_UI) |
treib | d733ef8b | 2015-01-21 19:56:32 | [diff] [blame] | 372 | ReportChildAccountFeedback(web_contents_, message, url_); |
treib | 9dbb930 | 2015-01-08 14:42:39 | [diff] [blame] | 373 | #else |
treib | 8a6a5d9 | 2015-05-12 16:33:16 | [diff] [blame] | 374 | chrome::ShowFeedbackPage(chrome::FindBrowserWithWebContents(web_contents_), |
atanasova | ca75173 | 2016-01-05 09:27:41 | [diff] [blame] | 375 | message, std::string()); |
treib | 9dbb930 | 2015-01-08 14:42:39 | [diff] [blame] | 376 | #endif |
treib | 5dd8daf3 | 2014-12-18 17:36:37 | [diff] [blame] | 377 | return; |
| 378 | } |
treib | 5dd8daf3 | 2014-12-18 17:36:37 | [diff] [blame] | 379 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 380 | NOTREACHED(); |
| 381 | } |
| 382 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 383 | void SupervisedUserInterstitial::OnProceed() { |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 384 | DispatchContinueRequest(true); |
| 385 | } |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 386 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 387 | void SupervisedUserInterstitial::OnDontProceed() { |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 388 | DispatchContinueRequest(false); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 389 | } |
| 390 | |
meacer | fc55bd5 | 2015-03-05 19:12:00 | [diff] [blame] | 391 | content::InterstitialPageDelegate::TypeID |
| 392 | SupervisedUserInterstitial::GetTypeForTesting() const { |
| 393 | return SupervisedUserInterstitial::kTypeForTesting; |
| 394 | } |
| 395 | |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 396 | void SupervisedUserInterstitial::OnURLFilterChanged() { |
| 397 | if (ShouldProceed()) |
| 398 | interstitial_page_->Proceed(); |
| 399 | } |
| 400 | |
bauerb | 646019b1 | 2014-10-16 16:23:09 | [diff] [blame] | 401 | void SupervisedUserInterstitial::OnAccessRequestAdded(bool success) { |
bauerb | fc4af2ac | 2014-11-07 14:27:05 | [diff] [blame] | 402 | VLOG(1) << "Sent access request for " << url_.spec() |
| 403 | << (success ? " successfully" : " unsuccessfully"); |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 404 | std::string jsFunc = |
| 405 | base::StringPrintf("setRequestStatus(%s);", success ? "true" : "false"); |
| 406 | interstitial_page_->GetMainFrame()->ExecuteJavaScript( |
| 407 | base::ASCIIToUTF16(jsFunc)); |
bauerb | 646019b1 | 2014-10-16 16:23:09 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 410 | bool SupervisedUserInterstitial::ShouldProceed() { |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 411 | SupervisedUserService* supervised_user_service = |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 412 | SupervisedUserServiceFactory::GetForProfile(profile_); |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 413 | SupervisedUserURLFilter* url_filter = |
| 414 | supervised_user_service->GetURLFilterForUIThread(); |
treib | 9e4fab90 | 2014-10-29 14:25:26 | [diff] [blame] | 415 | SupervisedUserURLFilter::FilteringBehavior behavior; |
treib | 22c3a04 | 2015-01-15 21:30:13 | [diff] [blame] | 416 | if (url_filter->HasAsyncURLChecker()) { |
| 417 | if (!url_filter->GetManualFilteringBehaviorForURL(url_, &behavior)) |
| 418 | return false; |
| 419 | } else { |
| 420 | behavior = url_filter->GetFilteringBehaviorForURL(url_); |
| 421 | } |
| 422 | return behavior != SupervisedUserURLFilter::BLOCK; |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | cce15bb | 2014-06-17 13:43:51 | [diff] [blame] | 425 | void SupervisedUserInterstitial::DispatchContinueRequest( |
| 426 | bool continue_request) { |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 427 | SupervisedUserService* supervised_user_service = |
| 428 | SupervisedUserServiceFactory::GetForProfile(profile_); |
| 429 | supervised_user_service->RemoveObserver(this); |
| 430 | |
peconn | a2092d4 | 2015-09-17 17:28:59 | [diff] [blame] | 431 | if (!callback_.is_null()) |
| 432 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 433 | base::Bind(callback_, continue_request)); |
treib | ab0a39e | 2014-09-24 14:48:28 | [diff] [blame] | 434 | |
| 435 | // After this, the WebContents may be destroyed. Make sure we don't try to use |
| 436 | // it again. |
| 437 | web_contents_ = NULL; |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 438 | } |