bool CommandLineArgs::init()

in remote/CommandLineArgs.cpp [52:190]


bool CommandLineArgs::init(int argc, char* argv[]) {
  // This method is called very early.

  // 1. Initialize logger at first and check '--help'
  if (doTrace)
    Log::trace("CommandLineArgs: init with args:");
  for (int c = 0; c < argc; ++c) {
    const char * arg = argv[c];
    if (arg == nullptr || arg[0] == 0) continue;

    std::string word(arg);
    size_t tokenPos;
    if (word.compare("--help") == 0 || word.compare("-h") == 0) {
      fprintf(stdout, "Usage: cef-server [--port=PORT] [--params=PARAMS_FILE] [--loglevel=LEVEL] [--logfile=FILE]\nOther switches are enumerated in CommandLineArgs.cpp\n");
      return false;
    }
    if ((tokenPos = word.find("--logfile=")) != word.npos) {
      myPathLogFile = word.substr(tokenPos + 10);
    } else if ((tokenPos = word.find("--loglevel=")) != word.npos) {
      std::string sval = word.substr(tokenPos + 11);
      try {
        myLogLevel = std::stoi(sval);
      } catch (const std::exception&) {
        myLogLevel = Log::str2level(word);
      }
    }
    if (doTrace)
      Log::trace("\t%s", word.c_str());
  } // for

  Log::init(myLogLevel, myPathLogFile);

  // 2. Parse other command line arguments.
  for (int c = 0; c < argc; ++c) {
    const char * arg = argv[c];
    if (arg == nullptr || arg[0] == 0) continue;

    // NOTE: these switches don't conflict with chromium one.
    // See https://peter.sh/experiments/chromium-command-line-switches/
    std::string word(arg);
    if (word.find("--loglevel") == 0 || word.find("--logfile") == 0)
      continue;

    if (doTrace)
      Log::trace("\tprocess cmd line arg: %s", word.c_str());

    if (word == "--cef-server-wait-debugger") {
      myWaitDebugger = true;
      continue;
    }

    std::string stmp;
    int ntmp;
    size_t tokenPos;
    if ((tokenPos = word.find("--port=")) != word.npos) {
      std::string val = word.substr(tokenPos + 7);
      myPort = std::stoi(val);
      myUseTcp = true;
    } else if ((tokenPos = word.find("--pipe=")) != word.npos) {
      myPathPipe = word.substr(tokenPos + 7);
      myUseTcp = false;
    } else if ((tokenPos = word.find("--params=")) != word.npos) {
      myPathParamsFile = word.substr(tokenPos + 9);
    } else if ((tokenPos = word.find("--root=")) != word.npos) {
      myPathRootCache = word.substr(tokenPos + 7);
    } else if ((tokenPos = word.find("--logchromiumfile=")) != word.npos) {
      myPathChromiumLogFile = word.substr(tokenPos + 18);
    } else if ((tokenPos = word.find("--logchromiumlevel=")) != word.npos) {
      std::string sval = word.substr(tokenPos + 19);
      try {
        myLogLevelChromium = std::stoi(sval);
      } catch (const std::exception&) {}
    } else if ((tokenPos = word.find("--deleteRootCacheDir")) != word.npos) {
      myDeleteRootCacheDir = true;
    } else if (CefSettingsParser::parseCefCmdLineSwitch(word, stmp)) {
      myChromiumSwitches.push_back(stmp);
    } else if (CefSettingsParser::parseCefSchemeWord(word, stmp, ntmp)) {
      myCustomSchemes.push_back(std::make_pair(stmp, ntmp));
    } else if (CefSettingsParser::parseCefSettingWord(word, myParsedCefSettings)) {
      ; // nothing to do
    } else if (word.find("--") == 0) {
      myChromiumSwitches.push_back(word);
    } else {
      if (doTrace)
        Log::trace("Parse command line: skip unknown word %s", word.c_str());
    }
  } // for

  trace("command line", myChromiumSwitches, myParsedCefSettings, myCustomSchemes);

  if (!myPathParamsFile.empty()) {
    std::vector<std::pair<std::string, std::string>> fileSettings;
    std::vector<std::string> fileSwitches;
    std::vector<std::pair<std::string, int>> fileSchemes;
    CefSettingsParser::parseParamsFile(myPathParamsFile, fileSwitches, fileSettings, fileSchemes);
    if (!fileSwitches.empty() || !fileSettings.empty() || !fileSchemes.empty()) {
      Log::debug("Params file isn't empty, some command line arguments can be overriden.");
      trace("file", fileSwitches, fileSettings, fileSchemes);
      myChromiumSwitches.insert(myChromiumSwitches.end(), fileSwitches.begin(), fileSwitches.end());
      myParsedCefSettings.insert(myParsedCefSettings.end(), fileSettings.begin(), fileSettings.end());
      myCustomSchemes.insert(myCustomSchemes.end(), fileSchemes.begin(), fileSchemes.end());
    }
  }

  // Init default chromium switches and CefSettings (if necessary)
  if (myChromiumSwitches.empty() && !dontUseDefaultChromiumSwitches) { // NOTE: dontUseDefaultChromiumSwitches == false by default
    Log::debug("Use default chromium switches.");
#if defined(OS_WIN)
     myChromiumSwitches.push_back("--disable-features=SpareRendererForSitePerProcess");
     myChromiumSwitches.push_back("--disable-gpu-process-crash-limit");
     myChromiumSwitches.push_back("--autoplay-policy=no-user-gesture-required");
     myChromiumSwitches.push_back("--disable-component-update");
#elif defined(OS_MAC)
    myChromiumSwitches.push_back("--disable-in-process-stack-traces");
    myChromiumSwitches.push_back("--use-mock-keychain");
    myChromiumSwitches.push_back("--disable-features=SpareRendererForSitePerProcess");
    myChromiumSwitches.push_back("--disable-notifications");
    myChromiumSwitches.push_back("--disable-gpu-process-crash-limit");
    myChromiumSwitches.push_back("--autoplay-policy=no-user-gesture-required");
    myChromiumSwitches.push_back("--disable-component-update");
  #else
    myChromiumSwitches.push_back("--disable-features=SpareRendererForSitePerProcess");
    myChromiumSwitches.push_back("--disable-gpu-process-crash-limit");
    myChromiumSwitches.push_back("--autoplay-policy=no-user-gesture-required");
    myChromiumSwitches.push_back("--disable-component-update");
    myChromiumSwitches.push_back("--no-proxy-server");
  #endif
  } // myChromiumSwitches.empty()

  if (myParsedCefSettings.empty()) {
    Log::debug("Use default cef settings.");
    myParsedCefSettings.push_back(std::make_pair("log_severity", Log::cefLogLevel2str(myLogLevelChromium)));
    if (!myPathChromiumLogFile.empty())
      myParsedCefSettings.push_back(std::make_pair("log_file", myPathChromiumLogFile));
  }

  trace("merged", myChromiumSwitches, myParsedCefSettings, myCustomSchemes);
  return true;
}