<commit_before>// Copyright 2013 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 <algorithm>
#include <iostream>

#include "base/at_exit.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "mojo/shell/child_process.h"
#include "mojo/shell/context.h"
#include "mojo/shell/init.h"
#include "mojo/shell/mojo_url_resolver.h"
#include "mojo/shell/switches.h"

#if defined(COMPONENT_BUILD)
#include "ui/gl/gl_surface.h"
#endif

namespace {

#if defined(OS_LINUX)
// Copied from ui/gfx/switches.cc to avoid a dependency on //ui/gfx
const char kEnableHarfBuzzRenderText[] = "enable-harfbuzz-rendertext";
#endif

#if defined(OS_WIN)
void SplitString(const base::string16& str, std::vector<std::string>* argv) {
  base::SplitString(base::UTF16ToUTF8(str), ' ', argv);
}
#elif defined(OS_POSIX)
void SplitString(const std::string& str, std::vector<std::string>* argv) {
  base::SplitString(str, ' ', argv);
}
#endif

bool is_empty(const std::string& s) {
  return s.empty();
}

// The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args
// is a list of "configuration" arguments separated by spaces. If one or more
// arguments are specified they will be available when the Mojo application
// is initialized. See ApplicationImpl::args().
GURL GetAppURLAndSetArgs(const base::CommandLine::StringType& app_url_and_args,
                         mojo::shell::Context* context) {
  // SplitString() returns empty strings for extra delimeter characters (' ').
  std::vector<std::string> argv;
  SplitString(app_url_and_args, &argv);
  argv.erase(std::remove_if(argv.begin(), argv.end(), is_empty), argv.end());

  if (argv.empty())
    return GURL::EmptyGURL();
  GURL app_url(argv[0]);
  if (argv.size() > 1)
    context->application_manager()->SetArgsForURL(argv, app_url);
  return app_url;
}

void RunApps(mojo::shell::Context* context) {
  const auto& command_line = *base::CommandLine::ForCurrentProcess();
  for (const auto& arg : command_line.GetArgs())
    context->Run(GetAppURLAndSetArgs(arg, context));
}

void Usage() {
  std::cerr << "Launch Mojo applications.\n";
  std::cerr
      << "Usage: mojo_shell"
      << " [--" << switches::kArgsFor << "=<mojo-app>]"
      << " [--" << switches::kContentHandlers << "=<handlers>]"
      << " [--" << switches::kEnableExternalApplications << "]"
      << " [--" << switches::kDisableCache << "]"
      << " [--" << switches::kEnableMultiprocess << "]"
      << " [--" << switches::kOrigin << "=<url-lib-path>]"
      << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]"
      << " <mojo-app> ...\n\n"
      << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within "
      << "quotes.\n"
      << "Example: mojo_shell \"mojo://js_standalone test.js\".\n"
      << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n"
      << "The value of <handlers> is a comma separated list like:\n"
      << "text/html,mojo://html_viewer,"
      << "application/javascript,mojo://js_content_handler\n";
}

bool ConfigureURLMappings(const std::string& mappings,
                          mojo::shell::MojoURLResolver* resolver) {
  base::StringPairs pairs;
  if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs))
    return false;
  using StringPair = std::pair<std::string, std::string>;
  for (const StringPair& pair : pairs) {
    const GURL from(pair.first);
    const GURL to(pair.second);
    if (!from.is_valid() || !to.is_valid())
      return false;
    resolver->AddCustomMapping(from, to);
  }
  return true;
}

bool isArgsFor(const std::string& arg, std::string* value) {
  const std::string kArgsForSwitches[] = {
    "-" + std::string(switches::kArgsFor),
    "--" + std::string(switches::kArgsFor),
  };
  for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) {
    std::string argsfor_switch(kArgsForSwitches[i]);
    if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) {
      *value = arg.substr(argsfor_switch.size() + 1, std::string::npos);
      return true;
    }
  }
  return false;
}

}  // namespace

int main(int argc, char** argv) {
  base::AtExitManager at_exit;
  base::CommandLine::Init(argc, argv);

  const base::CommandLine& command_line =
    *base::CommandLine::ForCurrentProcess();
  if (command_line.HasSwitch(switches::kHelp) ||
      command_line.GetArgs().empty()){
    Usage();
    return 0;
  }

#if defined(OS_LINUX)
  // We use gfx::RenderText from multiple threads concurrently and the pango
  // backend (currently the default on linux) is not close to threadsafe. Force
  // use of the harfbuzz backend for now.
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      kEnableHarfBuzzRenderText);
#endif
  mojo::shell::InitializeLogging();
<commit_msg>Don't early exit if we're listening for external apps<commit_after>// Copyright 2013 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 <algorithm>
#include <iostream>

#include "base/at_exit.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "mojo/shell/child_process.h"
#include "mojo/shell/context.h"
#include "mojo/shell/init.h"
#include "mojo/shell/mojo_url_resolver.h"
#include "mojo/shell/switches.h"

#if defined(COMPONENT_BUILD)
#include "ui/gl/gl_surface.h"
#endif

namespace {

#if defined(OS_LINUX)
// Copied from ui/gfx/switches.cc to avoid a dependency on //ui/gfx
const char kEnableHarfBuzzRenderText[] = "enable-harfbuzz-rendertext";
#endif

#if defined(OS_WIN)
void SplitString(const base::string16& str, std::vector<std::string>* argv) {
  base::SplitString(base::UTF16ToUTF8(str), ' ', argv);
}
#elif defined(OS_POSIX)
void SplitString(const std::string& str, std::vector<std::string>* argv) {
  base::SplitString(str, ' ', argv);
}
#endif

bool is_empty(const std::string& s) {
  return s.empty();
}

// The value of app_url_and_args is "<mojo_app_url> [<args>...]", where args
// is a list of "configuration" arguments separated by spaces. If one or more
// arguments are specified they will be available when the Mojo application
// is initialized. See ApplicationImpl::args().
GURL GetAppURLAndSetArgs(const base::CommandLine::StringType& app_url_and_args,
                         mojo::shell::Context* context) {
  // SplitString() returns empty strings for extra delimeter characters (' ').
  std::vector<std::string> argv;
  SplitString(app_url_and_args, &argv);
  argv.erase(std::remove_if(argv.begin(), argv.end(), is_empty), argv.end());

  if (argv.empty())
    return GURL::EmptyGURL();
  GURL app_url(argv[0]);
  if (argv.size() > 1)
    context->application_manager()->SetArgsForURL(argv, app_url);
  return app_url;
}

void RunApps(mojo::shell::Context* context) {
  const auto& command_line = *base::CommandLine::ForCurrentProcess();
  for (const auto& arg : command_line.GetArgs())
    context->Run(GetAppURLAndSetArgs(arg, context));
}

void Usage() {
  std::cerr << "Launch Mojo applications.\n";
  std::cerr
      << "Usage: mojo_shell"
      << " [--" << switches::kArgsFor << "=<mojo-app>]"
      << " [--" << switches::kContentHandlers << "=<handlers>]"
      << " [--" << switches::kEnableExternalApplications << "]"
      << " [--" << switches::kDisableCache << "]"
      << " [--" << switches::kEnableMultiprocess << "]"
      << " [--" << switches::kOrigin << "=<url-lib-path>]"
      << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]"
      << " <mojo-app> ...\n\n"
      << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within "
      << "quotes.\n"
      << "Example: mojo_shell \"mojo://js_standalone test.js\".\n"
      << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n"
      << "The value of <handlers> is a comma separated list like:\n"
      << "text/html,mojo://html_viewer,"
      << "application/javascript,mojo://js_content_handler\n";
}

bool ConfigureURLMappings(const std::string& mappings,
                          mojo::shell::MojoURLResolver* resolver) {
  base::StringPairs pairs;
  if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs))
    return false;
  using StringPair = std::pair<std::string, std::string>;
  for (const StringPair& pair : pairs) {
    const GURL from(pair.first);
    const GURL to(pair.second);
    if (!from.is_valid() || !to.is_valid())
      return false;
    resolver->AddCustomMapping(from, to);
  }
  return true;
}

bool isArgsFor(const std::string& arg, std::string* value) {
  const std::string kArgsForSwitches[] = {
    "-" + std::string(switches::kArgsFor),
    "--" + std::string(switches::kArgsFor),
  };
  for (size_t i = 0; i < arraysize(kArgsForSwitches); i++) {
    std::string argsfor_switch(kArgsForSwitches[i]);
    if (arg.compare(0, argsfor_switch.size(), argsfor_switch) == 0) {
      *value = arg.substr(argsfor_switch.size() + 1, std::string::npos);
      return true;
    }
  }
  return false;
}

}  // namespace

int main(int argc, char** argv) {
  base::AtExitManager at_exit;
  base::CommandLine::Init(argc, argv);

  const base::CommandLine& command_line =
    *base::CommandLine::ForCurrentProcess();
  if (!command_line.HasSwitch(switches::kEnableExternalApplications) &&
      (command_line.HasSwitch(switches::kHelp) ||
       command_line.GetArgs().empty())) {
    Usage();
    return 0;
  }

#if defined(OS_LINUX)
  // We use gfx::RenderText from multiple threads concurrently and the pango
  // backend (currently the default on linux) is not close to threadsafe. Force
  // use of the harfbuzz backend for now.
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      kEnableHarfBuzzRenderText);
#endif
  mojo::shell::InitializeLogging();

{'fraction_non_alphanumeric': 0.16412893019576824, 'fraction_numerical': 0.003954913980620921, 'mean_word_length': 4.098286290322581, 'pattern_counts': {'":': 0, '<': 159, '<?xml version=': 0, '>': 45, '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': '5193391', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3395, 'n_words': 765}