15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/child/npapi/plugin_host.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/lazy_instance.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/sys_string_conversions.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
17ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/child/npapi/plugin_instance.h"
18ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/child/npapi/plugin_lib.h"
19ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/child/npapi/plugin_stream_url.h"
20ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/child/npapi/webplugin_delegate.h"
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/common/content_client.h"
22c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch#include "content/public/common/content_switches.h"
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/common/user_agent.h"
24ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "content/public/common/webplugininfo.h"
25c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "net/base/filename_util.h"
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebBindings.h"
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "third_party/WebKit/public/web/WebKit.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/npapi/bindings/npruntime.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gl/gl_implementation.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gl/gl_surface.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/mac/mac_util.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)using blink::WebBindings;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Declarations for stub implementations of deprecated functions, which are no
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// longer listed in npapi.h.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* NPN_GetJavaEnv();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* NPN_GetJavaPeer(NPP);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochnamespace content {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Finds a PluginInstance from an NPP.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The caller must take a reference if needed.
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static PluginInstance* FindInstance(NPP id) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (id == NULL) {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return reinterpret_cast<PluginInstance*>(id->ndata);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if Core Animation plugins are supported. This requires that the
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OS supports shared accelerated surfaces via IOSurface. This is true on Snow
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Leopard and higher.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static bool SupportsCoreAnimationPlugins() {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      switches::kDisableCoreAnimationPlugins))
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We also need to be running with desktop GL and not the software
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // OSMesa renderer in order to share accelerated surfaces between
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // processes. Because on MacOS we lazy-initialize GLSurface in the
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // renderer process here, ensure we're not also initializing GL somewhere
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // else, and that we only do this once.
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static gfx::GLImplementation implementation = gfx::kGLImplementationNone;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (implementation == gfx::kGLImplementationNone) {
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Not initialized yet.
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DCHECK_EQ(implementation, gfx::GetGLImplementation())
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        << "GL already initialized by someone else to: "
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        << gfx::GetGLImplementation();
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!gfx::GLSurface::InitializeOneOff()) {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    implementation = gfx::GetGLImplementation();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (implementation == gfx::kGLImplementationDesktopGL);
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PluginHost::PluginHost() {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InitializeHostFuncs();
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PluginHost::~PluginHost() {
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PluginHost *PluginHost::Singleton() {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CR_DEFINE_STATIC_LOCAL(scoped_refptr<PluginHost>, singleton, ());
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (singleton.get() == NULL) {
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    singleton = new PluginHost();
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(singleton.get() != NULL);
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return singleton.get();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PluginHost::InitializeHostFuncs() {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  memset(&host_funcs_, 0, sizeof(host_funcs_));
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.size = sizeof(host_funcs_);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.version = (NP_VERSION_MAJOR << 8) | (NP_VERSION_MINOR);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The "basic" functions
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.geturl = &NPN_GetURL;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.posturl = &NPN_PostURL;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.requestread = &NPN_RequestRead;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.newstream = &NPN_NewStream;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.write = &NPN_Write;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.destroystream = &NPN_DestroyStream;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.status = &NPN_Status;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.uagent = &NPN_UserAgent;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.memalloc = &NPN_MemAlloc;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.memfree = &NPN_MemFree;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.memflush = &NPN_MemFlush;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.reloadplugins = &NPN_ReloadPlugins;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stubs for deprecated Java functions
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getJavaEnv = &NPN_GetJavaEnv;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getJavaPeer = &NPN_GetJavaPeer;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Advanced functions we implement
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.geturlnotify = &NPN_GetURLNotify;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.posturlnotify = &NPN_PostURLNotify;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getvalue = &NPN_GetValue;
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.setvalue = &NPN_SetValue;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.invalidaterect = &NPN_InvalidateRect;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.invalidateregion = &NPN_InvalidateRegion;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.forceredraw = &NPN_ForceRedraw;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These come from the Javascript Engine
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getstringidentifier = WebBindings::getStringIdentifier;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getstringidentifiers = WebBindings::getStringIdentifiers;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getintidentifier = WebBindings::getIntIdentifier;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.identifierisstring = WebBindings::identifierIsString;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.utf8fromidentifier = WebBindings::utf8FromIdentifier;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.intfromidentifier = WebBindings::intFromIdentifier;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.createobject = WebBindings::createObject;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.retainobject = WebBindings::retainObject;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.releaseobject = WebBindings::releaseObject;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.invoke = WebBindings::invoke;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.invokeDefault = WebBindings::invokeDefault;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.evaluate = WebBindings::evaluate;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getproperty = WebBindings::getProperty;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.setproperty = WebBindings::setProperty;
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.removeproperty = WebBindings::removeProperty;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.hasproperty = WebBindings::hasProperty;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.hasmethod = WebBindings::hasMethod;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.releasevariantvalue = WebBindings::releaseVariantValue;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.setexception = WebBindings::setException;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.pushpopupsenabledstate = NPN_PushPopupsEnabledState;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.poppopupsenabledstate = NPN_PopPopupsEnabledState;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.enumerate = WebBindings::enumerate;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.pluginthreadasynccall = NPN_PluginThreadAsyncCall;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.construct = WebBindings::construct;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getvalueforurl = NPN_GetValueForURL;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.setvalueforurl = NPN_SetValueForURL;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.getauthenticationinfo = NPN_GetAuthenticationInfo;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.scheduletimer = NPN_ScheduleTimer;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.unscheduletimer = NPN_UnscheduleTimer;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.popupcontextmenu = NPN_PopUpContextMenu;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.convertpoint = NPN_ConvertPoint;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.handleevent = NPN_HandleEvent;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.unfocusinstance = NPN_UnfocusInstance;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  host_funcs_.urlredirectresponse = NPN_URLRedirectResponse;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void PluginHost::PatchNPNetscapeFuncs(NPNetscapeFuncs* overrides) {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When running in the plugin process, we need to patch the NPN functions
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that the plugin calls to interact with NPObjects that we give.  Otherwise
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the plugin will call the v8 NPN functions, which won't work since we have
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an NPObjectProxy and not a real v8 implementation.
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->invoke)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.invoke = overrides->invoke;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->invokeDefault)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.invokeDefault = overrides->invokeDefault;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->evaluate)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.evaluate = overrides->evaluate;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->getproperty)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.getproperty = overrides->getproperty;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->setproperty)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.setproperty = overrides->setproperty;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->removeproperty)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.removeproperty = overrides->removeproperty;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->hasproperty)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.hasproperty = overrides->hasproperty;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->hasmethod)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.hasmethod = overrides->hasmethod;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->setexception)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.setexception = overrides->setexception;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overrides->enumerate)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_funcs_.enumerate = overrides->enumerate;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PluginHost::SetPostData(const char* buf,
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             uint32 length,
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             std::vector<std::string>* names,
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             std::vector<std::string>* values,
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             std::vector<char>* body) {
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use a state table to do the parsing.  Whitespace must be
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // trimmed after the fact if desired.  In our case, we actually
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // don't care about the whitespace, because we're just going to
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pass this back into another POST.  This function strips out the
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "Content-length" header and does not append it to the request.
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This parser takes action only on state changes.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Transition table:
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //                  :       \n  NULL    Other
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 0 GetHeader      1       2   4       0
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 1 GetValue       1       0   3       1
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 2 GetData        2       2   3       2
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 3 DONE
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 4 ERR
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum { INPUT_COLON=0, INPUT_NEWLINE, INPUT_NULL, INPUT_OTHER };
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum { GETNAME, GETVALUE, GETDATA, DONE, ERR };
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int statemachine[3][4] = { { GETVALUE, GETDATA, GETDATA, GETNAME },
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             { GETVALUE, GETNAME, DONE, GETVALUE },
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             { GETDATA,  GETDATA, DONE, GETDATA } };
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string name, value;
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* ptr = static_cast<const char*>(buf);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* start = ptr;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int state = GETNAME;  // initial state
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool done = false;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool err = false;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  do {
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int input;
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Translate the current character into an input
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // for the state table.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (*ptr) {
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case ':' :
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        input = INPUT_COLON;
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case '\n':
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        input = INPUT_NEWLINE;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case 0   :
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        input = INPUT_NULL;
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      default  :
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        input = INPUT_OTHER;
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int newstate = statemachine[state][input];
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Take action based on the new state.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (state != newstate) {
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      switch (newstate) {
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        case GETNAME:
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          // Got a value.
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          value = std::string(start, ptr - start);
267a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::TrimWhitespace(value, base::TRIM_ALL, &value);
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          // If the name field is empty, we'll skip this header
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          // but we won't error out.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (!name.empty() && name != "content-length") {
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            names->push_back(name);
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            values->push_back(value);
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          }
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          start = ptr + 1;
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          break;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        case GETVALUE:
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          // Got a header.
2786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          name = base::StringToLowerASCII(std::string(start, ptr - start));
279a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::TrimWhitespace(name, base::TRIM_ALL, &name);
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          start = ptr + 1;
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          break;
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        case GETDATA: {
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          // Finished headers, now get body
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (*ptr)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            start = ptr + 1;
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          size_t previous_size = body->size();
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          size_t new_body_size = length - static_cast<int>(start - buf);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          body->resize(previous_size + new_body_size);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (!body->empty())
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            memcpy(&body->front() + previous_size, start, new_body_size);
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          done = true;
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          break;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        case ERR:
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          // error
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          err = true;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          done = true;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          break;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    state = newstate;
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ptr++;
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } while (!done);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return !err;
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
308ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}  // namespace content
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" {
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
312ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochusing content::FindInstance;
313ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochusing content::PluginHost;
314ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochusing content::PluginInstance;
315ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochusing content::WebPlugin;
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Allocates memory from the host's memory space.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* NPN_MemAlloc(uint32_t size) {
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note: We must use the same allocator/deallocator
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that is used by the javascript library, as some of the
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // JS APIs will pass memory to the plugin which the plugin
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will attempt to free.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return malloc(size);
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Deallocates memory from the host's memory space
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_MemFree(void* ptr) {
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ptr != NULL && ptr != reinterpret_cast<void*>(-1))
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    free(ptr);
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Requests that the host free a specified amount of memory.
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint32_t NPN_MemFlush(uint32_t size) {
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is not relevant on Windows; MAC specific
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return size;
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is for dynamic discovery of new plugins.
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Should force a re-scan of the plugins directory to load new ones.
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_ReloadPlugins(NPBool reload_pages) {
341f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  blink::resetPluginCache(reload_pages ? true : false);
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Requests a range of bytes for a seekable stream.
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_RequestRead(NPStream* stream, NPByteRange* range_list) {
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!stream || !range_list)
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_GENERIC_ERROR;
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      reinterpret_cast<PluginInstance*>(stream->ndata));
351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!plugin.get())
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_GENERIC_ERROR;
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  plugin->RequestRead(stream, range_list);
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_NO_ERROR;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generic form of GetURL for common code between GetURL and GetURLNotify.
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static NPError GetURLNotify(NPP id,
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const char* url,
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const char* target,
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            bool notify,
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            void* notify_data) {
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_URL;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
368868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!plugin.get()) {
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_GENERIC_ERROR;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  plugin->RequestURL(url, "GET", target, NULL, 0, notify, notify_data);
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_NO_ERROR;
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Requests creation of a new stream with the contents of the
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// specified URL; gets notification of the result.
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_GetURLNotify(NPP id,
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const char* url,
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const char* target,
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         void* notify_data) {
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is identical to NPN_GetURL, but after finishing, the
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // browser will call NPP_URLNotify to inform the plugin that
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it has completed.
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // According to the NPAPI documentation, if target == _self
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or a parent to _self, the browser should return NPERR_INVALID_PARAM,
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // because it can't notify the plugin once deleted.  This is
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // absolutely false; firefox doesn't do this, and Flash relies on
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // being able to use this.
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Also according to the NPAPI documentation, we should return
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NPERR_INVALID_URL if the url requested is not valid.  However,
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this would require that we synchronously start fetching the
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URL.  That just isn't practical.  As such, there really is
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // no way to return this error.  From looking at the Firefox
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // implementation, it doesn't look like Firefox does this either.
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetURLNotify(id, url, target, true, notify_data);
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_GetURL(NPP id, const char* url, const char* target) {
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notes:
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    Request from the Plugin to fetch content either for the plugin
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    or to be placed into a browser window.
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If target == null, the browser fetches content and streams to plugin.
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    otherwise, the browser loads content into an existing browser frame.
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the target is the window/frame containing the plugin, the plugin
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    may be destroyed.
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the target is _blank, a mailto: or news: url open content in a new
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    browser window
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the target is _self, no other instance of the plugin is created.  The
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    plugin continues to operate in its own window
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetURLNotify(id, url, target, false, 0);
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generic form of PostURL for common code between PostURL and PostURLNotify.
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static NPError PostURLNotify(NPP id,
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const char* url,
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const char* target,
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             uint32_t len,
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const char* buf,
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             NPBool file,
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             bool notify,
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             void* notify_data) {
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url)
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_URL;
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
432868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!plugin.get()) {
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_GENERIC_ERROR;
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string post_file_contents;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (file) {
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Post data to be uploaded from a file. This can be handled in two
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // ways.
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // 1. Read entire file and send the contents as if it was a post data
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //    specified in the argument
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // 2. Send just the file details and read them in the browser at the
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //    time of sending the request.
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Approach 2 is more efficient but complicated. Approach 1 has a major
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // drawback of sending potentially large data over two IPC hops.  In a way
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // 'large data over IPC' problem exists as it is in case of plugin giving
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the data directly instead of in a file.
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Currently we are going with the approach 1 to get the feature working.
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We can optimize this later with approach 2.
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(joshia): Design a scheme to send a file descriptor instead of
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // entire file contents across.
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Security alert:
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // ---------------
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Here we are blindly uploading whatever file requested by a plugin.
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // This is risky as someone could exploit a plugin to send private
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // data in arbitrary locations.
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // A malicious (non-sandboxed) plugin has unfeterred access to OS
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // resources and can do this anyway without using browser's HTTP stack.
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // FWIW, Firefox and Safari don't perform any security checks.
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!buf)
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_FILE_NOT_FOUND;
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string file_path_ascii(buf);
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath file_path;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static const char kFileUrlPrefix[] = "file:";
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (StartsWithASCII(file_path_ascii, kFileUrlPrefix, false)) {
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GURL file_url(file_path_ascii);
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(file_url.SchemeIsFile());
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net::FileURLToFilePath(file_url, &file_path);
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
476424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      file_path = base::FilePath::FromUTF8Unsafe(file_path_ascii);
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::File::Info post_file_info;
480a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (!base::GetFileInfo(file_path, &post_file_info) ||
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        post_file_info.is_directory)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_FILE_NOT_FOUND;
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
48458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    if (!base::ReadFileToString(file_path, &post_file_contents))
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_FILE_NOT_FOUND;
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    buf = post_file_contents.c_str();
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    len = post_file_contents.size();
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The post data sent by a plugin contains both headers
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and post data.  Example:
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //      Content-type: text/html
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //      Content-length: 200
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //      <200 bytes of content here>
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unfortunately, our stream needs these broken apart,
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so we need to parse the data and set headers and data
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // separately.
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  plugin->RequestURL(url, "POST", target, buf, len, notify, notify_data);
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_NO_ERROR;
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_PostURLNotify(NPP id,
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const char* url,
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const char* target,
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          uint32_t len,
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const char* buf,
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          NPBool file,
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          void* notify_data) {
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return PostURLNotify(id, url, target, len, buf, file, true, notify_data);
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_PostURL(NPP id,
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const char* url,
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const char* target,
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    uint32_t len,
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const char* buf,
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    NPBool file) {
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // POSTs data to an URL, either from a temp file or a buffer.
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If file is true, buf contains a temp file (which host will delete after
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   completing), and len contains the length of the filename.
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If file is false, buf contains the data to send, and len contains the
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   length of the buffer
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If target is null,
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   server response is returned to the plugin
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If target is _current, _self, or _top,
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   server response is written to the plugin window and plugin is unloaded.
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If target is _new or _blank,
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   server response is written to a new browser window
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If target is an existing frame,
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   server response goes to that frame.
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For protocols other than FTP
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   file uploads must be line-end converted from \r\n to \n
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note:  you cannot specify headers (even a blank line) in a memory buffer,
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //        use NPN_PostURLNotify
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return PostURLNotify(id, url, target, len, buf, file, false, 0);
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_NewStream(NPP id,
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      NPMIMEType type,
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const char* target,
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      NPStream** stream) {
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Requests creation of a new data stream produced by the plugin,
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // consumed by the browser.
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Browser should put this stream into a window target.
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement me
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "NPN_NewStream is not implemented yet.";
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_GENERIC_ERROR;
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int32_t NPN_Write(NPP id, NPStream* stream, int32_t len, void* buffer) {
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Writes data to an existing Plugin-created stream.
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement me
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "NPN_Write is not implemented yet.";
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_GENERIC_ERROR;
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_DestroyStream(NPP id, NPStream* stream, NPReason reason) {
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Destroys a stream (could be created by plugin or browser).
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Reasons:
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPRES_DONE          - normal completion
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPRES_USER_BREAK    - user terminated
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPRES_NETWORK_ERROR - network error (all errors fit here?)
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (plugin.get() == NULL) {
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_GENERIC_ERROR;
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return plugin->NPP_DestroyStream(stream, reason);
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* NPN_UserAgent(NPP id) {
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Flash passes in a null id during the NP_initialize call.  We need to
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // default to the Mozilla user agent if we don't have an NPP instance or
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // else Flash won't request windowless mode.
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool use_mozilla_user_agent = true;
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (id) {
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_refptr<PluginInstance> plugin = FindInstance(id);
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (plugin.get() && !plugin->use_mozilla_user_agent())
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      use_mozilla_user_agent = false;
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (use_mozilla_user_agent)
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) "
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "Gecko/20061103 Firefox/2.0a1";
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Provide a consistent user-agent string with memory that lasts
6040529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // long enough for the caller to read it.
6050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  static base::LazyInstance<std::string>::Leaky leaky_user_agent =
6060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    LAZY_INSTANCE_INITIALIZER;
6070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (leaky_user_agent == NULL)
6080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    leaky_user_agent.Get() = content::GetContentClient()->GetUserAgent();
6090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return leaky_user_agent.Get().c_str();
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_Status(NPP id, const char* message) {
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Displays a message on the status line of the browser window.
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement me
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "NPN_Status is not implemented yet.";
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_InvalidateRect(NPP id, NPRect *invalidRect) {
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Invalidates specified drawing area prior to repainting or refreshing a
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // windowless plugin
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Before a windowless plugin can refresh part of its drawing area, it must
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // first invalidate it.  This function causes the NPP_HandleEvent method to
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pass an update event or a paint message to the plug-in.  After calling
626010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // this method, the plug-in receives a paint message asynchronously.
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The browser redraws invalid areas of the document and any windowless
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // plug-ins at regularly timed intervals. To force a paint message, the
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // plug-in can call NPN_ForceRedraw after calling this method.
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (plugin.get() && plugin->webplugin()) {
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (invalidRect) {
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!plugin->windowless()) {
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        RECT rect = {0};
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rect.left = invalidRect->left;
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rect.right = invalidRect->right;
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rect.top = invalidRect->top;
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rect.bottom = invalidRect->bottom;
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ::InvalidateRect(plugin->window_handle(), &rect, false);
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return;
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      gfx::Rect rect(invalidRect->left,
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     invalidRect->top,
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     invalidRect->right - invalidRect->left,
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     invalidRect->bottom - invalidRect->top);
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      plugin->webplugin()->InvalidateRect(rect);
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      plugin->webplugin()->Invalidate();
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_InvalidateRegion(NPP id, NPRegion invalidRegion) {
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Invalidates a specified drawing region prior to repainting
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or refreshing a window-less plugin.
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Similar to NPN_InvalidateRect.
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: this is overkill--add platform-specific region handling (at the
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // very least, fetch the region's bounding box and pass it to InvalidateRect).
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(plugin.get() != NULL);
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (plugin.get() && plugin->webplugin())
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    plugin->webplugin()->Invalidate();
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_ForceRedraw(NPP id) {
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Forces repaint for a windowless plug-in.
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We deliberately do not implement this; we don't want plugins forcing
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // synchronous paints.
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_GetValue(NPP id, NPNVariable variable, void* value) {
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allows the plugin to query the browser for information
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Variables:
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPNVxDisplay (unix only)
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPNVxtAppContext (unix only)
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPNVnetscapeWindow (win only) - Gets the native window on which the
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //              plug-in drawing occurs, returns HWND
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPNVjavascriptEnabledBool:  tells whether Javascript is enabled
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPNVasdEnabledBool:  tells whether SmartUpdate is enabled
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //    NPNVOfflineBool: tells whether offline-mode is enabled
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NPError rv = NPERR_GENERIC_ERROR;
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (static_cast<int>(variable)) {
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVWindowNPObject: {
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin(FindInstance(id));
695868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!plugin.get()) {
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_INVALID_INSTANCE_ERROR;
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPObject *np_object = plugin->webplugin()->GetWindowScriptNPObject();
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Return value is expected to be retained, as
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // described here:
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess>
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (np_object) {
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        WebBindings::retainObject(np_object);
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        void **v = (void **)value;
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        *v = np_object;
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rv = NPERR_NO_ERROR;
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVPluginElementNPObject: {
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin(FindInstance(id));
715868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!plugin.get()) {
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_INVALID_INSTANCE_ERROR;
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPObject *np_object = plugin->webplugin()->GetPluginElement();
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Return value is expected to be retained, as
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // described here:
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // <http://www.mozilla.org/projects/plugins/npruntime.html#browseraccess>
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (np_object) {
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        WebBindings::retainObject(np_object);
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        void** v = static_cast<void**>(value);
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        *v = np_object;
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rv = NPERR_NO_ERROR;
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #if !defined(OS_MACOSX)  // OS X doesn't have windowed plugins.
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVnetscapeWindow: {
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin = FindInstance(id);
736868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!plugin.get()) {
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_INVALID_INSTANCE_ERROR;
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      gfx::PluginWindowHandle handle = plugin->window_handle();
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *((void**)value) = (void*)handle;
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #endif
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVjavascriptEnabledBool: {
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // yes, JS is enabled.
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *((void**)value) = (void*)1;
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVSupportsWindowless: {
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPBool* supports_windowless = reinterpret_cast<NPBool*>(value);
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *supports_windowless = true;
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVprivateModeBool: {
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPBool* private_mode = reinterpret_cast<NPBool*>(value);
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin(FindInstance(id));
761868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!plugin.get()) {
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_INVALID_INSTANCE_ERROR;
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *private_mode = plugin->webplugin()->IsOffTheRecord();
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #if defined(OS_MACOSX)
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVpluginDrawingModel: {
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // return the drawing model that was negotiated when we initialized.
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin(FindInstance(id));
7737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (!plugin.get()) {
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_INVALID_INSTANCE_ERROR;
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *reinterpret_cast<int*>(value) = plugin->drawing_model();
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsCoreGraphicsBool:
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsCocoaBool: {
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // These drawing and event models are always supported.
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPBool* supports_model = reinterpret_cast<NPBool*>(value);
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *supports_model = true;
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsInvalidatingCoreAnimationBool:
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsCoreAnimationBool: {
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPBool* supports_model = reinterpret_cast<NPBool*>(value);
792ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      *supports_model = content::SupportsCoreAnimationPlugins();
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NP_NO_CARBON
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsCarbonBool:
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NP_NO_QUICKDRAW
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsQuickDrawBool:
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsOpenGLBool: {
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // These models are never supported. OpenGL was never widely supported,
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // and QuickDraw and Carbon have been deprecated for quite some time.
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPBool* supports_model = reinterpret_cast<NPBool*>(value);
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *supports_model = false;
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsCompositingCoreAnimationPluginsBool: {
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPBool* supports_compositing = reinterpret_cast<NPBool*>(value);
812ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      *supports_compositing = content::SupportsCoreAnimationPlugins();
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNVsupportsUpdatedCocoaTextInputBool: {
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We support the clarifications to the Cocoa IME event spec.
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NPBool* supports_update = reinterpret_cast<NPBool*>(value);
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *supports_update = true;
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      rv = NPERR_NO_ERROR;
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #endif  // OS_MACOSX
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DVLOG(1) << "NPN_GetValue(" << variable << ") is not implemented yet.";
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return rv;
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_SetValue(NPP id, NPPVariable variable, void* value) {
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allows the plugin to set various modes
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
835868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!plugin.get()) {
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_INSTANCE_ERROR;
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch(variable) {
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPPVpluginWindowBool: {
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Sets windowless mode for display of the plugin
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Note: the documentation at
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // http://developer.mozilla.org/en/docs/NPN_SetValue is wrong.  When
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // value is NULL, the mode is set to true.  This is the same way Mozilla
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // works.
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      plugin->set_windowless(value == 0);
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_NO_ERROR;
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPPVpluginTransparentBool: {
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Sets transparent mode for display of the plugin
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      //
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Transparent plugins require the browser to paint the background
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // before having the plugin paint.  By default, windowless plugins
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // are transparent.  Making a windowless plugin opaque means that
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // the plugin does not require the browser to paint the background.
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool mode = (value != 0);
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      plugin->set_transparent(mode);
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_NO_ERROR;
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPPVjavascriptPushCallerBool:
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Specifies whether you are pushing or popping the JSContext off.
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // the stack
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // TODO: implement me
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DVLOG(1) << "NPN_SetValue(NPPVJavascriptPushCallerBool) is not "
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  "implemented.";
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_GENERIC_ERROR;
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPPVpluginKeepLibraryInMemory:
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Tells browser that plugin library should live longer than usual.
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // TODO: implement me
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DVLOG(1) << "NPN_SetValue(NPPVpluginKeepLibraryInMemory) is not "
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  "implemented.";
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_GENERIC_ERROR;
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #if defined(OS_MACOSX)
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPPVpluginDrawingModel: {
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      intptr_t model = reinterpret_cast<intptr_t>(value);
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (model == NPDrawingModelCoreGraphics ||
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          ((model == NPDrawingModelInvalidatingCoreAnimation ||
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            model == NPDrawingModelCoreAnimation) &&
879ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch           content::SupportsCoreAnimationPlugins())) {
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        plugin->set_drawing_model(static_cast<NPDrawingModel>(model));
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_NO_ERROR;
8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_GENERIC_ERROR;
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPPVpluginEventModel: {
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Only the Cocoa event model is supported.
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      intptr_t model = reinterpret_cast<intptr_t>(value);
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (model == NPEventModelCocoa) {
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        plugin->set_event_model(static_cast<NPEventModel>(model));
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_NO_ERROR;
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_GENERIC_ERROR;
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  #endif
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // TODO: implement me
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DVLOG(1) << "NPN_SetValue(" << variable << ") is not implemented.";
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTREACHED();
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_GENERIC_ERROR;
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* NPN_GetJavaEnv() {
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement me
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "NPN_GetJavaEnv is not implemented.";
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void* NPN_GetJavaPeer(NPP) {
9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement me
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DVLOG(1) << "NPN_GetJavaPeer is not implemented.";
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_PushPopupsEnabledState(NPP id, NPBool enabled) {
9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
919868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (plugin.get())
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    plugin->PushPopupsEnabledState(enabled ? true : false);
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_PopPopupsEnabledState(NPP id) {
9245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
925868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (plugin.get())
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    plugin->PopPopupsEnabledState();
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_PluginThreadAsyncCall(NPP id,
9305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               void (*func)(void*),
9315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               void* user_data) {
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
933868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (plugin.get())
9345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    plugin->PluginThreadAsyncCall(func, user_data);
9355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_GetValueForURL(NPP id,
9385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           NPNURLVariable variable,
9395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           const char* url,
9405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           char** value,
9415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           uint32_t* len) {
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!id)
9435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_PARAM;
9445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url || !*url || !len)
9465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_URL;
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *len = 0;
9495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string result;
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (variable) {
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNURLVProxy: {
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result = "DIRECT";
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin(FindInstance(id));
955868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!plugin.get())
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_GENERIC_ERROR;
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      WebPlugin* webplugin = plugin->webplugin();
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!webplugin)
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_GENERIC_ERROR;
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!webplugin->FindProxyForUrl(GURL(std::string(url)), &result))
9635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_GENERIC_ERROR;
9645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNURLVCookie: {
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin(FindInstance(id));
968868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!plugin.get())
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_GENERIC_ERROR;
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      WebPlugin* webplugin = plugin->webplugin();
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!webplugin)
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_GENERIC_ERROR;
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Bypass third-party cookie blocking by using the url as the
9765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // first_party_for_cookies.
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GURL cookies_url((std::string(url)));
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result = webplugin->GetCookies(cookies_url, cookies_url);
9795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
9815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
9825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_GENERIC_ERROR;
9835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allocate this using the NPAPI allocator. The plugin will call
9865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NPN_Free to free this.
9875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *value = static_cast<char*>(NPN_MemAlloc(result.length() + 1));
9885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::strlcpy(*value, result.c_str(), result.length() + 1);
9895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *len = result.length();
9905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_NO_ERROR;
9925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_SetValueForURL(NPP id,
9955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           NPNURLVariable variable,
9965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           const char* url,
9975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           const char* value,
9985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           uint32_t len) {
9995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!id)
10005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_PARAM;
10015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url || !*url)
10035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_URL;
10045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (variable) {
10065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNURLVCookie: {
10075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<PluginInstance> plugin(FindInstance(id));
1008868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!plugin.get())
10095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_GENERIC_ERROR;
10105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      WebPlugin* webplugin = plugin->webplugin();
10125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!webplugin)
10135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return NPERR_GENERIC_ERROR;
10145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string cookie(value, len);
10165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GURL cookies_url((std::string(url)));
10175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      webplugin->SetCookie(cookies_url, cookies_url, cookie);
10185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NPERR_NO_ERROR;
10195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
10205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case NPNURLVProxy:
10215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We don't support setting proxy values, fall through...
10225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
10235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
10245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Fall through and return an error...
10255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
10265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_GENERIC_ERROR;
10295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_GetAuthenticationInfo(NPP id,
10325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const char* protocol,
10335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const char* host,
10345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  int32_t port,
10355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const char* scheme,
10365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const char* realm,
10375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  char** username,
10385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  uint32_t* ulen,
10395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  char** password,
10405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  uint32_t* plen) {
10415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!id || !protocol || !host || !scheme || !realm || !username ||
10425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      !ulen || !password || !plen)
10435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_PARAM;
10445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: implement me (bug 23928)
10465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_GENERIC_ERROR;
10475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint32_t NPN_ScheduleTimer(NPP id,
10505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           uint32_t interval,
10515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           NPBool repeat,
10525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           void (*func)(NPP id, uint32_t timer_id)) {
10535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
1054868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!plugin.get())
10555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return 0;
10565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return plugin->ScheduleTimer(interval, repeat, func);
10585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_UnscheduleTimer(NPP id, uint32_t timer_id) {
10615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
1062868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (plugin.get())
10635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    plugin->UnscheduleTimer(timer_id);
10645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPError NPN_PopUpContextMenu(NPP id, NPMenu* menu) {
10675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!menu)
10685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NPERR_INVALID_PARAM;
10695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
1071868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (plugin.get()) {
10725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return plugin->PopUpContextMenu(menu);
10735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTREACHED();
10755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NPERR_GENERIC_ERROR;
10765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPBool NPN_ConvertPoint(NPP id, double sourceX, double sourceY,
10795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        NPCoordinateSpace sourceSpace,
10805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        double *destX, double *destY,
10815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        NPCoordinateSpace destSpace) {
10825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(id));
1083868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (plugin.get()) {
1084868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return plugin->ConvertPoint(
1085868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        sourceX, sourceY, sourceSpace, destX, destY, destSpace);
10865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
10875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTREACHED();
10885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
10895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPBool NPN_HandleEvent(NPP id, void *event, NPBool handled) {
10925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: Implement advanced key handling: http://crbug.com/46578
10935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTIMPLEMENTED();
10945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
10955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
10965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
10975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NPBool NPN_UnfocusInstance(NPP id, NPFocusDirection direction) {
10985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO: Implement advanced key handling: http://crbug.com/46578
10995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTIMPLEMENTED();
11005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
11015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NPN_URLRedirectResponse(NPP instance, void* notify_data, NPBool allow) {
11045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<PluginInstance> plugin(FindInstance(instance));
1105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (plugin.get()) {
11065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    plugin->URLRedirectResponse(!!allow, notify_data);
11075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
11085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
11095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // extern "C"
1111