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

Unified Diff: chrome/renderer/chrome_content_renderer_client.cc

Issue 2154233003: Rewrite YouTube Flash embeds (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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
Index: chrome/renderer/chrome_content_renderer_client.cc
diff --git a/chrome/renderer/chrome_content_renderer_client.cc b/chrome/renderer/chrome_content_renderer_client.cc
index 11fd9c54a74b045e7ae339dc5575504e89986da3..fb0c665961f17172aec8d86c08e48f78e1510cfd 100644
--- a/chrome/renderer/chrome_content_renderer_client.cc
+++ b/chrome/renderer/chrome_content_renderer_client.cc
@@ -21,6 +21,7 @@
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/common/channel_info.h"
+#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_isolated_world_ids.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
@@ -1389,3 +1390,55 @@ bool ChromeContentRendererClient::ShouldEnforceWebRTCRoutingPreferences() {
return true;
#endif
}
+
+GURL ChromeContentRendererClient::OverrideFlashEmbedWithHTML(const GURL& url) {
+ if (!base::FeatureList::IsEnabled(features::kOverrideYouTubeFlashEmbed))
+ return GURL();
+
+ if (!url.is_valid())
+ return GURL();
+
+ // We'll only modify YouTube Flash embeds. The URLs can be recognized since
+ // they're in the following form: youtube.com/v/VIDEO_ID. So, we check to see
+ // if the given URL does follow that format.
+ if (!url.DomainIs("youtube.com") || url.path().find("/v/") != 0)
Mike West 2016/08/04 12:41:38 I'd suggest that we handle `youtube-nocookie.com`
kdsilva 2016/08/05 11:06:02 Done.
+ return GURL();
+
+ std::string url_str = url.spec();
+
+ // If the website is using an invalid YouTube URL, we'll try and
+ // fix the URL by ensuring that if there are multiple parameters,
+ // the parameter string begins with a "?" and then follows with a "&"
+ // for each subsequent parameter. We do this because the Flash video player
+ // has some URL correction capabilities so we don't want this move to HTML5
+ // to break webpages that used to work.
+ size_t index = url_str.find_first_of("&?");
+ bool invalid_url = index != std::string::npos && url_str.at(index) == '&';
+
+ if (invalid_url) {
+ // ? should appear first before all parameters
+ url_str.replace(index, 1, "?");
+
+ // Replace all instances of ? (after the first) with &
+ for (size_t pos = index + 1;
+ (pos = url_str.find("?",pos )) != std::string::npos; pos += 1)
Mike West 2016/08/04 12:41:38 Nit: Please add braces here, as the for loop is mu
kdsilva 2016/08/05 11:06:02 Done. Looks like `git cl format` did fix the inden
+ url_str.replace(pos, 1, "&");
+ }
+
+ GURL corrected_url = GURL(url_str);
Mike West 2016/08/04 12:41:38 Nit: Did you consider wrapping this up in the `url
kdsilva 2016/08/05 11:06:02 Ack.
+
+ // We don't modify any URLs that contain the enablejsapi=1 parameter
+ // since the page may be interacting with the YouTube Flash player in
+ // Javascript and we don't want to break working content.
+ if (corrected_url.query().find("enablejsapi=1") != std::string::npos)
Mike West 2016/08/04 12:41:38 This will also match `?XXXenablejsapi=1` and `?a=e
kdsilva 2016/08/05 11:06:02 Ack. Happy to discuss, but I think this is okay fo
+ return GURL();
+
+ // Change the path to use the YouTube HTML5 API
+ std::string path = corrected_url.path();
+ path.replace(path.find("/v/"), 3, "/embed/");
+
+ url::Replacements<char> r;
+ r.SetPath(path.c_str(), url::Component(0, path.length()));
+
+ return corrected_url.ReplaceComponents(r);
+}

Powered by Google App Engine
This is Rietveld 408576698