Home | History | Annotate | Download | only in PluginProcess
      1 /*
      2  * Copyright (C) 2010 Apple 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
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "WebProcessConnection.h"
     28 
     29 #if ENABLE(PLUGIN_PROCESS)
     30 
     31 #include "NPRemoteObjectMap.h"
     32 #include "PluginControllerProxy.h"
     33 #include "PluginProcess.h"
     34 #include "RunLoop.h"
     35 
     36 namespace WebKit {
     37 
     38 PassRefPtr<WebProcessConnection> WebProcessConnection::create(CoreIPC::Connection::Identifier connectionIdentifier)
     39 {
     40     return adoptRef(new WebProcessConnection(connectionIdentifier));
     41 }
     42 
     43 WebProcessConnection::~WebProcessConnection()
     44 {
     45     ASSERT(m_pluginControllers.isEmpty());
     46     ASSERT(!m_npRemoteObjectMap);
     47     ASSERT(!m_connection);
     48 }
     49 
     50 WebProcessConnection::WebProcessConnection(CoreIPC::Connection::Identifier connectionIdentifier)
     51 {
     52     m_connection = CoreIPC::Connection::createServerConnection(connectionIdentifier, this, RunLoop::main());
     53     m_npRemoteObjectMap = NPRemoteObjectMap::create(m_connection.get());
     54 
     55     m_connection->setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(true);
     56     m_connection->open();
     57 }
     58 
     59 void WebProcessConnection::addPluginControllerProxy(PassOwnPtr<PluginControllerProxy> pluginController)
     60 {
     61     uint64_t pluginInstanceID = pluginController->pluginInstanceID();
     62 
     63     ASSERT(!m_pluginControllers.contains(pluginInstanceID));
     64     m_pluginControllers.set(pluginInstanceID, pluginController.leakPtr());
     65 }
     66 
     67 void WebProcessConnection::destroyPluginControllerProxy(PluginControllerProxy* pluginController)
     68 {
     69     // This may end up calling removePluginControllerProxy which ends up deleting
     70     // the WebProcessConnection object if this was the last object.
     71     pluginController->destroy();
     72 }
     73 
     74 void WebProcessConnection::removePluginControllerProxy(PluginControllerProxy* pluginController, Plugin* plugin)
     75 {
     76     {
     77         ASSERT(m_pluginControllers.contains(pluginController->pluginInstanceID()));
     78 
     79         OwnPtr<PluginControllerProxy> pluginControllerOwnPtr = adoptPtr(m_pluginControllers.take(pluginController->pluginInstanceID()));
     80         ASSERT(pluginControllerOwnPtr == pluginController);
     81     }
     82 
     83     // Invalidate all objects related to this plug-in.
     84     if (plugin)
     85         m_npRemoteObjectMap->pluginDestroyed(plugin);
     86 
     87     if (!m_pluginControllers.isEmpty())
     88         return;
     89 
     90     m_npRemoteObjectMap = nullptr;
     91 
     92     // The last plug-in went away, close this connection.
     93     m_connection->invalidate();
     94     m_connection = nullptr;
     95 
     96     // This will cause us to be deleted.
     97     PluginProcess::shared().removeWebProcessConnection(this);
     98 }
     99 
    100 void WebProcessConnection::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
    101 {
    102     if (!arguments->destinationID()) {
    103         ASSERT_NOT_REACHED();
    104         return;
    105     }
    106 
    107     PluginControllerProxy* pluginControllerProxy = m_pluginControllers.get(arguments->destinationID());
    108     if (!pluginControllerProxy)
    109         return;
    110 
    111     PluginController::PluginDestructionProtector protector(pluginControllerProxy->asPluginController());
    112 
    113     pluginControllerProxy->didReceivePluginControllerProxyMessage(connection, messageID, arguments);
    114 }
    115 
    116 CoreIPC::SyncReplyMode WebProcessConnection::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, CoreIPC::ArgumentEncoder* reply)
    117 {
    118     uint64_t destinationID = arguments->destinationID();
    119 
    120     if (!destinationID)
    121         return didReceiveSyncWebProcessConnectionMessage(connection, messageID, arguments, reply);
    122 
    123     if (messageID.is<CoreIPC::MessageClassNPObjectMessageReceiver>())
    124         return m_npRemoteObjectMap->didReceiveSyncMessage(connection, messageID, arguments, reply);
    125 
    126     PluginControllerProxy* pluginControllerProxy = m_pluginControllers.get(arguments->destinationID());
    127     if (!pluginControllerProxy)
    128         return CoreIPC::AutomaticReply;
    129 
    130     PluginController::PluginDestructionProtector protector(pluginControllerProxy->asPluginController());
    131     CoreIPC::SyncReplyMode replyMode = pluginControllerProxy->didReceiveSyncPluginControllerProxyMessage(connection, messageID, arguments, reply);
    132 
    133     return replyMode;
    134 }
    135 
    136 void WebProcessConnection::didClose(CoreIPC::Connection*)
    137 {
    138     // The web process crashed. Destroy all the plug-in controllers. Destroying the last plug-in controller
    139     // will cause the web process connection itself to be destroyed.
    140     Vector<PluginControllerProxy*> pluginControllers;
    141     copyValuesToVector(m_pluginControllers, pluginControllers);
    142 
    143     for (size_t i = 0; i < pluginControllers.size(); ++i)
    144         destroyPluginControllerProxy(pluginControllers[i]);
    145 }
    146 
    147 void WebProcessConnection::destroyPlugin(uint64_t pluginInstanceID)
    148 {
    149     PluginControllerProxy* pluginControllerProxy = m_pluginControllers.get(pluginInstanceID);
    150     ASSERT(pluginControllerProxy);
    151 
    152     destroyPluginControllerProxy(pluginControllerProxy);
    153 }
    154 
    155 void WebProcessConnection::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID)
    156 {
    157     // FIXME: Implement.
    158 }
    159 
    160 void WebProcessConnection::syncMessageSendTimedOut(CoreIPC::Connection*)
    161 {
    162 }
    163 
    164 void WebProcessConnection::createPlugin(uint64_t pluginInstanceID, const Plugin::Parameters& parameters, const String& userAgent, bool isPrivateBrowsingEnabled, bool isAcceleratedCompositingEnabled, bool& result, uint32_t& remoteLayerClientID)
    165 {
    166     OwnPtr<PluginControllerProxy> pluginControllerProxy = PluginControllerProxy::create(this, pluginInstanceID, userAgent, isPrivateBrowsingEnabled, isAcceleratedCompositingEnabled);
    167 
    168     PluginControllerProxy* pluginControllerProxyPtr = pluginControllerProxy.get();
    169 
    170     // Make sure to add the proxy to the map before initializing it, since the plug-in might call out to the web process from
    171     // its NPP_New function. This will hand over ownership of the proxy to the web process connection.
    172     addPluginControllerProxy(pluginControllerProxy.release());
    173 
    174     // Now try to initialize the plug-in.
    175     result = pluginControllerProxyPtr->initialize(parameters);
    176 
    177     if (!result)
    178         return;
    179 
    180 #if PLATFORM(MAC)
    181     remoteLayerClientID = pluginControllerProxyPtr->remoteLayerClientID();
    182 #endif
    183 }
    184 
    185 } // namespace WebKit
    186 
    187 #endif // ENABLE(PLUGIN_PROCESS)
    188