Search in sources :

Example 6 with ChromeDevtoolsMethod

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

the class DOM method resolveNode.

@ChromeDevtoolsMethod
public ResolveNodeResponse resolveNode(JsonRpcPeer peer, JSONObject params) throws JsonRpcException {
    final ResolveNodeRequest request = mObjectMapper.convertValue(params, ResolveNodeRequest.class);
    final Object element = mDocument.postAndWait(new UncheckedCallable<Object>() {

        @Override
        public Object call() {
            return mDocument.getElementForNodeId(request.nodeId);
        }
    });
    if (element == null) {
        throw new JsonRpcException(new JsonRpcError(JsonRpcError.ErrorCode.INVALID_PARAMS, "No known nodeId=" + request.nodeId, null));
    }
    int mappedObjectId = Runtime.mapObject(peer, element);
    Runtime.RemoteObject remoteObject = new Runtime.RemoteObject();
    remoteObject.type = Runtime.ObjectType.OBJECT;
    remoteObject.subtype = Runtime.ObjectSubType.NODE;
    remoteObject.className = element.getClass().getName();
    // not a primitive
    remoteObject.value = null;
    // not sure what this does...
    remoteObject.description = null;
    remoteObject.objectId = String.valueOf(mappedObjectId);
    ResolveNodeResponse response = new ResolveNodeResponse();
    response.object = remoteObject;
    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 7 with ChromeDevtoolsMethod

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

the class DOMStorage method setDOMStorageItem.

@ChromeDevtoolsMethod
public void setDOMStorageItem(JsonRpcPeer peer, JSONObject params) throws JSONException, JsonRpcException {
    StorageId storage = mObjectMapper.convertValue(params.getJSONObject("storageId"), StorageId.class);
    String key = params.getString("key");
    String value = params.getString("value");
    if (storage.isLocalStorage) {
        SharedPreferences prefs = mContext.getSharedPreferences(storage.securityOrigin, Context.MODE_PRIVATE);
        Object existingValue = prefs.getAll().get(key);
        try {
            if (existingValue == null) {
                throw new DOMStorageAssignmentException("Unsupported: cannot add new key " + key + " due to lack of type inference");
            } else {
                SharedPreferences.Editor editor = prefs.edit();
                try {
                    assignByType(editor, key, SharedPreferencesHelper.valueFromString(value, existingValue));
                    editor.apply();
                } catch (IllegalArgumentException e) {
                    throw new DOMStorageAssignmentException(String.format(Locale.US, "Type mismatch setting %s to %s (expected %s)", key, value, existingValue.getClass().getSimpleName()));
                }
            }
        } catch (DOMStorageAssignmentException e) {
            CLog.writeToConsole(mDOMStoragePeerManager, Console.MessageLevel.ERROR, Console.MessageSource.STORAGE, e.getMessage());
            // JsonRpcException but the UI doesn't respect setDOMStorageItem failure.
            if (prefs.contains(key)) {
                mDOMStoragePeerManager.signalItemUpdated(storage, key, value, SharedPreferencesHelper.valueToString(existingValue));
            } else {
                mDOMStoragePeerManager.signalItemRemoved(storage, key);
            }
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) JSONObject(org.json.JSONObject) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)

Example 8 with ChromeDevtoolsMethod

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

the class DOMStorage method removeDOMStorageItem.

@ChromeDevtoolsMethod
public void removeDOMStorageItem(JsonRpcPeer peer, JSONObject params) throws JSONException {
    StorageId storage = mObjectMapper.convertValue(params.getJSONObject("storageId"), StorageId.class);
    String key = params.getString("key");
    if (storage.isLocalStorage) {
        SharedPreferences prefs = mContext.getSharedPreferences(storage.securityOrigin, Context.MODE_PRIVATE);
        prefs.edit().remove(key).apply();
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)

Example 9 with ChromeDevtoolsMethod

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

the class DOMStorage method getDOMStorageItems.

@ChromeDevtoolsMethod
public JsonRpcResult getDOMStorageItems(JsonRpcPeer peer, JSONObject params) throws JSONException {
    StorageId storage = mObjectMapper.convertValue(params.getJSONObject("storageId"), StorageId.class);
    ArrayList<List<String>> entries = new ArrayList<List<String>>();
    String prefTag = storage.securityOrigin;
    if (storage.isLocalStorage) {
        SharedPreferences prefs = mContext.getSharedPreferences(prefTag, Context.MODE_PRIVATE);
        for (Map.Entry<String, ?> prefsEntry : prefs.getAll().entrySet()) {
            ArrayList<String> entry = new ArrayList<String>(2);
            entry.add(prefsEntry.getKey());
            entry.add(SharedPreferencesHelper.valueToString(prefsEntry.getValue()));
            entries.add(entry);
        }
    }
    GetDOMStorageItemsResult result = new GetDOMStorageItemsResult();
    result.entries = entries;
    return result;
}
Also used : SharedPreferences(android.content.SharedPreferences) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ChromeDevtoolsMethod(com.facebook.stetho.inspector.protocol.ChromeDevtoolsMethod)

Example 10 with ChromeDevtoolsMethod

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

the class Database method executeSQL.

@ChromeDevtoolsMethod
public JsonRpcResult executeSQL(JsonRpcPeer peer, JSONObject params) {
    ExecuteSQLRequest request = mObjectMapper.convertValue(params, ExecuteSQLRequest.class);
    String databaseId = request.databaseId;
    String query = request.query;
    DatabaseDriver databaseDriver = getDatabasePeer(databaseId);
    try {
        return databaseDriver.executeSQL(request.databaseId, request.query, new DatabaseDriver.ExecuteResultHandler<ExecuteSQLResponse>() {

            @Override
            public ExecuteSQLResponse handleRawQuery() throws SQLiteException {
                ExecuteSQLResponse response = new ExecuteSQLResponse();
                // This is done because the inspector UI likes to delete rows if you give them no
                // name/value list
                response.columnNames = Collections.singletonList("success");
                response.values = Collections.singletonList("true");
                return response;
            }

            @Override
            public ExecuteSQLResponse handleSelect(Cursor result) throws SQLiteException {
                ExecuteSQLResponse response = new ExecuteSQLResponse();
                response.columnNames = Arrays.asList(result.getColumnNames());
                response.values = flattenRows(result, MAX_EXECUTE_RESULTS);
                return response;
            }

            @Override
            public ExecuteSQLResponse handleInsert(long insertedId) throws SQLiteException {
                ExecuteSQLResponse response = new ExecuteSQLResponse();
                response.columnNames = Collections.singletonList("ID of last inserted row");
                response.values = Collections.singletonList(String.valueOf(insertedId));
                return response;
            }

            @Override
            public ExecuteSQLResponse handleUpdateDelete(int count) throws SQLiteException {
                ExecuteSQLResponse response = new ExecuteSQLResponse();
                response.columnNames = Collections.singletonList("Modified rows");
                response.values = Collections.singletonList(String.valueOf(count));
                return response;
            }
        });
    } catch (RuntimeException e) {
        LogUtil.e(e, "Exception executing: %s", request.query);
        Error error = new Error();
        error.code = 0;
        error.message = e.getMessage();
        ExecuteSQLResponse response = new ExecuteSQLResponse();
        response.sqlError = error;
        return response;
    }
}
Also used : JsonRpcError(com.facebook.stetho.inspector.jsonrpc.protocol.JsonRpcError) SQLiteException(android.database.sqlite.SQLiteException) Cursor(android.database.Cursor) 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