<commit_before>/*
 *  Copyright (c) 2018-present, Facebook, Inc.
 *
 *  This source code is licensed under the MIT license found in the LICENSE
 *  file in the root directory of this source tree.
 *
 */
#if FB_SONARKIT_ENABLED

#import "SonarClient.h"
#import "SonarCppWrapperPlugin.h"
#import <Sonar/SonarClient.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/ScopedEventBaseThread.h>
#import <UIKit/UIKit.h>
#include "SKStateUpdateCPPWrapper.h"
#import "FlipperDiagnosticsViewController.h"

#if !TARGET_OS_SIMULATOR
//#import "SKPortForwardingServer.h"
#endif

using WrapperPlugin = facebook::sonar::SonarCppWrapperPlugin;

@implementation SonarClient {
  facebook::sonar::SonarClient *_cppClient;
  folly::ScopedEventBaseThread sonarThread;
  folly::ScopedEventBaseThread connectionThread;
#if !TARGET_OS_SIMULATOR
 // SKPortForwardingServer *_server;
#endif
}

+ (instancetype)sharedClient
{
  static SonarClient *sharedClient = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedClient = [[self alloc] init];
  });
  return sharedClient;
}

- (instancetype)init
{
  if (self = [super init]) {
    UIDevice *device = [UIDevice currentDevice];
    NSString *deviceName = [device name];
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
    NSString *deviceId = [[device identifierForVendor]UUIDString];
    NSString *appId = appName;
    NSString *privateAppDirectory = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0];

    NSFileManager *manager = [NSFileManager defaultManager];

    if ([manager fileExistsAtPath:privateAppDirectory isDirectory:NULL] == NO) {
      //TODO: Handle errors properly
      [manager createDirectoryAtPath:privateAppDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    }

#if TARGET_OS_SIMULATOR
    deviceName = [NSString stringWithFormat:@"%@ %@", [[UIDevice currentDevice] model], @"Simulator"];
#endif

    facebook::sonar::SonarClient::init({
      {
        "localhost",
        "iOS",
        [deviceName UTF8String],
        [deviceId UTF8String],
        [appName UTF8String],
        [appId UTF8String],
        [privateAppDirectory UTF8String],
      },
      sonarThread.getEventBase(),
      connectionThread.getEventBase()
    });
    _cppClient = facebook::sonar::SonarClient::instance();
  }
  return self;
}

- (void)refreshPlugins
{
  _cppClient->refreshPlugins();
}

- (void)addPlugin:(NSObject<SonarPlugin> *)plugin
{
  _cppClient->addPlugin(std::make_shared<WrapperPlugin>(plugin));
}

- (void)removePlugin:(NSObject<SonarPlugin> *)plugin
{
  _cppClient->removePlugin(std::make_shared<WrapperPlugin>(plugin));
}

- (NSObject<SonarPlugin> *)pluginWithIdentifier:(NSString *)identifier<commit_msg>Don't use vendorID as deviceId<commit_after>/*
 *  Copyright (c) 2018-present, Facebook, Inc.
 *
 *  This source code is licensed under the MIT license found in the LICENSE
 *  file in the root directory of this source tree.
 *
 */
#if FB_SONARKIT_ENABLED

#import "SonarClient.h"
#import "SonarCppWrapperPlugin.h"
#import <Sonar/SonarClient.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/ScopedEventBaseThread.h>
#import <UIKit/UIKit.h>
#include "SKStateUpdateCPPWrapper.h"
#import "FlipperDiagnosticsViewController.h"

#if !TARGET_OS_SIMULATOR
//#import "SKPortForwardingServer.h"
#endif

using WrapperPlugin = facebook::sonar::SonarCppWrapperPlugin;

@implementation SonarClient {
  facebook::sonar::SonarClient *_cppClient;
  folly::ScopedEventBaseThread sonarThread;
  folly::ScopedEventBaseThread connectionThread;
#if !TARGET_OS_SIMULATOR
 // SKPortForwardingServer *_server;
#endif
}

+ (instancetype)sharedClient
{
  static SonarClient *sharedClient = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedClient = [[self alloc] init];
  });
  return sharedClient;
}

- (instancetype)init
{
  if (self = [super init]) {
    UIDevice *device = [UIDevice currentDevice];
    NSString *deviceName = [device name];
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
    NSString *appId = appName;
    NSString *privateAppDirectory = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0];

    NSFileManager *manager = [NSFileManager defaultManager];

    if ([manager fileExistsAtPath:privateAppDirectory isDirectory:NULL] == NO) {
      //TODO: Handle errors properly
      [manager createDirectoryAtPath:privateAppDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    }

#if TARGET_OS_SIMULATOR
    deviceName = [NSString stringWithFormat:@"%@ %@", [[UIDevice currentDevice] model], @"Simulator"];
#endif

    facebook::sonar::SonarClient::init({
      {
        "localhost",
        "iOS",
        [deviceName UTF8String],
        "unknown",
        [appName UTF8String],
        [appId UTF8String],
        [privateAppDirectory UTF8String],
      },
      sonarThread.getEventBase(),
      connectionThread.getEventBase()
    });
    _cppClient = facebook::sonar::SonarClient::instance();
  }
  return self;
}

- (void)refreshPlugins
{
  _cppClient->refreshPlugins();
}

- (void)addPlugin:(NSObject<SonarPlugin> *)plugin
{
  _cppClient->addPlugin(std::make_shared<WrapperPlugin>(plugin));
}

- (void)removePlugin:(NSObject<SonarPlugin> *)plugin
{
  _cppClient->removePlugin(std::make_shared<WrapperPlugin>(plugin));
}

- (NSObject<SonarPlugin> *)pluginWithIdentifier:(NSString *)identifier
{'fraction_non_alphanumeric': 0.12015781922525108, 'fraction_numerical': 0.0034074605451936872, 'mean_word_length': 5.135313531353136, 'pattern_counts': {'":': 0, '<': 21, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '895477', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1733, 'n_words': 340}