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"
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/";
27 std::string
GetYoutubeVideoId(const blink::WebPluginParams
& params
) {
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
);
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
)
52 std::string str
= base::StringToLowerASCII(path
);
53 // Youtube flash url can start with /v/ or /e/.
54 if (strncmp(str
.data(), kSlashVSlash
, len
) != 0 &&
55 strncmp(str
.data(), kSlashESlash
, len
) != 0)
59 for (unsigned i
= len
; i
< path
.length(); i
++) {
61 if (isalpha(c
) || isdigit(c
) || c
== '_' || c
== '-')
63 // The url can have more parameters such as &hl=en after the video id.
64 // Once we start seeing extra parameters we can return true.
65 return c
== '&' && i
> len
;
74 gin::WrapperInfo
MobileYouTubePlugin::kWrapperInfo
= {gin::kEmbedderNativeGin
};
76 MobileYouTubePlugin::MobileYouTubePlugin(content::RenderFrame
* render_frame
,
77 blink::WebLocalFrame
* frame
,
78 const blink::WebPluginParams
& params
,
79 base::StringPiece
& template_html
)
80 : PluginPlaceholderBase(render_frame
,
83 HtmlData(params
, template_html
)) {
86 MobileYouTubePlugin::~MobileYouTubePlugin() {}
89 bool MobileYouTubePlugin::IsYouTubeURL(const GURL
& url
,
90 const std::string
& mime_type
) {
91 std::string host
= url
.host();
92 bool is_youtube
= EndsWith(host
, "youtube.com", true) ||
93 EndsWith(host
, "youtube-nocookie.com", true);
95 return is_youtube
&& IsValidYouTubeVideo(url
.path()) &&
96 base::LowerCaseEqualsASCII(mime_type
,
97 content::kFlashPluginSwfMimeType
);
100 void MobileYouTubePlugin::OpenYoutubeUrlCallback() {
101 std::string
youtube("vnd.youtube:");
102 GURL
url(youtube
.append(GetYoutubeVideoId(GetPluginParams())));
103 WebURLRequest request
;
104 request
.initialize();
106 render_frame()->LoadURLExternally(
107 GetFrame(), request
, blink::WebNavigationPolicyNewForegroundTab
);
110 v8::Local
<v8::Value
> MobileYouTubePlugin::GetV8Handle(v8::Isolate
* isolate
) {
111 return gin::CreateHandle(isolate
, this).ToV8();
114 gin::ObjectTemplateBuilder
MobileYouTubePlugin::GetObjectTemplateBuilder(
115 v8::Isolate
* isolate
) {
116 return gin::Wrappable
<MobileYouTubePlugin
>::GetObjectTemplateBuilder(isolate
)
117 .SetMethod("openYoutubeURL",
118 &MobileYouTubePlugin::OpenYoutubeUrlCallback
);
121 } // namespace plugins