Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / plugins / renderer / mobile_youtube_plugin.cc
blob563ec264a724424108d62fbe3f379a1950c764e5
1 // Copyright 2013 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.
5 #include "components/plugins/renderer/mobile_youtube_plugin.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 #include "content/public/common/content_constants.h"
13 #include "content/public/renderer/render_frame.h"
14 #include "gin/object_template_builder.h"
15 #include "third_party/WebKit/public/web/WebFrame.h"
16 #include "ui/base/webui/jstemplate_builder.h"
18 using blink::WebFrame;
19 using blink::WebPlugin;
20 using blink::WebURLRequest;
22 const char* const kSlashVSlash = "/v/";
23 const char* const kSlashESlash = "/e/";
25 namespace {
27 std::string GetYoutubeVideoId(const blink::WebPluginParams& params) {
28 GURL url(params.url);
29 std::string video_id = url.path().substr(strlen(kSlashVSlash));
31 // Extract just the video id
32 size_t video_id_end = video_id.find('&');
33 if (video_id_end != std::string::npos)
34 video_id = video_id.substr(0, video_id_end);
35 return video_id;
38 std::string HtmlData(const blink::WebPluginParams& params,
39 base::StringPiece template_html) {
40 base::DictionaryValue values;
41 values.SetString("video_id", GetYoutubeVideoId(params));
42 return webui::GetI18nTemplateHtml(template_html, &values);
45 bool IsValidYouTubeVideo(const std::string& path) {
46 unsigned len = strlen(kSlashVSlash);
48 // check for more than just /v/ or /e/.
49 if (path.length() <= len)
50 return false;
52 // Youtube flash url can start with /v/ or /e/.
53 if (!base::StartsWith(path, kSlashVSlash,
54 base::CompareCase::INSENSITIVE_ASCII) ||
55 !base::StartsWith(path, kSlashESlash,
56 base::CompareCase::INSENSITIVE_ASCII))
57 return false;
59 // Start after /v/
60 for (unsigned i = len; i < path.length(); i++) {
61 char c = path[i];
62 if (isalpha(c) || isdigit(c) || c == '_' || c == '-')
63 continue;
64 // The url can have more parameters such as &hl=en after the video id.
65 // Once we start seeing extra parameters we can return true.
66 return c == '&' && i > len;
68 return true;
71 } // namespace
73 namespace plugins {
75 gin::WrapperInfo MobileYouTubePlugin::kWrapperInfo = {gin::kEmbedderNativeGin};
77 MobileYouTubePlugin::MobileYouTubePlugin(content::RenderFrame* render_frame,
78 blink::WebLocalFrame* frame,
79 const blink::WebPluginParams& params,
80 base::StringPiece& template_html)
81 : PluginPlaceholderBase(render_frame,
82 frame,
83 params,
84 HtmlData(params, template_html)) {
87 MobileYouTubePlugin::~MobileYouTubePlugin() {}
89 // static
90 bool MobileYouTubePlugin::IsYouTubeURL(const GURL& url,
91 const std::string& mime_type) {
92 std::string host = url.host();
93 bool is_youtube =
94 base::EndsWith(host, "youtube.com", base::CompareCase::SENSITIVE) ||
95 base::EndsWith(host, "youtube-nocookie.com",
96 base::CompareCase::SENSITIVE);
98 return is_youtube && IsValidYouTubeVideo(url.path()) &&
99 base::LowerCaseEqualsASCII(mime_type,
100 content::kFlashPluginSwfMimeType);
103 void MobileYouTubePlugin::OpenYoutubeUrlCallback() {
104 std::string youtube("vnd.youtube:");
105 GURL url(youtube.append(GetYoutubeVideoId(GetPluginParams())));
106 WebURLRequest request;
107 request.initialize();
108 request.setURL(url);
109 render_frame()->LoadURLExternally(
110 GetFrame(), request, blink::WebNavigationPolicyNewForegroundTab);
113 v8::Local<v8::Value> MobileYouTubePlugin::GetV8Handle(v8::Isolate* isolate) {
114 return gin::CreateHandle(isolate, this).ToV8();
117 gin::ObjectTemplateBuilder MobileYouTubePlugin::GetObjectTemplateBuilder(
118 v8::Isolate* isolate) {
119 return gin::Wrappable<MobileYouTubePlugin>::GetObjectTemplateBuilder(isolate)
120 .SetMethod("openYoutubeURL",
121 &MobileYouTubePlugin::OpenYoutubeUrlCallback);
124 } // namespace plugins