blob: 4d31b3e74fd5dbea79b1e50b625f12d7e10278d5 [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]93c23212013-09-19 14:22:3812#include "chrome/browser/infobars/infobar_delegate.h"
13#include "chrome/browser/infobars/infobar_service.h"
[email protected]ad7a89e2013-05-31 12:03:2414#include "chrome/browser/managed_mode/managed_user_service.h"
15#include "chrome/browser/managed_mode/managed_user_service_factory.h"
[email protected]a09159a2012-11-29 12:51:4816#include "chrome/browser/profiles/profile.h"
[email protected]a09159a2012-11-29 12:51:4817#include "chrome/common/pref_names.h"
18#include "chrome/common/url_constants.h"
19#include "content/public/browser/browser_thread.h"
20#include "content/public/browser/interstitial_page.h"
[email protected]93c23212013-09-19 14:22:3821#include "content/public/browser/navigation_controller.h"
22#include "content/public/browser/navigation_details.h"
23#include "content/public/browser/navigation_entry.h"
[email protected]a09159a2012-11-29 12:51:4824#include "content/public/browser/web_contents.h"
[email protected]59f21562013-05-23 16:19:3025#include "content/public/browser/web_ui.h"
[email protected]a09159a2012-11-29 12:51:4826#include "grit/browser_resources.h"
27#include "grit/generated_resources.h"
28#include "net/base/net_util.h"
29#include "ui/base/l10n/l10n_util.h"
30#include "ui/base/resource/resource_bundle.h"
[email protected]73592382013-01-18 19:22:3731#include "ui/webui/jstemplate_builder.h"
[email protected]5053d402013-01-23 05:19:2632#include "ui/webui/web_ui_util.h"
[email protected]a09159a2012-11-29 12:51:4833
34using content::BrowserThread;
35
[email protected]a09159a2012-11-29 12:51:4836ManagedModeInterstitial::ManagedModeInterstitial(
37 content::WebContents* web_contents,
38 const GURL& url,
39 const base::Callback<void(bool)>& callback)
40 : web_contents_(web_contents),
[email protected]61f5da22013-09-05 15:52:1041 interstitial_page_(NULL),
[email protected]a09159a2012-11-29 12:51:4842 url_(url),
43 callback_(callback) {
[email protected]61f5da22013-09-05 15:52:1044 if (ShouldProceed()) {
45 // It can happen that the site was only allowed very recently and the URL
46 // filter on the IO thread had not been updated yet. Proceed with the
47 // request without showing the interstitial.
48 DispatchContinueRequest(true);
49 delete this;
50 return;
51 }
52
[email protected]93c23212013-09-19 14:22:3853 InfoBarService* service = InfoBarService::FromWebContents(web_contents);
54 if (service) {
55 // Remove all the infobars which are attached to |web_contents| and for
56 // which ShouldExpire() returns true.
57 content::LoadCommittedDetails details;
58 // |details.is_in_page| is default false, and |details.is_main_frame| is
59 // default true. This results in is_navigation_to_different_page() returning
60 // true.
61 DCHECK(details.is_navigation_to_different_page());
62 const content::NavigationController& controller =
63 web_contents->GetController();
64 details.entry = controller.GetActiveEntry();
65 if (controller.GetLastCommittedEntry()) {
66 details.previous_entry_index = controller.GetLastCommittedEntryIndex();
67 details.previous_url = controller.GetLastCommittedEntry()->GetURL();
68 }
69 details.type = content::NAVIGATION_TYPE_NEW_PAGE;
70 for (int i = service->infobar_count() - 1; i >= 0; --i) {
71 if (service->infobar_at(i)->ShouldExpire(details))
72 service->RemoveInfoBar(service->infobar_at(i));
73 }
74 }
75
[email protected]61f5da22013-09-05 15:52:1076 // TODO(bauerb): Extract an observer callback on ManagedUserService for this.
[email protected]a09159a2012-11-29 12:51:4877 Profile* profile =
78 Profile::FromBrowserContext(web_contents->GetBrowserContext());
[email protected]61f5da22013-09-05 15:52:1079 PrefService* prefs = profile->GetPrefs();
80 pref_change_registrar_.Init(prefs);
81 pref_change_registrar_.Add(
82 prefs::kDefaultManagedModeFilteringBehavior,
83 base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged,
84 base::Unretained(this)));
85 pref_change_registrar_.Add(
86 prefs::kManagedModeManualHosts,
87 base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged,
88 base::Unretained(this)));
89 pref_change_registrar_.Add(
90 prefs::kManagedModeManualURLs,
91 base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged,
92 base::Unretained(this)));
[email protected]a09159a2012-11-29 12:51:4893
[email protected]61f5da22013-09-05 15:52:1094 languages_ = prefs->GetString(prefs::kAcceptLanguages);
[email protected]0369d6ab2013-08-09 01:52:5995 interstitial_page_ =
96 content::InterstitialPage::Create(web_contents, true, url_, this);
[email protected]a09159a2012-11-29 12:51:4897 interstitial_page_->Show();
98}
99
100ManagedModeInterstitial::~ManagedModeInterstitial() {}
101
[email protected]a09159a2012-11-29 12:51:48102std::string ManagedModeInterstitial::GetHTMLContents() {
103 DictionaryValue strings;
104 strings.SetString("blockPageTitle",
105 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
[email protected]fae057a2013-06-21 22:46:08106
107 Profile* profile =
108 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
109 ManagedUserService* managed_user_service =
110 ManagedUserServiceFactory::GetForProfile(profile);
[email protected]0369d6ab2013-08-09 01:52:59111
112 bool allow_access_requests = managed_user_service->AccessRequestsEnabled();
113 strings.SetBoolean("allowAccessRequests", allow_access_requests);
114
[email protected]fae057a2013-06-21 22:46:08115 string16 custodian = UTF8ToUTF16(managed_user_service->GetCustodianName());
116 strings.SetString(
117 "blockPageMessage",
[email protected]0369d6ab2013-08-09 01:52:59118 allow_access_requests
119 ? l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE,
120 custodian)
121 : l10n_util::GetStringUTF16(
122 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED));
[email protected]fae057a2013-06-21 22:46:08123
[email protected]a09159a2012-11-29 12:51:48124 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
125 strings.SetString(
[email protected]59f21562013-05-23 16:19:30126 "requestAccessButton",
127 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
128
[email protected]59f21562013-05-23 16:19:30129 strings.SetString(
130 "requestSentMessage",
131 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE,
[email protected]fae057a2013-06-21 22:46:08132 custodian));
[email protected]59f21562013-05-23 16:19:30133
[email protected]5053d402013-01-23 05:19:26134 webui::SetFontAndTextDirection(&strings);
[email protected]a09159a2012-11-29 12:51:48135
[email protected]0369d6ab2013-08-09 01:52:59136 base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource(
137 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML));
[email protected]a09159a2012-11-29 12:51:48138
[email protected]2779e3a2013-01-22 18:40:21139 webui::UseVersion2 version;
140 return webui::GetI18nTemplateHtml(html, &strings);
[email protected]a09159a2012-11-29 12:51:48141}
142
143void ManagedModeInterstitial::CommandReceived(const std::string& command) {
[email protected]2c145422013-01-24 18:15:40144 // For use in histograms.
145 enum Commands {
146 PREVIEW,
147 BACK,
148 NTP,
[email protected]59f21562013-05-23 16:19:30149 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40150 HISTOGRAM_BOUNDING_VALUE
151 };
152
[email protected]a09159a2012-11-29 12:51:48153 if (command == "\"back\"") {
[email protected]2c145422013-01-24 18:15:40154 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
155 BACK,
156 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:48157 interstitial_page_->DontProceed();
158 return;
159 }
160
[email protected]59f21562013-05-23 16:19:30161 if (command == "\"request\"") {
[email protected]2c145422013-01-24 18:15:40162 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
[email protected]59f21562013-05-23 16:19:30163 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40164 HISTOGRAM_BOUNDING_VALUE);
[email protected]ad7a89e2013-05-31 12:03:24165
166 Profile* profile =
167 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
168 ManagedUserService* managed_user_service =
169 ManagedUserServiceFactory::GetForProfile(profile);
170 managed_user_service->AddAccessRequest(url_);
[email protected]59f21562013-05-23 16:19:30171 DVLOG(1) << "Sent access request for " << url_.spec();
172
[email protected]a09159a2012-11-29 12:51:48173 return;
174 }
175
176 NOTREACHED();
177}
178
[email protected]61f5da22013-09-05 15:52:10179void ManagedModeInterstitial::OnProceed() {
180 // CHECK instead of DCHECK as defense in depth in case we'd accidentally
181 // proceed on a blocked page.
182 CHECK(ShouldProceed());
183 DispatchContinueRequest(true);
184}
[email protected]a09159a2012-11-29 12:51:48185
186void ManagedModeInterstitial::OnDontProceed() {
[email protected]6c7af96d2013-03-28 22:55:14187 DispatchContinueRequest(false);
[email protected]a09159a2012-11-29 12:51:48188}
189
[email protected]61f5da22013-09-05 15:52:10190bool ManagedModeInterstitial::ShouldProceed() {
191 Profile* profile =
192 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
193 ManagedUserService* managed_user_service =
194 ManagedUserServiceFactory::GetForProfile(profile);
195 ManagedModeURLFilter* url_filter =
196 managed_user_service->GetURLFilterForUIThread();
197 return url_filter->GetFilteringBehaviorForURL(url_) !=
198 ManagedModeURLFilter::BLOCK;
199}
200
201void ManagedModeInterstitial::OnFilteringPrefsChanged() {
202 if (ShouldProceed())
203 interstitial_page_->Proceed();
204}
205
[email protected]6c7af96d2013-03-28 22:55:14206void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) {
[email protected]0369d6ab2013-08-09 01:52:59207 BrowserThread::PostTask(
208 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
[email protected]6c7af96d2013-03-28 22:55:14209}