<commit_before>// Copyright (c) 2009 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/renderer_main_platform_delegate.h"

#include "base/debug_util.h"
#include "base/message_loop.h"

#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
#import <Cocoa/Cocoa.h>
extern "C" {
#include <sandbox.h>
}

#include "base/mac_util.h"
#include "base/scoped_cftyperef.h"
#include "base/scoped_nsautorelease_pool.h"
#include "base/sys_info.h"
#include "chrome/common/chrome_switches.h"
#include "third_party/WebKit/WebKit/mac/WebCoreSupport/WebSystemInterface.h"

RendererMainPlatformDelegate::RendererMainPlatformDelegate(
    const MainFunctionParams& parameters)
        : parameters_(parameters) {
}

RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
}

// Warmup System APIs that empirically need to be accessed before the Sandbox
// is turned on.
// This method is layed out in blocks, each one containing a separate function
// that needs to be warmed up. The OS version on which we found the need to
// enable the function is also noted.
// This function is tested on the following OS versions:
//     10.5.6, 10.6 seed release
void SandboxWarmup() {
  base::ScopedNSAutoreleasePool scoped_pool;

  { // CGColorSpaceCreateWithName(), CGBitmapContextCreate() - 10.5.6
    CGColorSpaceRef rgb_colorspace =
        CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

    // Allocate a 1 byte image.
    char data[8];
    CGContextRef tmp = CGBitmapContextCreate(data, 1, 1, 8, 1*8,
                                             rgb_colorspace,
                                             kCGImageAlphaPremultipliedFirst |
                                             kCGBitmapByteOrder32Host);

    CGColorSpaceRelease(rgb_colorspace);
    CGContextRelease(tmp);
  }

  {  // [-NSColor colorUsingColorSpaceName] - 10.5.6
    NSColor *color = [NSColor controlTextColor];
    [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
  }

  {  // localtime() - 10.5.6
    time_t tv = {0};
    localtime(&tv);
  }

  {  // CGImageSourceGetStatus() - 10.6 seed release.
    // Create a png with just enough data to get everything warmed up...
    char png_header[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
    NSData *data = [NSData dataWithBytes:png_header
                                  length:arraysize(png_header)];
    scoped_cftyperef<CGImageSourceRef> img(
        CGImageSourceCreateWithData((CFDataRef)data,
        NULL));
    CGImageSourceGetStatus(img);
  }
}

namespace {

// Since we use Cocoa in the renderer process <http://crbug.com/13890>, the
// windowserver believes we are a UI process and sends events to us. If we do
// not process them (or at least remove them from the queue), the windowserver
// will mark us as "not responding" and will start doing bad things like run
// spindump on us (see <http://crbug.com/11319>). This function just keeps the
// event queue empty. TODO(avi):Once Cocoa is gone from the renderer, remove
// this code <http://crbug.com/13893>.
void PullAccumulatedWindowserverEvents() {
  base::ScopedNSAutoreleasePool scoped_pool;

  while ([[NSApplication sharedApplication]
      nextEventMatchingMask:NSAnyEventMask
                  untilDate:nil
                     inMode:NSDefaultRunLoopMode
                    dequeue:YES]) {
    // just drop all pending events on the floor
  }

  MessageLoop::current()->PostDelayedTask(FROM_HERE,
      NewRunnableFunction(PullAccumulatedWindowserverEvents), 1000);
}

}  // namepsace

// TODO(mac-port): Any code needed to initialize a process for
// purposes of running a renderer needs to also be reflected in
// chrome_dll_main.cc for --single-process support.
void RendererMainPlatformDelegate::PlatformInitialize() {
  // Load WebKit system interfaces.
  InitWebCoreSystemInterface();
<commit_msg>Changing the mode we pull events in, so we don't fire off timers and notifications.<commit_after>// Copyright (c) 2009 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/renderer_main_platform_delegate.h"

#include "base/debug_util.h"
#include "base/message_loop.h"

#import <Foundation/Foundation.h>
#import <ApplicationServices/ApplicationServices.h>
#import <Cocoa/Cocoa.h>
extern "C" {
#include <sandbox.h>
}

#include "base/mac_util.h"
#include "base/scoped_cftyperef.h"
#include "base/scoped_nsautorelease_pool.h"
#include "base/sys_info.h"
#include "chrome/common/chrome_switches.h"
#include "third_party/WebKit/WebKit/mac/WebCoreSupport/WebSystemInterface.h"

RendererMainPlatformDelegate::RendererMainPlatformDelegate(
    const MainFunctionParams& parameters)
        : parameters_(parameters) {
}

RendererMainPlatformDelegate::~RendererMainPlatformDelegate() {
}

// Warmup System APIs that empirically need to be accessed before the Sandbox
// is turned on.
// This method is layed out in blocks, each one containing a separate function
// that needs to be warmed up. The OS version on which we found the need to
// enable the function is also noted.
// This function is tested on the following OS versions:
//     10.5.6, 10.6 seed release
void SandboxWarmup() {
  base::ScopedNSAutoreleasePool scoped_pool;

  { // CGColorSpaceCreateWithName(), CGBitmapContextCreate() - 10.5.6
    CGColorSpaceRef rgb_colorspace =
        CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

    // Allocate a 1 byte image.
    char data[8];
    CGContextRef tmp = CGBitmapContextCreate(data, 1, 1, 8, 1*8,
                                             rgb_colorspace,
                                             kCGImageAlphaPremultipliedFirst |
                                             kCGBitmapByteOrder32Host);

    CGColorSpaceRelease(rgb_colorspace);
    CGContextRelease(tmp);
  }

  {  // [-NSColor colorUsingColorSpaceName] - 10.5.6
    NSColor *color = [NSColor controlTextColor];
    [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
  }

  {  // localtime() - 10.5.6
    time_t tv = {0};
    localtime(&tv);
  }

  {  // CGImageSourceGetStatus() - 10.6 seed release.
    // Create a png with just enough data to get everything warmed up...
    char png_header[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
    NSData *data = [NSData dataWithBytes:png_header
                                  length:arraysize(png_header)];
    scoped_cftyperef<CGImageSourceRef> img(
        CGImageSourceCreateWithData((CFDataRef)data,
        NULL));
    CGImageSourceGetStatus(img);
  }
}

namespace {

// Since we use Cocoa in the renderer process <http://crbug.com/13890>, the
// windowserver believes we are a UI process and sends events to us. If we do
// not process them (or at least remove them from the queue), the windowserver
// will mark us as "not responding" and will start doing bad things like run
// spindump on us (see <http://crbug.com/11319>). This function just keeps the
// event queue empty. It uses a custom run loop mode so that no timers or
// notifications fire and surprise Cocoa that's running in another thread.
// TODO(avi):Once Cocoa is gone from the renderer, remove this code
// <http://crbug.com/13893>.
void PullAccumulatedWindowserverEvents() {
  base::ScopedNSAutoreleasePool scoped_pool;

  while ([[NSApplication sharedApplication]
      nextEventMatchingMask:NSAnyEventMask
                  untilDate:nil
                     inMode:@"org.chromium.CustomRunLoopModeSoThatNothingFires"
                    dequeue:YES]) {
    // just drop all pending events on the floor
  }

  MessageLoop::current()->PostDelayedTask(FROM_HERE,
      NewRunnableFunction(PullAccumulatedWindowserverEvents), 1000);
}

}  // namepsace

// TODO(mac-port): Any code needed to initialize a process for
// purposes of running a renderer needs to also be reflected in
// chrome_dll_main.cc for --single-process support.
void RendererMainPlatformDelegate::PlatformInitialize() {
  // Load WebKit system interfaces.
  InitWebCoreSystemInterface();

{'fraction_non_alphanumeric': 0.0936315853807603, 'fraction_numerical': 0.018090697958684756, 'mean_word_length': 3.2592399791775115, 'pattern_counts': {'":': 0, '<': 19, '<?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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4437036', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2386, 'n_words': 710}