blob: b3daecc6e2eefdb11b5fffdc6082455324af2fb2 [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
[email protected]0369d6ab2013-08-09 01:52:5944 interstitial_page_ =
45 content::InterstitialPage::Create(web_contents, true, url_, this);
[email protected]a09159a2012-11-29 12:51:4846 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);
[email protected]0369d6ab2013-08-09 01:52:5960
61 bool allow_access_requests = managed_user_service->AccessRequestsEnabled();
62 strings.SetBoolean("allowAccessRequests", allow_access_requests);
63
[email protected]fae057a2013-06-21 22:46:0864 string16 custodian = UTF8ToUTF16(managed_user_service->GetCustodianName());
65 strings.SetString(
66 "blockPageMessage",
[email protected]0369d6ab2013-08-09 01:52:5967 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]fae057a2013-06-21 22:46:0872
[email protected]a09159a2012-11-29 12:51:4873 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
74 strings.SetString(
[email protected]59f21562013-05-23 16:19:3075 "requestAccessButton",
76 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
77
[email protected]59f21562013-05-23 16:19:3078 strings.SetString(
79 "requestSentMessage",
80 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE,
[email protected]fae057a2013-06-21 22:46:0881 custodian));
[email protected]59f21562013-05-23 16:19:3082
[email protected]5053d402013-01-23 05:19:2683 webui::SetFontAndTextDirection(&strings);
[email protected]a09159a2012-11-29 12:51:4884
[email protected]0369d6ab2013-08-09 01:52:5985 base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource(
86 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML));
[email protected]a09159a2012-11-29 12:51:4887
[email protected]2779e3a2013-01-22 18:40:2188 webui::UseVersion2 version;
89 return webui::GetI18nTemplateHtml(html, &strings);
[email protected]a09159a2012-11-29 12:51:4890}
91
92void ManagedModeInterstitial::CommandReceived(const std::string& command) {
[email protected]2c145422013-01-24 18:15:4093 // For use in histograms.
94 enum Commands {
95 PREVIEW,
96 BACK,
97 NTP,
[email protected]59f21562013-05-23 16:19:3098 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:4099 HISTOGRAM_BOUNDING_VALUE
100 };
101
[email protected]a09159a2012-11-29 12:51:48102 if (command == "\"back\"") {
[email protected]2c145422013-01-24 18:15:40103 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
104 BACK,
105 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:48106 interstitial_page_->DontProceed();
107 return;
108 }
109
[email protected]59f21562013-05-23 16:19:30110 if (command == "\"request\"") {
[email protected]2c145422013-01-24 18:15:40111 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
[email protected]59f21562013-05-23 16:19:30112 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40113 HISTOGRAM_BOUNDING_VALUE);
[email protected]ad7a89e2013-05-31 12:03:24114
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]59f21562013-05-23 16:19:30120 DVLOG(1) << "Sent access request for " << url_.spec();
121
[email protected]a09159a2012-11-29 12:51:48122 return;
123 }
124
125 NOTREACHED();
126}
127
[email protected]0369d6ab2013-08-09 01:52:59128void ManagedModeInterstitial::OnProceed() { NOTREACHED(); }
[email protected]a09159a2012-11-29 12:51:48129
130void ManagedModeInterstitial::OnDontProceed() {
[email protected]6c7af96d2013-03-28 22:55:14131 DispatchContinueRequest(false);
[email protected]a09159a2012-11-29 12:51:48132}
133
[email protected]6c7af96d2013-03-28 22:55:14134void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) {
[email protected]0369d6ab2013-08-09 01:52:59135 BrowserThread::PostTask(
136 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
[email protected]6c7af96d2013-03-28 22:55:14137}