[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "chrome/browser/managed_mode/managed_mode_interstitial.h" |
| 6 | |
| 7 | #include "base/i18n/rtl.h" |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 9 | #include "base/prefs/pref_service.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | ad7a89e | 2013-05-31 12:03:24 | [diff] [blame] | 12 | #include "chrome/browser/managed_mode/managed_user_service.h" |
| 13 | #include "chrome/browser/managed_mode/managed_user_service_factory.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 15 | #include "chrome/common/pref_names.h" |
| 16 | #include "chrome/common/url_constants.h" |
| 17 | #include "content/public/browser/browser_thread.h" |
| 18 | #include "content/public/browser/interstitial_page.h" |
| 19 | #include "content/public/browser/web_contents.h" |
| 20 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 21 | #include "content/public/browser/web_ui.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 22 | #include "grit/browser_resources.h" |
| 23 | #include "grit/generated_resources.h" |
| 24 | #include "net/base/net_util.h" |
| 25 | #include "ui/base/l10n/l10n_util.h" |
| 26 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 7359238 | 2013-01-18 19:22:37 | [diff] [blame] | 27 | #include "ui/webui/jstemplate_builder.h" |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 28 | #include "ui/webui/web_ui_util.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 29 | |
| 30 | using content::BrowserThread; |
| 31 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 32 | ManagedModeInterstitial::ManagedModeInterstitial( |
| 33 | content::WebContents* web_contents, |
| 34 | const GURL& url, |
| 35 | const base::Callback<void(bool)>& callback) |
| 36 | : web_contents_(web_contents), |
| 37 | url_(url), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 38 | weak_ptr_factory_(this), |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 39 | callback_(callback) { |
| 40 | Profile* profile = |
| 41 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 42 | languages_ = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 43 | |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame^] | 44 | interstitial_page_ = |
| 45 | content::InterstitialPage::Create(web_contents, true, url_, this); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 46 | interstitial_page_->Show(); |
| 47 | } |
| 48 | |
| 49 | ManagedModeInterstitial::~ManagedModeInterstitial() {} |
| 50 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 51 | std::string ManagedModeInterstitial::GetHTMLContents() { |
| 52 | DictionaryValue strings; |
| 53 | strings.SetString("blockPageTitle", |
| 54 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE)); |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 55 | |
| 56 | Profile* profile = |
| 57 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 58 | ManagedUserService* managed_user_service = |
| 59 | ManagedUserServiceFactory::GetForProfile(profile); |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame^] | 60 | |
| 61 | bool allow_access_requests = managed_user_service->AccessRequestsEnabled(); |
| 62 | strings.SetBoolean("allowAccessRequests", allow_access_requests); |
| 63 | |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 64 | string16 custodian = UTF8ToUTF16(managed_user_service->GetCustodianName()); |
| 65 | strings.SetString( |
| 66 | "blockPageMessage", |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame^] | 67 | allow_access_requests |
| 68 | ? l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE, |
| 69 | custodian) |
| 70 | : l10n_util::GetStringUTF16( |
| 71 | IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED)); |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 72 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 73 | strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON)); |
| 74 | strings.SetString( |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 75 | "requestAccessButton", |
| 76 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON)); |
| 77 | |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 78 | strings.SetString( |
| 79 | "requestSentMessage", |
| 80 | l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE, |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 81 | custodian)); |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 82 | |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 83 | webui::SetFontAndTextDirection(&strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 84 | |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame^] | 85 | base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 86 | IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML)); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 87 | |
[email protected] | 2779e3a | 2013-01-22 18:40:21 | [diff] [blame] | 88 | webui::UseVersion2 version; |
| 89 | return webui::GetI18nTemplateHtml(html, &strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void ManagedModeInterstitial::CommandReceived(const std::string& command) { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 93 | // For use in histograms. |
| 94 | enum Commands { |
| 95 | PREVIEW, |
| 96 | BACK, |
| 97 | NTP, |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 98 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 99 | HISTOGRAM_BOUNDING_VALUE |
| 100 | }; |
| 101 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 102 | if (command == "\"back\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 103 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
| 104 | BACK, |
| 105 | HISTOGRAM_BOUNDING_VALUE); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 106 | interstitial_page_->DontProceed(); |
| 107 | return; |
| 108 | } |
| 109 | |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 110 | if (command == "\"request\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 111 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 112 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 113 | HISTOGRAM_BOUNDING_VALUE); |
[email protected] | ad7a89e | 2013-05-31 12:03:24 | [diff] [blame] | 114 | |
| 115 | Profile* profile = |
| 116 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 117 | ManagedUserService* managed_user_service = |
| 118 | ManagedUserServiceFactory::GetForProfile(profile); |
| 119 | managed_user_service->AddAccessRequest(url_); |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 120 | DVLOG(1) << "Sent access request for " << url_.spec(); |
| 121 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
| 125 | NOTREACHED(); |
| 126 | } |
| 127 | |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame^] | 128 | void ManagedModeInterstitial::OnProceed() { NOTREACHED(); } |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 129 | |
| 130 | void ManagedModeInterstitial::OnDontProceed() { |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 131 | DispatchContinueRequest(false); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 134 | void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) { |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame^] | 135 | BrowserThread::PostTask( |
| 136 | BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 137 | } |