Search in sources :

Example 1 with ChromeDevtoolsMethod

use of com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod in project stetho by facebook.

the class CSS method setPropertyText.

@ChromeDevtoolsMethod
public SetPropertyTextResult setPropertyText(JsonRpcPeer peer, JSONObject params) {
    final SetPropertyTextRequest request = mObjectMapper.convertValue(params, SetPropertyTextRequest.class);
    final String[] parts = request.styleSheetId.split("\\.", 2);
    final int nodeId = Integer.parseInt(parts[0]);
    final String ruleName = parts[1];
    final String value;
    final String key;
    if ("/* undefined */".equals(request.text)) {
        // This gets sent when a key is disabled. Chrome does not send the key which was disabled
        // though so not much we can do here.
        key = null;
        value = null;
    } else {
        final String[] keyValue = request.text.split(":", 2);
        key = keyValue[0].trim();
        value = StringUtil.removeAll(keyValue[1], ';').trim();
    }
    final SetPropertyTextResult result = new SetPropertyTextResult();
    result.style = new CSSStyle();
    result.style.styleSheetId = request.styleSheetId;
    result.style.cssProperties = new ArrayList<>();
    result.style.shorthandEntries = Collections.emptyList();
    mDocument.postAndWait(new Runnable() {

        @Override
        public void run() {
            final Object elementForNodeId = mDocument.getElementForNodeId(nodeId);
            if (elementForNodeId == null) {
                LogUtil.w("Failed to get style of an element that does not exist, nodeid=" + nodeId);
                return;
            }
            if (key != null) {
                mDocument.setElementStyle(elementForNodeId, ruleName, key, value);
            }
            mDocument.getElementStyles(elementForNodeId, ruleName, new StyleAccumulator() {

                @Override
                public void store(String name, String value, boolean isDefault) {
                    final CSSProperty property = new CSSProperty();
                    property.name = name;
                    property.value = value;
                    result.style.cssProperties.add(property);
                }
            });
        }
    });
    return result;
}
Also used : JSONObject(org.json.JSONObject) ComputedStyleAccumulator(com.facebook.stetho.inspector.elements.ComputedStyleAccumulator) StyleAccumulator(com.facebook.stetho.inspector.elements.StyleAccumulator) SuppressLint(android.annotation.SuppressLint) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)

Example 2 with ChromeDevtoolsMethod

use of com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod in project stetho by facebook.

the class CSS method getMatchedStylesForNode.

@SuppressLint("DefaultLocale")
@ChromeDevtoolsMethod
public JsonRpcResult getMatchedStylesForNode(JsonRpcPeer peer, JSONObject params) {
    final GetMatchedStylesForNodeRequest request = mObjectMapper.convertValue(params, GetMatchedStylesForNodeRequest.class);
    final GetMatchedStylesForNodeResult result = new GetMatchedStylesForNodeResult();
    result.matchedCSSRules = new ArrayList<>();
    result.inherited = Collections.emptyList();
    result.pseudoElements = Collections.emptyList();
    mDocument.postAndWait(new Runnable() {

        @Override
        public void run() {
            final Object elementForNodeId = mDocument.getElementForNodeId(request.nodeId);
            if (elementForNodeId == null) {
                LogUtil.w("Failed to get style of an element that does not exist, nodeid=" + request.nodeId);
                return;
            }
            mDocument.getElementStyleRuleNames(elementForNodeId, new StyleRuleNameAccumulator() {

                @Override
                public void store(String ruleName, boolean editable) {
                    final ArrayList<CSSProperty> properties = new ArrayList<>();
                    final RuleMatch match = new RuleMatch();
                    match.matchingSelectors = ListUtil.newImmutableList(0);
                    final Selector selector = new Selector();
                    selector.value = ruleName;
                    final CSSRule rule = new CSSRule();
                    rule.origin = Origin.REGULAR;
                    rule.selectorList = new SelectorList();
                    rule.selectorList.selectors = ListUtil.newImmutableList(selector);
                    rule.style = new CSSStyle();
                    rule.style.cssProperties = properties;
                    rule.style.shorthandEntries = Collections.emptyList();
                    if (editable) {
                        rule.style.styleSheetId = String.format("%s.%s", Integer.toString(request.nodeId), selector.value);
                    }
                    mDocument.getElementStyles(elementForNodeId, ruleName, new StyleAccumulator() {

                        @Override
                        public void store(String name, String value, boolean isDefault) {
                            final CSSProperty property = new CSSProperty();
                            property.name = name;
                            property.value = value;
                            properties.add(property);
                        }
                    });
                    match.rule = rule;
                    result.matchedCSSRules.add(match);
                }
            });
        }
    });
    return result;
}
Also used : ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) ComputedStyleAccumulator(com.facebook.stetho.inspector.elements.ComputedStyleAccumulator) StyleAccumulator(com.facebook.stetho.inspector.elements.StyleAccumulator) StyleRuleNameAccumulator(com.facebook.stetho.inspector.elements.StyleRuleNameAccumulator) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod) SuppressLint(android.annotation.SuppressLint)

Example 3 with ChromeDevtoolsMethod

use of com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod in project stetho by facebook.

the class Database method getDatabaseTableNames.

@ChromeDevtoolsMethod
public JsonRpcResult getDatabaseTableNames(JsonRpcPeer peer, JSONObject params) throws JsonRpcException {
    GetDatabaseTableNamesRequest request = mObjectMapper.convertValue(params, GetDatabaseTableNamesRequest.class);
    String databaseId = request.databaseId;
    DatabaseDriver databaseDriver = getDatabasePeer(databaseId);
    try {
        GetDatabaseTableNamesResponse response = new GetDatabaseTableNamesResponse();
        response.tableNames = databaseDriver.getTableNames(request.databaseId);
        return response;
    } catch (SQLiteException e) {
        throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.INVALID_REQUEST, e.toString(), null));
    }
}
Also used : JsonRpcError(com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcError) SQLiteException(android.database.sqlite.SQLiteException) JsonRpcException(com.facebook.stetho.inspector.jsonrpc.JsonRpcException) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)

Example 4 with ChromeDevtoolsMethod

use of com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod in project stetho by facebook.

the class Runtime method callFunctionOn.

@ChromeDevtoolsMethod
public CallFunctionOnResponse callFunctionOn(JsonRpcPeer peer, JSONObject params) throws JsonRpcException {
    CallFunctionOnRequest args = mObjectMapper.convertValue(params, CallFunctionOnRequest.class);
    Session session = getSession(peer);
    Object object = session.getObjectOrThrow(args.objectId);
    // what this function does by name.
    if (!args.functionDeclaration.startsWith("function protoList(")) {
        throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.INTERNAL_ERROR, "Expected protoList, got: " + args.functionDeclaration, null));
    }
    // Since this is really a function call we have to create this fake object to hold the
    // "result" of the function.
    ObjectProtoContainer objectContainer = new ObjectProtoContainer(object);
    RemoteObject result = new RemoteObject();
    result.type = ObjectType.OBJECT;
    result.subtype = ObjectSubType.NODE;
    result.className = object.getClass().getName();
    result.description = getPropertyClassName(object);
    result.objectId = String.valueOf(session.getObjects().putObject(objectContainer));
    CallFunctionOnResponse response = new CallFunctionOnResponse();
    response.result = result;
    response.wasThrown = false;
    return response;
}
Also used : JSONObject(org.json.JSONObject) JsonRpcError(com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcError) JsonRpcException(com.facebook.stetho.inspector.jsonrpc.JsonRpcException) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)

Example 5 with ChromeDevtoolsMethod

use of com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod in project stetho by facebook.

the class DOM method getDocument.

@ChromeDevtoolsMethod
public JsonRpcResult getDocument(JsonRpcPeer peer, JSONObject params) {
    final GetDocumentResponse result = new GetDocumentResponse();
    result.root = mDocument.postAndWait(new UncheckedCallable<Node>() {

        @Override
        public Node call() {
            Object element = mDocument.getRootElement();
            return createNodeForElement(element, mDocument.getDocumentView(), null);
        }
    });
    return result;
}
Also used : UncheckedCallable(com.facebook.stetho.common.UncheckedCallable) JSONObject(org.json.JSONObject) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)

Aggregations

ChromeDevtoolsMethod (com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)11 JSONObject (org.json.JSONObject)6 JsonRpcError (com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcError)4 SharedPreferences (android.content.SharedPreferences)3 JsonRpcException (com.facebook.stetho.inspector.jsonrpc.JsonRpcException)3 SuppressLint (android.annotation.SuppressLint)2 SQLiteException (android.database.sqlite.SQLiteException)2 ComputedStyleAccumulator (com.facebook.stetho.inspector.elements.ComputedStyleAccumulator)2 StyleAccumulator (com.facebook.stetho.inspector.elements.StyleAccumulator)2 ArrayList (java.util.ArrayList)2 Cursor (android.database.Cursor)1 UncheckedCallable (com.facebook.stetho.common.UncheckedCallable)1 StyleRuleNameAccumulator (com.facebook.stetho.inspector.elements.StyleRuleNameAccumulator)1 ScreencastDispatcher (com.facebook.stetho.inspector.screencast.ScreencastDispatcher)1 List (java.util.List)1 Map (java.util.Map)1