Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2590)

Unified Diff: chrome/browser/net/chrome_network_delegate.cc

Issue 354183002: Enforce SafetyMode for YouTube if prefs::kForceSafeSearch is on. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/strings/string_util.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/net/chrome_network_delegate.cc
diff --git a/chrome/browser/net/chrome_network_delegate.cc b/chrome/browser/net/chrome_network_delegate.cc
index 6558ae123a8d01eac386242f1b1d44222e8e392a..1ce96d656e2d04a61aeded7fb843772cde907b65 100644
--- a/chrome/browser/net/chrome_network_delegate.cc
+++ b/chrome/browser/net/chrome_network_delegate.cc
@@ -17,6 +17,8 @@
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/cookie_settings.h"
@@ -161,6 +163,67 @@ void ForceGoogleSafeSearchCallbackWrapper(
callback.Run(rv);
}
+const char kYoutubePrefCookieName[] = "PREF";
battre 2014/07/07 09:00:06 What do you think of moving the youtube and safebr
Marc Treib 2014/07/07 16:06:46 It is grungy :-/ I moved the code into new safe_se
+// Youtube pref flags are stored in bit masks of 31 bits each, called "f1",
+// "f2" etc. The Safety Mode flag is bit 58, so bit 27 in "f2".
+const char kYoutubePrefCookieSafetyModeFlagsEntryName[] = "f2";
+const int kYoutubePrefCookieSafetyModeFlagsEntryValue = (1 << 27);
+const char kYoutubePrefCookieFlagsValueFormat[] = "%x";
+
+bool IsYoutubePrefCookie(const std::pair<std::string, std::string>& cookie) {
+ return cookie.first == kYoutubePrefCookieName;
+}
+
+bool IsYoutubePrefCookieSafetyModeFlagsEntry(
+ const std::pair<std::string, std::string>& pref_entry) {
+ return pref_entry.first == kYoutubePrefCookieSafetyModeFlagsEntryName;
+}
+
+// If |request| is a request to Youtube, enforces Youtube's Safety Mode by
+// adding/modifying Youtube's PrefCookie header.
+void ForceYoutubeSafetyMode(const net::URLRequest* request,
+ net::HttpRequestHeaders* headers) {
battre 2014/07/07 09:00:06 Could you add more newlines into this function and
Marc Treib 2014/07/07 16:06:46 Done.
+ if (!google_util::IsYoutubeDomainUrl(
+ request->url(),
+ google_util::ALLOW_SUBDOMAIN,
+ google_util::DISALLOW_NON_STANDARD_PORTS))
+ return;
+
+ std::string cookie_string;
+ headers->GetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie),
+ &cookie_string);
+ base::StringPairs cookies;
+ base::SplitStringIntoKeyValuePairs(cookie_string, '=', ';', &cookies);
+ base::StringPairs::iterator pref_it =
+ std::find_if(cookies.begin(), cookies.end(), IsYoutubePrefCookie);
+ if (pref_it == cookies.end()) {
+ cookies.push_back(std::make_pair(std::string(kYoutubePrefCookieName),
+ std::string()));
+ pref_it = cookies.end() - 1;
+ }
+ base::StringPairs pref_values;
+ base::SplitStringIntoKeyValuePairs(pref_it->second, '=', '&', &pref_values);
+ base::StringPairs::iterator flag_it =
+ std::find_if(pref_values.begin(), pref_values.end(),
+ IsYoutubePrefCookieSafetyModeFlagsEntry);
+ int flag_value = 0;
+ if (flag_it == pref_values.end()) {
+ pref_values.push_back(
+ std::make_pair(std::string(kYoutubePrefCookieSafetyModeFlagsEntryName),
+ std::string()));
+ flag_it = pref_values.end() - 1;
+ } else {
+ base::HexStringToInt(base::StringPiece(flag_it->second), &flag_value);
+ }
+ flag_value |= kYoutubePrefCookieSafetyModeFlagsEntryValue;
+ flag_it->second =
+ base::StringPrintf(kYoutubePrefCookieFlagsValueFormat, flag_value);
+ pref_it->second = JoinStringKeyValuePairs(pref_values, '=', '&');
+ cookie_string = JoinStringKeyValuePairs(cookies, '=', ';');
+ headers->SetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie),
+ base::StringPiece(cookie_string));
+}
+
void UpdateContentLengthPrefs(
int received_content_length,
int original_content_length,
@@ -478,6 +541,11 @@ int ChromeNetworkDelegate::OnBeforeSendHeaders(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
+ bool force_safe_search = force_google_safe_search_ &&
+ force_google_safe_search_->GetValue();
+ if (force_safe_search)
+ ForceYoutubeSafetyMode(request, headers);
+
TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
}
« no previous file with comments | « base/strings/string_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698