<commit_before>// Copyright 2015 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.

#import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"

#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <WebKit/WebKit.h>

#include "base/ios/ios_util.h"
#import "base/ios/weak_nsobject.h"
#import "base/logging.h"
#import "ios/web/alloc_with_zone_interceptor.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/web_state/js/page_script_util.h"
#import "ios/web/web_state/web_view_internal_creation_util.h"

#if !defined(NDEBUG)

namespace {
BOOL gAllowWKProcessPoolCreation = NO;

// TODO(eugenebut): Cleanup this macro, once all bots switched to iOS9 SDK
// (crbug.com/523365).
#if defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
// By default WKProcessPool creation is not allowed by embedder to prevent
// issues with browsing data clearing. However some iOS system methods do create
// WKProcessPool inside, which is perfectly fine and should be allowed. This
// method whitelists given |klass| with given |selector|, so creation of
// WKProcessPool is allowed inside that selector call. This function currently
// supports Objective-C methods with up to 4 arguments and needs to be updated
// if support for more arguments is required.
void AllowWKProcessPoolCreation(Class klass, SEL selector) {
  Method method = class_getInstanceMethod(klass, selector);
  IMP originalImp = method_getImplementation(method);
  IMP safeImp = imp_implementationWithBlock(
      ^(id self, id arg1, id arg2, id arg3, id arg4) {
        BOOL oldAllowWKProcessPoolCreation = gAllowWKProcessPoolCreation;
        gAllowWKProcessPoolCreation = YES;
        id result = originalImp(self, selector, arg1, arg2, arg3, arg4);
        gAllowWKProcessPoolCreation = oldAllowWKProcessPoolCreation;
        return result;
      });

  method_setImplementation(method, safeImp);
}
#endif  // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
}

@interface WKProcessPool (CRWAdditions)
@end

@implementation WKProcessPool (CRWAdditions)

+ (void)load {
  id (^allocator)(Class klass, NSZone* zone) = ^id(Class klass, NSZone* zone) {
    if (gAllowWKProcessPoolCreation || web::IsWebViewAllocInitAllowed()) {
      return NSAllocateObject(klass, 0, zone);
    }
    // You have hit this because you are trying to create a WKProcessPool
    // directly or indirectly (f.e. by creating WKWebViewConfiguration
    // manually). Please use GetWebViewConfiguration() to get
    // WKWebViewConfiguration object.
    NOTREACHED();
    return nil;
  };
  web::AddAllocWithZoneMethod([WKProcessPool class], allocator);

// TODO(eugenebut): Cleanup this macro, once all bots switched to iOS9 SDK
// (crbug.com/523365).
#if defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
  if (!base::ios::IsRunningOnIOS9OrLater())
    return;

  // Make sure that WKWebsiteDataStore is allowed to create WKProcessPool for
  // internal implementation purposes.
  AllowWKProcessPoolCreation(
      [WKWebsiteDataStore class],
      @selector(fetchDataRecordsOfTypes:completionHandler:completionHandler));
  AllowWKProcessPoolCreation(
      [WKWebsiteDataStore class],
      @selector(removeDataOfTypes:forDataRecords:completionHandler:));
  AllowWKProcessPoolCreation(<commit_msg>WKWebView: Corrected WKWebsiteDataStore selector usage.<commit_after>// Copyright 2015 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.

#import "ios/web/web_state/ui/wk_web_view_configuration_provider.h"

#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <WebKit/WebKit.h>

#include "base/ios/ios_util.h"
#import "base/ios/weak_nsobject.h"
#import "base/logging.h"
#import "ios/web/alloc_with_zone_interceptor.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/web_state/js/page_script_util.h"
#import "ios/web/web_state/web_view_internal_creation_util.h"

#if !defined(NDEBUG)

namespace {
BOOL gAllowWKProcessPoolCreation = NO;

// TODO(eugenebut): Cleanup this macro, once all bots switched to iOS9 SDK
// (crbug.com/523365).
#if defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
// By default WKProcessPool creation is not allowed by embedder to prevent
// issues with browsing data clearing. However some iOS system methods do create
// WKProcessPool inside, which is perfectly fine and should be allowed. This
// method whitelists given |klass| with given |selector|, so creation of
// WKProcessPool is allowed inside that selector call. This function currently
// supports Objective-C methods with up to 4 arguments and needs to be updated
// if support for more arguments is required.
void AllowWKProcessPoolCreation(Class klass, SEL selector) {
  Method method = class_getInstanceMethod(klass, selector);
  IMP originalImp = method_getImplementation(method);
  IMP safeImp = imp_implementationWithBlock(
      ^(id self, id arg1, id arg2, id arg3, id arg4) {
        BOOL oldAllowWKProcessPoolCreation = gAllowWKProcessPoolCreation;
        gAllowWKProcessPoolCreation = YES;
        id result = originalImp(self, selector, arg1, arg2, arg3, arg4);
        gAllowWKProcessPoolCreation = oldAllowWKProcessPoolCreation;
        return result;
      });

  method_setImplementation(method, safeImp);
}
#endif  // __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
}

@interface WKProcessPool (CRWAdditions)
@end

@implementation WKProcessPool (CRWAdditions)

+ (void)load {
  id (^allocator)(Class klass, NSZone* zone) = ^id(Class klass, NSZone* zone) {
    if (gAllowWKProcessPoolCreation || web::IsWebViewAllocInitAllowed()) {
      return NSAllocateObject(klass, 0, zone);
    }
    // You have hit this because you are trying to create a WKProcessPool
    // directly or indirectly (f.e. by creating WKWebViewConfiguration
    // manually). Please use GetWebViewConfiguration() to get
    // WKWebViewConfiguration object.
    NOTREACHED();
    return nil;
  };
  web::AddAllocWithZoneMethod([WKProcessPool class], allocator);

// TODO(eugenebut): Cleanup this macro, once all bots switched to iOS9 SDK
// (crbug.com/523365).
#if defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
  if (!base::ios::IsRunningOnIOS9OrLater())
    return;

  // Make sure that WKWebsiteDataStore is allowed to create WKProcessPool for
  // internal implementation purposes.
  AllowWKProcessPoolCreation(
      [WKWebsiteDataStore class],
      @selector(fetchDataRecordsOfTypes:completionHandler:completionHandler:));
  AllowWKProcessPoolCreation(
      [WKWebsiteDataStore class],
      @selector(removeDataOfTypes:forDataRecords:completionHandler:));
  AllowWKProcessPoolCreation(
{'fraction_non_alphanumeric': 0.09253033182283292, 'fraction_numerical': 0.01140184183598889, 'mean_word_length': 4.7592592592592595, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '5214716', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2094, 'n_words': 585}