<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/renderer/render_thread.h"

#include <algorithm>
#include <vector>

#include "base/command_line.h"
#include "base/shared_memory.h"
#include "base/stats_table.h"
#include "chrome/common/app_cache/app_cache_context_impl.h"
#include "chrome/common/app_cache/app_cache_dispatcher.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/url_constants.h"
#include "chrome/plugin/npobject_util.h"
// TODO(port)
#if defined(OS_WIN)
#include "chrome/plugin/plugin_channel.h"
#else
#include "base/scoped_handle.h"
#include "chrome/plugin/plugin_channel_base.h"
#include "webkit/glue/weburlrequest.h"
#endif
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/extension_process_bindings.h"
#include "chrome/renderer/extensions/renderer_extension_bindings.h"
#include "chrome/renderer/js_only_v8_extensions.h"
#include "chrome/renderer/loadtimes_extension_bindings.h"
#include "chrome/renderer/net/render_dns_master.h"
#include "chrome/renderer/render_process.h"
#include "chrome/renderer/render_view.h"
#include "chrome/renderer/renderer_webkitclient_impl.h"
#include "chrome/renderer/user_script_slave.h"
#include "chrome/renderer/visitedlink_slave.h"
#include "third_party/WebKit/WebKit/chromium/public/WebCache.h"
#include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
#include "webkit/extensions/v8/gears_extension.h"
#include "webkit/extensions/v8/interval_extension.h"
#include "webkit/extensions/v8/playback_extension.h"

#if defined(OS_WIN)
#include <windows.h>
#include <objbase.h>
#endif

using WebKit::WebCache;
using WebKit::WebString;

static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;

//-----------------------------------------------------------------------------
// Methods below are only called on the owner's thread:

// When we run plugins in process, we actually run them on the render thread,
// which means that we need to make the render thread pump UI events.
RenderThread::RenderThread()
    : ChildThread(
          base::Thread::Options(RenderProcess::InProcessPlugins() ?
              MessageLoop::TYPE_UI : MessageLoop::TYPE_DEFAULT, kV8StackSize)) {
}

RenderThread::RenderThread(const std::wstring& channel_name)
    : ChildThread(
          base::Thread::Options(RenderProcess::InProcessPlugins() ?
              MessageLoop::TYPE_UI : MessageLoop::TYPE_DEFAULT, kV8StackSize)) {
  SetChannelName(channel_name);
}

RenderThread::~RenderThread() {
}

RenderThread* RenderThread::current() {
  DCHECK(!IsPluginProcess());
  return static_cast<RenderThread*>(ChildThread::current());
}

void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
  channel()->AddFilter(filter);
}

void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
  channel()->RemoveFilter(filter);
}

void RenderThread::Resolve(const char* name, size_t length) {
  return dns_master_->Resolve(name, length);
}

void RenderThread::SendHistograms() {
  return histogram_snapshots_->SendHistograms();
}

static WebAppCacheContext* CreateAppCacheContextForRenderer() {
  return new AppCacheContextImpl(RenderThread::current());
}

void RenderThread::Init() {
  // TODO(darin): Why do we need COM here?  This is probably bogus. Perhaps
  // this is for InProcessPlugin support?
#if defined(OS_WIN)
  // The renderer thread should wind-up COM.
  CoInitialize(0);
#endif

  ChildThread::Init();
  notification_service_.reset(new NotificationService);
  cache_stats_factory_.reset(
      new ScopedRunnableMethodFactory<RenderThread>(this));

  visited_link_slave_.reset(new VisitedLinkSlave());
  user_script_slave_.reset(new UserScriptSlave());
  dns_master_.reset(new RenderDnsMaster());
  histogram_snapshots_.reset(new RendererHistogramSnapshots());
  app_cache_dispatcher_.reset(new AppCacheDispatcher());
  WebAppCacheContext::SetFactory(CreateAppCacheContextForRenderer);
}

void RenderThread::CleanUp() {
  // Shutdown in reverse of the initialization order.

  WebAppCacheContext::SetFactory(NULL);
  app_cache_dispatcher_.reset();
  histogram_snapshots_.reset();
  dns_master_.reset();
  user_script_slave_.reset();
  visited_link_slave_.reset();

  if (webkit_client_.get()) {
    WebKit::shutdown();
    webkit_client_.reset();
  }

  notification_service_.reset();

  ChildThread::CleanUp();

  // TODO(port)
#if defined(OS_WIN)
  // Clean up plugin channels before this thread goes away.
  PluginChannelBase::CleanupChannels();
#endif

#if defined(OS_WIN)
  CoUninitialize();
#endif
}

void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
  DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
  visited_link_slave_->Init(table);
}

void RenderThread::OnUpdateUserScripts(
    base::SharedMemoryHandle scripts) {
  DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
  user_script_slave_->UpdateScripts(scripts);
}
<commit_msg>Don't call COM stuff in the renderer - Remove calls to CoInitialize / CoUninitialize - CoUninitialize sometimes causes the OS to try to load oleauth.dll - An exception for in-process-plugins.<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/renderer/render_thread.h"

#include <algorithm>
#include <vector>

#include "base/command_line.h"
#include "base/shared_memory.h"
#include "base/stats_table.h"
#include "chrome/common/app_cache/app_cache_context_impl.h"
#include "chrome/common/app_cache/app_cache_dispatcher.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/url_constants.h"
#include "chrome/plugin/npobject_util.h"
// TODO(port)
#if defined(OS_WIN)
#include "chrome/plugin/plugin_channel.h"
#else
#include "base/scoped_handle.h"
#include "chrome/plugin/plugin_channel_base.h"
#include "webkit/glue/weburlrequest.h"
#endif
#include "chrome/renderer/extensions/event_bindings.h"
#include "chrome/renderer/extensions/extension_process_bindings.h"
#include "chrome/renderer/extensions/renderer_extension_bindings.h"
#include "chrome/renderer/js_only_v8_extensions.h"
#include "chrome/renderer/loadtimes_extension_bindings.h"
#include "chrome/renderer/net/render_dns_master.h"
#include "chrome/renderer/render_process.h"
#include "chrome/renderer/render_view.h"
#include "chrome/renderer/renderer_webkitclient_impl.h"
#include "chrome/renderer/user_script_slave.h"
#include "chrome/renderer/visitedlink_slave.h"
#include "third_party/WebKit/WebKit/chromium/public/WebCache.h"
#include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
#include "webkit/extensions/v8/gears_extension.h"
#include "webkit/extensions/v8/interval_extension.h"
#include "webkit/extensions/v8/playback_extension.h"

#if defined(OS_WIN)
#include <windows.h>
#include <objbase.h>
#endif

using WebKit::WebCache;
using WebKit::WebString;

static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;

//-----------------------------------------------------------------------------
// Methods below are only called on the owner's thread:

// When we run plugins in process, we actually run them on the render thread,
// which means that we need to make the render thread pump UI events.
RenderThread::RenderThread()
    : ChildThread(
          base::Thread::Options(RenderProcess::InProcessPlugins() ?
              MessageLoop::TYPE_UI : MessageLoop::TYPE_DEFAULT, kV8StackSize)) {
}

RenderThread::RenderThread(const std::wstring& channel_name)
    : ChildThread(
          base::Thread::Options(RenderProcess::InProcessPlugins() ?
              MessageLoop::TYPE_UI : MessageLoop::TYPE_DEFAULT, kV8StackSize)) {
  SetChannelName(channel_name);
}

RenderThread::~RenderThread() {
}

RenderThread* RenderThread::current() {
  DCHECK(!IsPluginProcess());
  return static_cast<RenderThread*>(ChildThread::current());
}

void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
  channel()->AddFilter(filter);
}

void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
  channel()->RemoveFilter(filter);
}

void RenderThread::Resolve(const char* name, size_t length) {
  return dns_master_->Resolve(name, length);
}

void RenderThread::SendHistograms() {
  return histogram_snapshots_->SendHistograms();
}

static WebAppCacheContext* CreateAppCacheContextForRenderer() {
  return new AppCacheContextImpl(RenderThread::current());
}

void RenderThread::Init() {
#if defined(OS_WIN)
  // If you are running plugins in this thread you need COM active but in
  // the normal case you don't.
  if (RenderProcess::InProcessPlugins())
    CoInitialize(0);
#endif

  ChildThread::Init();
  notification_service_.reset(new NotificationService);
  cache_stats_factory_.reset(
      new ScopedRunnableMethodFactory<RenderThread>(this));

  visited_link_slave_.reset(new VisitedLinkSlave());
  user_script_slave_.reset(new UserScriptSlave());
  dns_master_.reset(new RenderDnsMaster());
  histogram_snapshots_.reset(new RendererHistogramSnapshots());
  app_cache_dispatcher_.reset(new AppCacheDispatcher());
  WebAppCacheContext::SetFactory(CreateAppCacheContextForRenderer);
}

void RenderThread::CleanUp() {
  // Shutdown in reverse of the initialization order.

  WebAppCacheContext::SetFactory(NULL);
  app_cache_dispatcher_.reset();
  histogram_snapshots_.reset();
  dns_master_.reset();
  user_script_slave_.reset();
  visited_link_slave_.reset();

  if (webkit_client_.get()) {
    WebKit::shutdown();
    webkit_client_.reset();
  }

  notification_service_.reset();

  ChildThread::CleanUp();

  // TODO(port)
#if defined(OS_WIN)
  // Clean up plugin channels before this thread goes away.
  PluginChannelBase::CleanupChannels();
  // Don't call COM if the renderer is in the sandbox.
  if (RenderProcess::InProcessPlugins())
    CoUninitialize();
#endif
}

void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
  DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
  visited_link_slave_->Init(table);
}

void RenderThread::OnUpdateUserScripts(
    base::SharedMemoryHandle scripts) {
  DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
  user_script_slave_->UpdateScripts(scripts);
}

{'fraction_non_alphanumeric': 0.1357646622123237, 'fraction_numerical': 0.003526354862657758, 'mean_word_length': 5.59547123623011, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, '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': '2992097', 'n_tokens_mistral': 3341, 'n_tokens_neox': 3328, 'n_words': 644}