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

Unified Diff: components/plugins/renderer/mobile_youtube_plugin.cc

Issue 27197004: Move renderer plugin code into a new component (re-land) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move renderer plugin code into a new component (re-land) - fix nit and rebase Created 7 years, 2 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: components/plugins/renderer/mobile_youtube_plugin.cc
diff --git a/components/plugins/renderer/mobile_youtube_plugin.cc b/components/plugins/renderer/mobile_youtube_plugin.cc
new file mode 100644
index 0000000000000000000000000000000000000000..889443f3e0d408d0893a743457f5c18c9113ea44
--- /dev/null
+++ b/components/plugins/renderer/mobile_youtube_plugin.cc
@@ -0,0 +1,113 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/plugins/renderer/mobile_youtube_plugin.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/strings/string_piece.h"
+#include "base/strings/string_util.h"
+#include "base/values.h"
+#include "content/public/common/content_constants.h"
+#include "content/public/renderer/render_view.h"
+#include "third_party/WebKit/public/web/WebFrame.h"
+#include "ui/base/webui/jstemplate_builder.h"
+
+using WebKit::WebFrame;
+using WebKit::WebPlugin;
+using WebKit::WebURLRequest;
+
+const char* const kSlashVSlash = "/v/";
+const char* const kSlashESlash = "/e/";
+
+namespace {
+
+std::string GetYoutubeVideoId(const WebKit::WebPluginParams& params) {
+ GURL url(params.url);
+ std::string video_id = url.path().substr(strlen(kSlashVSlash));
+
+ // Extract just the video id
+ size_t video_id_end = video_id.find('&');
+ if (video_id_end != std::string::npos)
+ video_id = video_id.substr(0, video_id_end);
+ return video_id;
+}
+
+std::string HtmlData(const WebKit::WebPluginParams& params,
+ base::StringPiece template_html) {
+ base::DictionaryValue values;
+ values.SetString("video_id", GetYoutubeVideoId(params));
+ return webui::GetI18nTemplateHtml(template_html, &values);
+}
+
+bool IsValidYouTubeVideo(const std::string& path) {
+ unsigned len = strlen(kSlashVSlash);
+
+ // check for more than just /v/ or /e/.
+ if (path.length() <= len)
+ return false;
+
+ std::string str = StringToLowerASCII(path);
+ // Youtube flash url can start with /v/ or /e/.
+ if (strncmp(str.data(), kSlashVSlash, len) != 0 &&
+ strncmp(str.data(), kSlashESlash, len) != 0)
+ return false;
+
+ // Start after /v/
+ for (unsigned i = len; i < path.length(); i++) {
+ char c = str[i];
+ if (isalpha(c) || isdigit(c) || c == '_' || c == '-')
+ continue;
+ // The url can have more parameters such as &hl=en after the video id.
+ // Once we start seeing extra parameters we can return true.
+ return c == '&' && i > len;
+ }
+ return true;
+}
+
+} // namespace
+
+namespace plugins {
+
+MobileYouTubePlugin::MobileYouTubePlugin(content::RenderView* render_view,
+ WebKit::WebFrame* frame,
+ const WebKit::WebPluginParams& params,
+ base::StringPiece& template_html,
+ GURL placeholderDataUrl)
+ : PluginPlaceholder(render_view,
+ frame,
+ params,
+ HtmlData(params, template_html),
+ placeholderDataUrl) {}
+
+// static
+bool MobileYouTubePlugin::IsYouTubeURL(const GURL& url,
+ const std::string& mime_type) {
+ std::string host = url.host();
+ bool is_youtube = EndsWith(host, "youtube.com", true) ||
+ EndsWith(host, "youtube-nocookie.com", true);
+
+ return is_youtube && IsValidYouTubeVideo(url.path()) &&
+ LowerCaseEqualsASCII(mime_type, content::kFlashPluginSwfMimeType);
+}
+
+void MobileYouTubePlugin::OpenYoutubeUrlCallback(
+ const webkit_glue::CppArgumentList& args,
+ webkit_glue::CppVariant* result) {
+ std::string youtube("vnd.youtube:");
+ GURL url(youtube.append(GetYoutubeVideoId(GetPluginParams())));
+ WebURLRequest request;
+ request.initialize();
+ request.setURL(url);
+ render_view()->LoadURLExternally(
+ GetFrame(), request, WebKit::WebNavigationPolicyNewForegroundTab);
+}
+void MobileYouTubePlugin::BindWebFrame(WebFrame* frame) {
+ PluginPlaceholder::BindWebFrame(frame);
+ BindCallback("openYoutubeURL",
+ base::Bind(&MobileYouTubePlugin::OpenYoutubeUrlCallback,
+ base::Unretained(this)));
+}
+
+} // namespace plugins
« no previous file with comments | « components/plugins/renderer/mobile_youtube_plugin.h ('k') | components/plugins/renderer/plugin_placeholder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698