1/*
2 * Copyright (C) 2009 Torch Mobile, Inc. All rights reserved.
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Library General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Library General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Library General Public License
15 *  along with this library; see the file COPYING.LIB.  If not, write to
16 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 *  Boston, MA 02110-1301, USA.
18 */
19
20#if ENABLE(VIDEO)
21
22#include "config.h"
23#include "MediaPlayerProxy.h"
24
25#include "BridgeJSC.h"
26#include "DocumentLoader.h"
27#include "HTMLPlugInElement.h"
28#include "HTMLVideoElement.h"
29#include "JSDOMBinding.h"
30#include "JSPluginElementFunctions.h"
31#include "MediaPlayer.h"
32#include "Node.h"
33#include "PluginView.h"
34#include "RenderPartObject.h"
35#include "RenderWidget.h"
36#include "Widget.h"
37#include "c_class.h"
38#include "c_instance.h"
39#include "c_runtime.h"
40#include "npruntime_impl.h"
41#include <runtime/Identifier.h>
42#include <wtf/text/WTFString.h>
43
44using namespace JSC;
45
46namespace WebCore {
47
48using namespace Bindings;
49using namespace HTMLNames;
50
51WebMediaPlayerProxy::WebMediaPlayerProxy(MediaPlayer* player)
52    : m_mediaPlayer(player)
53    , m_init(false)
54    , m_hasSentResponseToPlugin(false)
55{
56    if (!m_init)
57        initEngine();
58}
59
60WebMediaPlayerProxy::~WebMediaPlayerProxy()
61{
62    m_instance.release();
63}
64
65ScriptInstance WebMediaPlayerProxy::pluginInstance()
66{
67    if (!m_instance) {
68        RenderObject* r = element()->renderer();
69        if (!r || !r->isWidget())
70            return 0;
71
72        Frame* frame = element()->document().frame();
73
74        RenderWidget* renderWidget = static_cast<RenderWidget*>(element()->renderer());
75        if (renderWidget && renderWidget->widget())
76            m_instance = frame->script().createScriptInstanceForWidget(renderWidget->widget());
77    }
78
79    return m_instance;
80}
81
82void WebMediaPlayerProxy::load(const String& url)
83{
84    if (!m_init)
85        initEngine();
86    if (m_init)
87        invokeMethod("play");
88}
89
90void WebMediaPlayerProxy::initEngine()
91{
92    HTMLMediaElement* element = toHTMLMediaElement(m_mediaPlayer->mediaPlayerClient());
93    String url = element->initialURL();
94
95    if (url.isEmpty())
96        return;
97
98    Frame* frame = element->document().frame();
99    Vector<String> paramNames;
100    Vector<String> paramValues;
101    String serviceType;
102
103    // add all attributes set on the embed object
104    if (element->hasAttributes()) {
105        for (unsigned i = 0; i < element->attributeCount(); ++i) {
106            Attribute* it = element->attributeItem(i);
107            paramNames.append(it->name().localName().string());
108            paramValues.append(it->value().string());
109        }
110    }
111    serviceType = "application/x-mplayer2";
112    frame->loader()->subframeLoader()->requestObject(static_cast<RenderPartObject*>(element->renderer()), url, nullAtom, serviceType, paramNames, paramValues);
113    m_init = true;
114
115}
116
117HTMLMediaElement* WebMediaPlayerProxy::element()
118{
119    return toHTMLMediaElement(m_mediaPlayer->mediaPlayerClient());
120
121}
122
123void WebMediaPlayerProxy::invokeMethod(const String& methodName)
124{
125    Frame* frame = element()->document().frame();
126    RootObject* root = frame->script().bindingRootObject();
127    if (!root)
128        return;
129    ExecState *exec = root->globalObject()->globalExec();
130    Instance* instance = pluginInstance().get();
131    if (!instance)
132        return;
133
134    instance->begin();
135    Class *aClass = instance->getClass();
136    Identifier iden(exec, methodName);
137    MethodList methodList = aClass->methodsNamed(iden, instance);
138    ArgList args;
139    instance->invokeMethod(exec, methodList , args);
140    instance->end();
141}
142
143}
144
145#endif
146