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