<commit_before>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/plugin/npobject_stub.h"

#include "chrome/common/child_process_logging.h"
#include "chrome/common/plugin_messages.h"
#include "chrome/plugin/npobject_util.h"
#include "chrome/plugin/plugin_channel_base.h"
#include "chrome/plugin/plugin_thread.h"
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npruntime.h"
#include "webkit/api/public/WebBindings.h"
#include "webkit/glue/plugins/plugin_constants_win.h"

using WebKit::WebBindings;

NPObjectStub::NPObjectStub(
    NPObject* npobject,
    PluginChannelBase* channel,
    int route_id,
    gfx::NativeViewId containing_window,
    const GURL& page_url)
    : npobject_(npobject),
      channel_(channel),
      route_id_(route_id),
      containing_window_(containing_window),
      page_url_(page_url) {
  channel_->AddRoute(route_id, this, true);

  // We retain the object just as PluginHost does if everything was in-process.
  WebBindings::retainObject(npobject_);
}

NPObjectStub::~NPObjectStub() {
  channel_->RemoveRoute(route_id_);
  if (npobject_)
    WebBindings::releaseObject(npobject_);
}

bool NPObjectStub::Send(IPC::Message* msg) {
  return channel_->Send(msg);
}

void NPObjectStub::OnPluginDestroyed() {
  // We null out the underlying NPObject pointer since it's not valid anymore (
  // ScriptController manually deleted the object).  As a result,
  // OnMessageReceived won't dispatch any more messages.  Since this includes
  // OnRelease, this object won't get deleted until OnChannelError which might
  // not happen for a long time if this renderer process has a long lived
  // plugin instance to the same process.  So we delete this object manually.
  npobject_ = NULL;
  MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

void NPObjectStub::OnMessageReceived(const IPC::Message& msg) {
  child_process_logging::ScopedActiveURLSetter url_setter(page_url_);

  if (!npobject_) {
    if (msg.is_sync()) {
      // The object could be garbage because the frame has gone away, so
      // just send an error reply to the caller.
      IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg);
      reply->set_reply_error();
      Send(reply);
    }

    return;
  }

  IPC_BEGIN_MESSAGE_MAP(NPObjectStub, msg)
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Release, OnRelease);
    IPC_MESSAGE_HANDLER(NPObjectMsg_HasMethod, OnHasMethod);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Invoke, OnInvoke);
    IPC_MESSAGE_HANDLER(NPObjectMsg_HasProperty, OnHasProperty);
    IPC_MESSAGE_HANDLER(NPObjectMsg_GetProperty, OnGetProperty);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_SetProperty, OnSetProperty);
    IPC_MESSAGE_HANDLER(NPObjectMsg_RemoveProperty, OnRemoveProperty);
    IPC_MESSAGE_HANDLER(NPObjectMsg_Invalidate, OnInvalidate);
    IPC_MESSAGE_HANDLER(NPObjectMsg_Enumeration, OnEnumeration);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Construct, OnConstruct);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Evaluate, OnEvaluate);
    IPC_MESSAGE_HANDLER(NPObjectMsg_SetException, OnSetException);
    IPC_MESSAGE_UNHANDLED_ERROR()
  IPC_END_MESSAGE_MAP()
}

void NPObjectStub::OnChannelError() {
  delete this;<commit_msg>Fix double delete in NPObjectStub that I introduced.<commit_after>// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/plugin/npobject_stub.h"

#include "chrome/common/child_process_logging.h"
#include "chrome/common/plugin_messages.h"
#include "chrome/plugin/npobject_util.h"
#include "chrome/plugin/plugin_channel_base.h"
#include "chrome/plugin/plugin_thread.h"
#include "third_party/npapi/bindings/npapi.h"
#include "third_party/npapi/bindings/npruntime.h"
#include "webkit/api/public/WebBindings.h"
#include "webkit/glue/plugins/plugin_constants_win.h"

using WebKit::WebBindings;

NPObjectStub::NPObjectStub(
    NPObject* npobject,
    PluginChannelBase* channel,
    int route_id,
    gfx::NativeViewId containing_window,
    const GURL& page_url)
    : npobject_(npobject),
      channel_(channel),
      route_id_(route_id),
      containing_window_(containing_window),
      page_url_(page_url) {
  channel_->AddRoute(route_id, this, true);

  // We retain the object just as PluginHost does if everything was in-process.
  WebBindings::retainObject(npobject_);
}

NPObjectStub::~NPObjectStub() {
  channel_->RemoveRoute(route_id_);
  if (npobject_)
    WebBindings::releaseObject(npobject_);
}

bool NPObjectStub::Send(IPC::Message* msg) {
  return channel_->Send(msg);
}

void NPObjectStub::OnPluginDestroyed() {
  // We null out the underlying NPObject pointer since it's not valid anymore (
  // ScriptController manually deleted the object).  As a result,
  // OnMessageReceived won't dispatch any more messages.  Since this includes
  // OnRelease, this object won't get deleted until OnChannelError which might
  // not happen for a long time if this renderer process has a long lived
  // plugin instance to the same process.  So we delete this object manually.
  npobject_ = NULL;
  MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

void NPObjectStub::OnMessageReceived(const IPC::Message& msg) {
  child_process_logging::ScopedActiveURLSetter url_setter(page_url_);

  if (!npobject_) {
    if (msg.is_sync()) {
      // The object could be garbage because the frame has gone away, so
      // just send an error reply to the caller.
      IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg);
      reply->set_reply_error();
      Send(reply);
    }

    return;
  }

  IPC_BEGIN_MESSAGE_MAP(NPObjectStub, msg)
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Release, OnRelease);
    IPC_MESSAGE_HANDLER(NPObjectMsg_HasMethod, OnHasMethod);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Invoke, OnInvoke);
    IPC_MESSAGE_HANDLER(NPObjectMsg_HasProperty, OnHasProperty);
    IPC_MESSAGE_HANDLER(NPObjectMsg_GetProperty, OnGetProperty);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_SetProperty, OnSetProperty);
    IPC_MESSAGE_HANDLER(NPObjectMsg_RemoveProperty, OnRemoveProperty);
    IPC_MESSAGE_HANDLER(NPObjectMsg_Invalidate, OnInvalidate);
    IPC_MESSAGE_HANDLER(NPObjectMsg_Enumeration, OnEnumeration);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Construct, OnConstruct);
    IPC_MESSAGE_HANDLER_DELAY_REPLY(NPObjectMsg_Evaluate, OnEvaluate);
    IPC_MESSAGE_HANDLER(NPObjectMsg_SetException, OnSetException);
    IPC_MESSAGE_UNHANDLED_ERROR()
  IPC_END_MESSAGE_MAP()
}

void NPObjectStub::OnChannelError() {
  // If npobject_ is NULLed out, that means a DeleteSoon is happening.
  if (npobject_)
    delete this;
{'fraction_non_alphanumeric': 0.09323636363636363, 'fraction_numerical': 0.0023272727272727273, 'mean_word_length': 5.183453237410072, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6319493', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2216, 'n_words': 485}