blob: d3e74717ddd54825dc51b95c914c84b5f0a2a143 [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]8991d81b2013-01-09 15:56:039#include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
[email protected]a09159a2012-11-29 12:51:4810#include "chrome/browser/prefs/pref_service.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/tab_contents/tab_util.h"
[email protected]a09159a2012-11-29 12:51:4813#include "chrome/common/pref_names.h"
14#include "chrome/common/url_constants.h"
15#include "content/public/browser/browser_thread.h"
16#include "content/public/browser/interstitial_page.h"
17#include "content/public/browser/web_contents.h"
18#include "content/public/browser/web_contents_delegate.h"
19#include "grit/browser_resources.h"
20#include "grit/generated_resources.h"
21#include "net/base/net_util.h"
22#include "ui/base/l10n/l10n_util.h"
23#include "ui/base/resource/resource_bundle.h"
[email protected]73592382013-01-18 19:22:3724#include "ui/webui/jstemplate_builder.h"
[email protected]5053d402013-01-23 05:19:2625#include "ui/webui/web_ui_util.h"
[email protected]a09159a2012-11-29 12:51:4826
27using content::BrowserThread;
28
29namespace {
30
31void ShowInterstitialOnUIThread(int render_process_host_id,
32 int render_view_id,
33 const GURL& url,
34 const base::Callback<void(bool)>& callback) {
35 // The tab might have been closed.
36 content::WebContents* web_contents =
37 tab_util::GetWebContentsByID(render_process_host_id, render_view_id);
38 if (!web_contents) {
39 BrowserThread::PostTask(
40 BrowserThread::IO, FROM_HERE, base::Bind(callback, true));
41 return;
42 }
43
[email protected]8991d81b2013-01-09 15:56:0344 ManagedModeNavigationObserver* navigation_observer =
45 ManagedModeNavigationObserver::FromWebContents(web_contents);
46 navigation_observer->SetStateToRecordingAfterPreview();
47
[email protected]a09159a2012-11-29 12:51:4848 new ManagedModeInterstitial(web_contents, url, callback);
49}
50
51} // namespace
52
53// static
54void ManagedModeInterstitial::ShowInterstitial(
55 int render_process_host_id,
56 int render_view_id,
57 const GURL& url,
58 const base::Callback<void(bool)>& callback) {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
60 BrowserThread::PostTask(
61 BrowserThread::UI, FROM_HERE,
62 base::Bind(&ShowInterstitialOnUIThread, render_process_host_id,
63 render_view_id, url, callback));
64}
65
66ManagedModeInterstitial::ManagedModeInterstitial(
67 content::WebContents* web_contents,
68 const GURL& url,
69 const base::Callback<void(bool)>& callback)
70 : web_contents_(web_contents),
71 url_(url),
72 callback_(callback) {
73 Profile* profile =
74 Profile::FromBrowserContext(web_contents->GetBrowserContext());
75 languages_ = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
76
77 interstitial_page_ = content::InterstitialPage::Create(
78 web_contents, true, url_, this);
79 interstitial_page_->Show();
80}
81
82ManagedModeInterstitial::~ManagedModeInterstitial() {}
83
84void ManagedModeInterstitial::GoToNewTabPage() {
85 web_contents_->GetDelegate()->OpenURLFromTab(
86 web_contents_,
87 content::OpenURLParams(GURL(chrome::kChromeUINewTabURL),
88 content::Referrer(),
89 CURRENT_TAB,
90 content::PAGE_TRANSITION_LINK,
91 false));
92}
93
94std::string ManagedModeInterstitial::GetHTMLContents() {
95 DictionaryValue strings;
96 strings.SetString("blockPageTitle",
97 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
98 strings.SetString("blockPageMessage",
99 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE));
100 strings.SetString("blockedUrl", net::FormatUrl(url_, languages_));
101 strings.SetString("bypassBlockMessage",
102 l10n_util::GetStringUTF16(IDS_BYPASS_BLOCK_MESSAGE));
103 strings.SetString("bypassBlockButton",
104 l10n_util::GetStringUTF16(IDS_BYPASS_BLOCK_BUTTON));
105 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
106 strings.SetString(
107 "contentPacksSectionButton",
108 l10n_util::GetStringUTF16(IDS_CONTENT_PACKS_SECTION_BUTTON));
[email protected]5053d402013-01-23 05:19:26109 webui::SetFontAndTextDirection(&strings);
[email protected]a09159a2012-11-29 12:51:48110
111 base::StringPiece html(
112 ResourceBundle::GetSharedInstance().GetRawDataResource(
113 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML));
114
[email protected]2779e3a2013-01-22 18:40:21115 webui::UseVersion2 version;
116 return webui::GetI18nTemplateHtml(html, &strings);
[email protected]a09159a2012-11-29 12:51:48117}
118
119void ManagedModeInterstitial::CommandReceived(const std::string& command) {
[email protected]2c145422013-01-24 18:15:40120 // For use in histograms.
121 enum Commands {
122 PREVIEW,
123 BACK,
124 NTP,
125 HISTOGRAM_BOUNDING_VALUE
126 };
127
[email protected]a09159a2012-11-29 12:51:48128 if (command == "\"preview\"") {
[email protected]2c145422013-01-24 18:15:40129 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
130 PREVIEW,
131 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:48132 interstitial_page_->Proceed();
133 return;
134 }
135
136 if (command == "\"back\"") {
[email protected]2c145422013-01-24 18:15:40137 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
138 BACK,
139 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:48140 interstitial_page_->DontProceed();
141 return;
142 }
143
144 if (command == "\"ntp\"") {
[email protected]2c145422013-01-24 18:15:40145 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
146 NTP,
147 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:48148 GoToNewTabPage();
149 return;
150 }
151
152 NOTREACHED();
153}
154
155void ManagedModeInterstitial::OnProceed() {
156 DispatchContinueRequest(true);
157}
158
159void ManagedModeInterstitial::OnDontProceed() {
160 DispatchContinueRequest(false);
161}
162
163void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) {
164 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
165 base::Bind(callback_, continue_request));
166}
167