<commit_before>/*
 * Copyright 2010 The Native Client 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 "native_client/src/trusted/plugin/ppapi/browser_interface_ppapi.h"

#include "native_client/src/include/checked_cast.h"
#include "native_client/src/include/nacl_elf.h"
#include "native_client/src/include/nacl_macros.h"
#include "native_client/src/include/portability.h"
#include "native_client/src/trusted/plugin/api_defines.h"
#include "native_client/src/trusted/plugin/ppapi/scriptable_handle_ppapi.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/var.h"

using nacl::assert_cast;

namespace {

bool GetWindow(plugin::InstanceIdentifier instance_id, pp::Var* window) {
  pp::Instance* instance = plugin::InstanceIdentifierToPPInstance(instance_id);
  *window = instance->GetWindowObject();
  return window->is_void();
}

}  // namespace

namespace plugin {

uintptr_t BrowserInterfacePpapi::StringToIdentifier(const nacl::string& str) {
  return reinterpret_cast<uintptr_t>(new pp::Var(str));
}


nacl::string BrowserInterfacePpapi::IdentifierToString(uintptr_t ident) {
  pp::Var* var = reinterpret_cast<pp::Var*>(ident);
  return var->AsString();
}


bool BrowserInterfacePpapi::Alert(InstanceIdentifier instance_id,
                                  const nacl::string& text) {
  pp::Var window;
  if (!GetWindow(instance_id, &window)) {
    return false;
  }
  pp::Var exception;
  window.Call("alert", text, &exception);
  return exception.is_void();
}


bool BrowserInterfacePpapi::EvalString(InstanceIdentifier instance_id,
                                       const nacl::string& expression) {
  pp::Var window;
  if (!GetWindow(instance_id, &window)) {
    return false;
  }
  pp::Var exception;
  window.Call("eval", expression, &exception);
  return exception.is_void();
}


bool BrowserInterfacePpapi::GetOrigin(InstanceIdentifier instance_id,
                                      nacl::string* origin) {
  const nacl::string kUnknownOrigin = "";
  *origin = kUnknownOrigin;
  pp::Var window;
  if (!GetWindow(instance_id, &window)) {
    return false;
  }
  pp::Var location = window.GetProperty("location");
  if (location.is_string()) {
    *origin = location.AsString();
  } else {
    pp::Var href = window.GetProperty("href");
    if (!href.is_string()) {
      *origin = href.AsString();
    }
  }
  return (kUnknownOrigin != *origin);
}


ScriptableHandle* BrowserInterfacePpapi::NewScriptableHandle(
    PortableHandle* handle) {
  return ScriptableHandlePpapi::New(handle);
}


pp::Instance* InstanceIdentifierToPPInstance(InstanceIdentifier instance_id) {
  return reinterpret_cast<pp::Instance*>(assert_cast<intptr_t>(instance_id));
}


InstanceIdentifier PPInstanceToInstanceIdentifier(pp::Instance* instance) {
  return assert_cast<InstanceIdentifier>(reinterpret_cast<intptr_t>(instance));
}

}  // namespace plugin
<commit_msg>Fix two bugs with the PPAPI browser interface discovered during ppapi host testing development.<commit_after>/*
 * Copyright 2010 The Native Client 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 "native_client/src/trusted/plugin/ppapi/browser_interface_ppapi.h"

#include "native_client/src/include/checked_cast.h"
#include "native_client/src/include/nacl_elf.h"
#include "native_client/src/include/nacl_macros.h"
#include "native_client/src/include/portability.h"
#include "native_client/src/trusted/plugin/api_defines.h"
#include "native_client/src/trusted/plugin/ppapi/scriptable_handle_ppapi.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/var.h"

using nacl::assert_cast;

namespace {

bool GetWindow(plugin::InstanceIdentifier instance_id, pp::Var* window) {
  pp::Instance* instance = plugin::InstanceIdentifierToPPInstance(instance_id);
  *window = instance->GetWindowObject();
  return !window->is_void();
}

}  // namespace

namespace plugin {

uintptr_t BrowserInterfacePpapi::StringToIdentifier(const nacl::string& str) {
  return reinterpret_cast<uintptr_t>(new pp::Var(str));
}


nacl::string BrowserInterfacePpapi::IdentifierToString(uintptr_t ident) {
  pp::Var* var = reinterpret_cast<pp::Var*>(ident);
  return var->AsString();
}


bool BrowserInterfacePpapi::Alert(InstanceIdentifier instance_id,
                                  const nacl::string& text) {
  pp::Var window;
  if (!GetWindow(instance_id, &window)) {
    return false;
  }
  pp::Var exception;
  window.Call("alert", text, &exception);
  return exception.is_void();
}


bool BrowserInterfacePpapi::EvalString(InstanceIdentifier instance_id,
                                       const nacl::string& expression) {
  pp::Var window;
  if (!GetWindow(instance_id, &window)) {
    return false;
  }
  pp::Var exception;
  window.Call("eval", expression, &exception);
  return exception.is_void();
}


bool BrowserInterfacePpapi::GetOrigin(InstanceIdentifier instance_id,
                                      nacl::string* origin) {
  const nacl::string kUnknownOrigin = "";
  *origin = kUnknownOrigin;
  pp::Var window;
  if (!GetWindow(instance_id, &window)) {
    return false;
  }
  pp::Var location = window.GetProperty("location");
  if (location.is_object()) {
    pp::Var href = location.GetProperty("href");
    if (!href.is_string()) {
      *origin = href.AsString();
    }
  }
  return (kUnknownOrigin != *origin);
}


ScriptableHandle* BrowserInterfacePpapi::NewScriptableHandle(
    PortableHandle* handle) {
  return ScriptableHandlePpapi::New(handle);
}


pp::Instance* InstanceIdentifierToPPInstance(InstanceIdentifier instance_id) {
  return reinterpret_cast<pp::Instance*>(assert_cast<intptr_t>(instance_id));
}


InstanceIdentifier PPInstanceToInstanceIdentifier(pp::Instance* instance) {
  return assert_cast<InstanceIdentifier>(reinterpret_cast<intptr_t>(instance));
}

}  // namespace plugin

{'fraction_non_alphanumeric': 0.12485355648535565, 'fraction_numerical': 0.0013389121338912135, 'mean_word_length': 4.669829222011385, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7145126', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1817, 'n_words': 356}