summaryrefslogtreecommitdiffstats
path: root/components/plugins/renderer/mobile_youtube_plugin.cc
blob: 9745a0a138e37371b137f3d8f14b42199e3fba15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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_frame.h"
#include "gin/object_template_builder.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "ui/base/webui/jstemplate_builder.h"

using blink::WebFrame;
using blink::WebPlugin;
using blink::WebURLRequest;

const char* const kSlashVSlash = "/v/";
const char* const kSlashESlash = "/e/";

namespace {

std::string GetYoutubeVideoId(const blink::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 blink::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 = base::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 {

gin::WrapperInfo MobileYouTubePlugin::kWrapperInfo = {gin::kEmbedderNativeGin};

MobileYouTubePlugin::MobileYouTubePlugin(content::RenderFrame* render_frame,
                                         blink::WebLocalFrame* frame,
                                         const blink::WebPluginParams& params,
                                         base::StringPiece& template_html)
    : PluginPlaceholderBase(render_frame,
                            frame,
                            params,
                            HtmlData(params, template_html)) {
}

MobileYouTubePlugin::~MobileYouTubePlugin() {}

// 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()) &&
         base::LowerCaseEqualsASCII(mime_type,
                                    content::kFlashPluginSwfMimeType);
}

void MobileYouTubePlugin::OpenYoutubeUrlCallback() {
  std::string youtube("vnd.youtube:");
  GURL url(youtube.append(GetYoutubeVideoId(GetPluginParams())));
  WebURLRequest request;
  request.initialize();
  request.setURL(url);
  render_frame()->LoadURLExternally(
      GetFrame(), request, blink::WebNavigationPolicyNewForegroundTab);
}

v8::Local<v8::Value> MobileYouTubePlugin::GetV8Handle(v8::Isolate* isolate) {
  return gin::CreateHandle(isolate, this).ToV8();
}

gin::ObjectTemplateBuilder MobileYouTubePlugin::GetObjectTemplateBuilder(
    v8::Isolate* isolate) {
  return gin::Wrappable<MobileYouTubePlugin>::GetObjectTemplateBuilder(isolate)
      .SetMethod("openYoutubeURL",
                 &MobileYouTubePlugin::OpenYoutubeUrlCallback);
}

}  // namespace plugins