28c2e0a7b868809aacafafcb49e080e8ced87d47
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebHelperPluginImpl.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "web/WebHelperPluginImpl.h"
33
34 #include "core/html/HTMLObjectElement.h"
35 #include "core/loader/FrameLoader.h"
36 #include "core/loader/FrameLoaderClient.h"
37 #include "public/web/WebPlugin.h"
38 #include "web/WebLocalFrameImpl.h"
39 #include "web/WebPluginContainerImpl.h"
40
41 namespace blink {
42
43 DEFINE_TYPE_CASTS(WebHelperPluginImpl, WebHelperPlugin, plugin, true, true);
44
45 WebHelperPlugin* WebHelperPlugin::create(const WebString& pluginType, WebLocalFrame* frame)
46 {
47     OwnPtr<WebHelperPlugin> plugin = adoptPtr<WebHelperPlugin>(new WebHelperPluginImpl());
48     if (!toWebHelperPluginImpl(plugin.get())->initialize(pluginType, toWebLocalFrameImpl(frame)))
49         return 0;
50     return plugin.leakPtr();
51 }
52
53 WebHelperPluginImpl::WebHelperPluginImpl()
54     : m_destructionTimer(this, &WebHelperPluginImpl::reallyDestroy)
55 {
56 }
57
58 WebHelperPluginImpl::~WebHelperPluginImpl()
59 {
60 #if ENABLE(OILPAN)
61     // FIXME: Oilpan: it is potentially problematic to support plugin
62     // disposal during an Oilpan GC. If it happens, we need to know
63     // and evaluate possible ways to handle it.
64     ASSERT(!ThreadState::current()->isSweepInProgress());
65     if (m_pluginContainer)
66         m_pluginContainer->dispose();
67 #endif
68 }
69
70 bool WebHelperPluginImpl::initialize(const String& pluginType, WebLocalFrameImpl* frame)
71 {
72     ASSERT(!m_objectElement && !m_pluginContainer);
73     if (!frame->frame()->loader().client())
74         return false;
75
76     m_objectElement = HTMLObjectElement::create(*frame->frame()->document(), 0, false);
77     Vector<String> attributeNames;
78     Vector<String> attributeValues;
79     ASSERT(frame->frame()->document()->url().isValid());
80     m_pluginContainer = adoptRefWillBeNoop(toWebPluginContainerImpl(frame->frame()->loader().client()->createPlugin(
81         m_objectElement.get(),
82         frame->frame()->document()->url(),
83         attributeNames,
84         attributeValues,
85         pluginType,
86         false,
87         FrameLoaderClient::AllowDetachedPlugin).leakRef()));
88
89     if (!m_pluginContainer)
90         return false;
91
92     // Getting a placeholder plugin is also failure, since it's not the plugin the caller needed.
93     return !getPlugin()->isPlaceholder();
94 }
95
96 void WebHelperPluginImpl::reallyDestroy(Timer<WebHelperPluginImpl>*)
97 {
98     delete this;
99 }
100
101 void WebHelperPluginImpl::destroy()
102 {
103     // Defer deletion so we don't do too much work when called via stopActiveDOMObjects().
104     // FIXME: It's not clear why we still need this. The original code held a Page and a
105     // WebFrame, and destroying it would cause JavaScript triggered by frame detach to run,
106     // which isn't allowed inside stopActiveDOMObjects(). Removing this causes one Chrome test
107     // to fail with a timeout.
108     m_destructionTimer.startOneShot(0, FROM_HERE);
109 }
110
111 WebPlugin* WebHelperPluginImpl::getPlugin()
112 {
113     ASSERT(m_pluginContainer);
114     ASSERT(m_pluginContainer->plugin());
115     return m_pluginContainer->plugin();
116 }
117
118 } // namespace blink