1// Copyright 2014 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#if defined(ENABLE_PEPPER_CDMS)
6#include "content/renderer/media/crypto/pepper_cdm_wrapper_impl.h"
7
8#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
9#include "content/renderer/pepper/pepper_webplugin_impl.h"
10#include "third_party/WebKit/public/platform/WebString.h"
11#include "third_party/WebKit/public/web/WebDocument.h"
12#include "third_party/WebKit/public/web/WebElement.h"
13#include "third_party/WebKit/public/web/WebFrame.h"
14#include "third_party/WebKit/public/web/WebHelperPlugin.h"
15#include "third_party/WebKit/public/web/WebPlugin.h"
16#include "third_party/WebKit/public/web/WebPluginContainer.h"
17#include "third_party/WebKit/public/web/WebView.h"
18
19namespace content {
20
21void WebHelperPluginDeleter::operator()(blink::WebHelperPlugin* plugin) const {
22  plugin->destroy();
23}
24
25scoped_ptr<PepperCdmWrapper> PepperCdmWrapperImpl::Create(
26    blink::WebLocalFrame* frame,
27    const std::string& pluginType,
28    const GURL& security_origin) {
29  DCHECK(frame);
30  ScopedHelperPlugin helper_plugin(blink::WebHelperPlugin::create(
31      blink::WebString::fromUTF8(pluginType), frame));
32  if (!helper_plugin)
33    return scoped_ptr<PepperCdmWrapper>();
34
35  blink::WebPlugin* plugin = helper_plugin->getPlugin();
36  DCHECK(!plugin->isPlaceholder());  // Prevented by Blink.
37
38  // Only Pepper plugins are supported, so it must ultimately be a ppapi object.
39  PepperWebPluginImpl* ppapi_plugin = static_cast<PepperWebPluginImpl*>(plugin);
40  scoped_refptr<PepperPluginInstanceImpl> plugin_instance =
41      ppapi_plugin->instance();
42  if (!plugin_instance.get())
43    return scoped_ptr<PepperCdmWrapper>();
44
45  GURL url(plugin_instance->container()->element().document().url());
46  CHECK_EQ(security_origin.GetOrigin(), url.GetOrigin())
47      << "Pepper instance has a different origin than the EME call.";
48
49  if (!plugin_instance->GetContentDecryptorDelegate())
50    return scoped_ptr<PepperCdmWrapper>();
51
52  return scoped_ptr<PepperCdmWrapper>(
53      new PepperCdmWrapperImpl(helper_plugin.Pass(), plugin_instance));
54}
55
56PepperCdmWrapperImpl::PepperCdmWrapperImpl(
57    ScopedHelperPlugin helper_plugin,
58    const scoped_refptr<PepperPluginInstanceImpl>& plugin_instance)
59    : helper_plugin_(helper_plugin.Pass()),
60      plugin_instance_(plugin_instance) {
61  DCHECK(helper_plugin_);
62  DCHECK(plugin_instance_.get());
63  // Plugin must be a CDM.
64  DCHECK(plugin_instance_->GetContentDecryptorDelegate());
65}
66
67PepperCdmWrapperImpl::~PepperCdmWrapperImpl() {
68  // Destroy the nested objects in reverse order.
69  plugin_instance_ = NULL;
70  helper_plugin_.reset();
71}
72
73ContentDecryptorDelegate* PepperCdmWrapperImpl::GetCdmDelegate() {
74  return plugin_instance_->GetContentDecryptorDelegate();
75}
76
77}  // namespace content
78
79#endif  // defined(ENABLE_PEPPER_CDMS)
80