blob: e74e6651df546ee07bcd7e90dc9f7d554a4d4e9e [file] [log] [blame]
[email protected]a09159a2012-11-29 12:51:481// 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]2c145422013-01-24 18:15:408#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:579#include "base/prefs/pref_service.h"
[email protected]112158af2013-06-07 23:46:1810#include "base/strings/utf_string_conversions.h"
[email protected]59f21562013-05-23 16:19:3011#include "base/values.h"
[email protected]ad7a89e2013-05-31 12:03:2412#include "chrome/browser/managed_mode/managed_user_service.h"
13#include "chrome/browser/managed_mode/managed_user_service_factory.h"
[email protected]a09159a2012-11-29 12:51:4814#include "chrome/browser/profiles/profile.h"
[email protected]a09159a2012-11-29 12:51:4815#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]59f21562013-05-23 16:19:3021#include "content/public/browser/web_ui.h"
[email protected]a09159a2012-11-29 12:51:4822#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]73592382013-01-18 19:22:3727#include "ui/webui/jstemplate_builder.h"
[email protected]5053d402013-01-23 05:19:2628#include "ui/webui/web_ui_util.h"
[email protected]a09159a2012-11-29 12:51:4829
30using content::BrowserThread;
31
[email protected]a09159a2012-11-29 12:51:4832ManagedModeInterstitial::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]9c009092013-05-01 03:14:0938 weak_ptr_factory_(this),
[email protected]a09159a2012-11-29 12:51:4839 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
49ManagedModeInterstitial::~ManagedModeInterstitial() {}
50
[email protected]a09159a2012-11-29 12:51:4851std::string ManagedModeInterstitial::GetHTMLContents() {
52 DictionaryValue strings;
53 strings.SetString("blockPageTitle",
54 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
[email protected]fae057a2013-06-21 22:46:0855
56 Profile* profile =
57 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
58 ManagedUserService* managed_user_service =
59 ManagedUserServiceFactory::GetForProfile(profile);
60 string16 custodian = UTF8ToUTF16(managed_user_service->GetCustodianName());
61 strings.SetString(
62 "blockPageMessage",
63 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE, custodian));
64
[email protected]a09159a2012-11-29 12:51:4865 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
66 strings.SetString(
[email protected]59f21562013-05-23 16:19:3067 "requestAccessButton",
68 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
69
[email protected]59f21562013-05-23 16:19:3070 strings.SetString(
71 "requestSentMessage",
72 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE,
[email protected]fae057a2013-06-21 22:46:0873 custodian));
[email protected]59f21562013-05-23 16:19:3074
[email protected]5053d402013-01-23 05:19:2675 webui::SetFontAndTextDirection(&strings);
[email protected]a09159a2012-11-29 12:51:4876
77 base::StringPiece html(
78 ResourceBundle::GetSharedInstance().GetRawDataResource(
79 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML));
80
[email protected]2779e3a2013-01-22 18:40:2181 webui::UseVersion2 version;
82 return webui::GetI18nTemplateHtml(html, &strings);
[email protected]a09159a2012-11-29 12:51:4883}
84
85void ManagedModeInterstitial::CommandReceived(const std::string& command) {
[email protected]2c145422013-01-24 18:15:4086 // For use in histograms.
87 enum Commands {
88 PREVIEW,
89 BACK,
90 NTP,
[email protected]59f21562013-05-23 16:19:3091 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:4092 HISTOGRAM_BOUNDING_VALUE
93 };
94
[email protected]a09159a2012-11-29 12:51:4895 if (command == "\"back\"") {
[email protected]2c145422013-01-24 18:15:4096 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
97 BACK,
98 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:4899 interstitial_page_->DontProceed();
100 return;
101 }
102
[email protected]59f21562013-05-23 16:19:30103 if (command == "\"request\"") {
[email protected]2c145422013-01-24 18:15:40104 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
[email protected]59f21562013-05-23 16:19:30105 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40106 HISTOGRAM_BOUNDING_VALUE);
[email protected]ad7a89e2013-05-31 12:03:24107
108 Profile* profile =
109 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
110 ManagedUserService* managed_user_service =
111 ManagedUserServiceFactory::GetForProfile(profile);
112 managed_user_service->AddAccessRequest(url_);
[email protected]59f21562013-05-23 16:19:30113 DVLOG(1) << "Sent access request for " << url_.spec();
114
[email protected]a09159a2012-11-29 12:51:48115 return;
116 }
117
118 NOTREACHED();
119}
120
121void ManagedModeInterstitial::OnProceed() {
[email protected]59f21562013-05-23 16:19:30122 NOTREACHED();
[email protected]a09159a2012-11-29 12:51:48123}
124
125void ManagedModeInterstitial::OnDontProceed() {
[email protected]6c7af96d2013-03-28 22:55:14126 DispatchContinueRequest(false);
[email protected]a09159a2012-11-29 12:51:48127}
128
[email protected]6c7af96d2013-03-28 22:55:14129void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) {
130 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
131 base::Bind(callback_, continue_request));
132}