[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 | |
| 44 | interstitial_page_ = content::InterstitialPage::Create( |
| 45 | web_contents, true, url_, this); |
| 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)); |
| 55 | strings.SetString("blockPageMessage", |
| 56 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE)); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 57 | strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON)); |
| 58 | strings.SetString( |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 59 | "requestAccessButton", |
| 60 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON)); |
| 61 | |
| 62 | // TODO(sergiu): Set name to real value here. |
| 63 | std::string custodian_name("John Doe"); |
| 64 | strings.SetString( |
| 65 | "requestSentMessage", |
| 66 | l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE, |
| 67 | ASCIIToUTF16(custodian_name))); |
| 68 | |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 69 | webui::SetFontAndTextDirection(&strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 70 | |
| 71 | base::StringPiece html( |
| 72 | ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 73 | IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML)); |
| 74 | |
[email protected] | 2779e3a | 2013-01-22 18:40:21 | [diff] [blame] | 75 | webui::UseVersion2 version; |
| 76 | return webui::GetI18nTemplateHtml(html, &strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void ManagedModeInterstitial::CommandReceived(const std::string& command) { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 80 | // For use in histograms. |
| 81 | enum Commands { |
| 82 | PREVIEW, |
| 83 | BACK, |
| 84 | NTP, |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 85 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 86 | HISTOGRAM_BOUNDING_VALUE |
| 87 | }; |
| 88 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 89 | if (command == "\"back\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 90 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
| 91 | BACK, |
| 92 | HISTOGRAM_BOUNDING_VALUE); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 93 | interstitial_page_->DontProceed(); |
| 94 | return; |
| 95 | } |
| 96 | |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 97 | if (command == "\"request\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 98 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 99 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 100 | HISTOGRAM_BOUNDING_VALUE); |
[email protected] | ad7a89e | 2013-05-31 12:03:24 | [diff] [blame] | 101 | |
| 102 | Profile* profile = |
| 103 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 104 | ManagedUserService* managed_user_service = |
| 105 | ManagedUserServiceFactory::GetForProfile(profile); |
| 106 | managed_user_service->AddAccessRequest(url_); |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 107 | DVLOG(1) << "Sent access request for " << url_.spec(); |
| 108 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 109 | return; |
| 110 | } |
| 111 | |
| 112 | NOTREACHED(); |
| 113 | } |
| 114 | |
| 115 | void ManagedModeInterstitial::OnProceed() { |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 116 | NOTREACHED(); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void ManagedModeInterstitial::OnDontProceed() { |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 120 | DispatchContinueRequest(false); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 123 | void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) { |
| 124 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 125 | base::Bind(callback_, continue_request)); |
| 126 | } |