java/org/cef/handler/CefAppHandlerAdapter.java (80 lines of code) (raw):
// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
package org.cef.handler;
import org.cef.CefApp;
import org.cef.CefApp.CefAppState;
import org.cef.callback.CefCommandLine;
import org.cef.callback.CefSchemeRegistrar;
import org.cef.misc.CefLog;
import java.util.*;
/**
* An abstract adapter class for managing app handler events.
* The methods in this class are using a default implementation.
* This class exists as convenience for creating handler objects.
*/
public abstract class CefAppHandlerAdapter implements CefAppHandler {
private final String[] args_;
public CefAppHandlerAdapter(String[] args) {
args_ = args;
}
public String[] getArgs() {
return args_ == null ? null : Arrays.copyOf(args_, args_.length);
}
@Override
public void onBeforeCommandLineProcessing(String process_type, CefCommandLine command_line) {
if (process_type.isEmpty() && args_ != null) {
// Forward switches and arguments from Java to Cef
boolean parseSwitchesDone = false;
for (String arg : args_) {
if (parseSwitchesDone || arg.length() < 2) {
command_line.appendArgument(arg);
continue;
}
// Arguments with '--', '-' and, on Windows, '/' prefixes are considered switches.
int switchCnt = arg.startsWith("--") ? 2
: arg.startsWith("/") ? 1
: arg.startsWith("-") ? 1
: 0;
switch (switchCnt) {
case 2:
// An argument of "--" will terminate switch parsing with all subsequent
// tokens
if (arg.length() == 2) {
parseSwitchesDone = true;
continue;
}
// FALL THRU
case 1: {
// Switches can optionally have a value specified using the '=' delimiter
// (e.g. "-switch=value").
String switchStr = arg.substring(switchCnt);
int index = switchStr.indexOf('=');
if (index > 0) {
command_line.appendSwitchWithValue(
switchStr.substring(0, index), switchStr.substring(index + 1));
} else {
command_line.appendSwitch(switchStr);
}
break;
}
case 0:
command_line.appendArgument(arg);
break;
}
}
}
}
@Override
public boolean onBeforeTerminate() {
// The default implementation does nothing
return false;
}
@Override
public void stateHasChanged(CefAppState state) {
// The default implementation does nothing
}
@Override
public void onRegisterCustomSchemes(CefSchemeRegistrar registrar) {
// The default implementation does nothing
}
@Override
public void onContextInitialized() {
// The default implementation does nothing
}
@Override
public void onScheduleMessagePumpWork(long delay_ms) {
final CefApp cefApp = CefApp.getInstanceIfAny();
if (cefApp == null)
return;
if (cefApp.isTerminated()) {
CefLog.Debug("CefApp is terminated, skip doMessageLoopWork");
return;
}
cefApp.doMessageLoopWork(delay_ms);
}
@Override
public void onBeforeChildProcessLaunch(String command_line) {
// The default implementation does nothing
}
}