blob: d444fcac2c39ad30a60512892954090e55d244cf [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));
55 strings.SetString("blockPageMessage",
56 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE));
[email protected]a09159a2012-11-29 12:51:4857 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
58 strings.SetString(
[email protected]59f21562013-05-23 16:19:3059 "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]5053d402013-01-23 05:19:2669 webui::SetFontAndTextDirection(&strings);
[email protected]a09159a2012-11-29 12:51:4870
71 base::StringPiece html(
72 ResourceBundle::GetSharedInstance().GetRawDataResource(
73 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML));
74
[email protected]2779e3a2013-01-22 18:40:2175 webui::UseVersion2 version;
76 return webui::GetI18nTemplateHtml(html, &strings);
[email protected]a09159a2012-11-29 12:51:4877}
78
79void ManagedModeInterstitial::CommandReceived(const std::string& command) {
[email protected]2c145422013-01-24 18:15:4080 // For use in histograms.
81 enum Commands {
82 PREVIEW,
83 BACK,
84 NTP,
[email protected]59f21562013-05-23 16:19:3085 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:4086 HISTOGRAM_BOUNDING_VALUE
87 };
88
[email protected]a09159a2012-11-29 12:51:4889 if (command == "\"back\"") {
[email protected]2c145422013-01-24 18:15:4090 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
91 BACK,
92 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:4893 interstitial_page_->DontProceed();
94 return;
95 }
96
[email protected]59f21562013-05-23 16:19:3097 if (command == "\"request\"") {
[email protected]2c145422013-01-24 18:15:4098 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
[email protected]59f21562013-05-23 16:19:3099 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40100 HISTOGRAM_BOUNDING_VALUE);
[email protected]ad7a89e2013-05-31 12:03:24101
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]59f21562013-05-23 16:19:30107 DVLOG(1) << "Sent access request for " << url_.spec();
108
[email protected]a09159a2012-11-29 12:51:48109 return;
110 }
111
112 NOTREACHED();
113}
114
115void ManagedModeInterstitial::OnProceed() {
[email protected]59f21562013-05-23 16:19:30116 NOTREACHED();
[email protected]a09159a2012-11-29 12:51:48117}
118
119void ManagedModeInterstitial::OnDontProceed() {
[email protected]6c7af96d2013-03-28 22:55:14120 DispatchContinueRequest(false);
[email protected]a09159a2012-11-29 12:51:48121}
122
[email protected]6c7af96d2013-03-28 22:55:14123void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) {
124 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
125 base::Bind(callback_, continue_request));
126}