public void setDOMStorageItem()

in stetho/src/main/java/com/facebook/stetho/inspector/protocol/module/DOMStorage.java [78:130]


  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());

        // Force the DevTools UI to refresh with the old value again (it assumes that the set
        // operation succeeded).  Note that we should be able to do this by throwing
        // 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);
        }
      }
    }
  }