Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10] RN debugger fixes #3368

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions lib/browser/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,42 @@

'use strict';

import { keys, objectTypes } from './constants';
import { createMethods } from './util';
import { keys, objectTypes } from "./constants";
import { createMethods } from "./util";
import { createAppRPC, _logInRPC } from "./rpc";

function setupApp(app, info) {
app[keys.id] = info.id;
app[keys.realm] = "(App object)";
app[keys.type] = objectTypes.APP;
}

export default class App {
constructor(config) {
let info = createAppRPC(config);
setupApp(this, info);
}

_logIn(creds, callback) {
return _logInRPC(creds, callback);
}
}

createMethods(App.prototype, objectTypes.APP, [
'logIn',
'allUsers',
'currentUser',
'switchUser'
"logIn",
"allUsers",
"currentUser",
"switchUser"
]);

Object.defineProperties(App, {
_logIn: {
value: function(creds, callback) {
return _logInRPC(App[keys.id], creds, callback)
},
},
});

export function createApp(realmId, info) {
const appProxy = Object.create(App.prototype);

Expand Down
5 changes: 1 addition & 4 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import App, { createApp } from './app';
import Credentials, { createCredentials } from './credentials';
import * as rpc from './rpc';
import * as util from './util';
import { static as staticUserMethods } from '../user';
import { createSession } from './session';
import { invalidateCache } from './cache';

Expand Down Expand Up @@ -212,9 +211,7 @@ Object.defineProperties(Realm, {

for (let i = 0, len = debugHosts.length; i < len; i++) {
try {
let refreshAccessTokenCallback = staticUserMethods && staticUserMethods._refreshAccessToken ? staticUserMethods._refreshAccessToken.bind(User) : () => {};
// The session ID refers to the Realm constructor object in the RPC server.
Realm[keys.id] = createSession(refreshAccessTokenCallback, debugHosts[i] + ':' + debugPort);
Realm[keys.id] = rpc.createSession(debugHosts[i] + ":" + debugPort);
break;
} catch (e) {
// Only throw exception after all hosts have been tried.
Expand Down
26 changes: 25 additions & 1 deletion lib/browser/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
'use strict';

import * as base64 from './base64';
import * as util from './util';
import { keys, objectTypes } from './constants';
import { invalidateCache } from './cache';

Expand Down Expand Up @@ -65,6 +64,14 @@ function beforeNotify(realm) {
invalidateCache(realm[keys.realm]);
}


export function createSession(host) {
sessionHost = host;
sessionId = sendRequest("create_session", null, host);

return sessionId;
}

export function createRealm(args) {
if (args) {
args = args.map((arg) => serialize(null, arg));
Expand All @@ -73,6 +80,10 @@ export function createRealm(args) {
return sendRequest('create_realm', { arguments: args, beforeNotify: serialize(null, beforeNotify) });
}

export function createAppRPC(config) {
return sendRequest("create_app", { arguments: [ serialize(null, config) ] });
}

export function asyncOpenRealm(id, config, callback) {
return deserialize(undefined, sendRequest('call_method', {
id,
Expand All @@ -89,6 +100,19 @@ export function asyncOpenRealm(id, config, callback) {
}));
}

export function _logInRPC(id, creds, callback) {
return deserialize(undefined, sendRequest("call_method", {
id,
name: "_logIn",
arguments: [
serialize(null, creds),
serialize(null, (user, error) => {
callback(user, error);
})
]
}));
}

export function _anonymousRPC() {
const result = sendRequest('_anonymous', { arguments: undefined });
return deserialize(undefined, result);
Expand Down
10 changes: 10 additions & 0 deletions src/js_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ void AppClass<T>::constructor(ContextType ctx, ObjectType this_object, Arguments
auto result = js::Function<T>::call(ctx, js::Value<T>::to_function(ctx, user_agent_function), realm_constructor, 0, nullptr);
user_agent_binding_info = js::Value<T>::validated_to_string(ctx, result);
}
else {
// FIXME: When debugging RN apps, _createUserAgentDescription cannot be found
user_agent_binding_info = "N/A";
}
ensure_directory_exists_for_file(default_realm_file_directory());

auto platform_description_function = js::Object<T>::get_property(ctx, realm_constructor, "_createPlatformDescription");
Expand All @@ -185,6 +189,12 @@ void AppClass<T>::constructor(ContextType ctx, ObjectType this_object, Arguments
config.platform_version = js::Value<T>::validated_to_string(ctx, Object::get_property(ctx, result_object, platform_version_name));
config.sdk_version = js::Value<T>::validated_to_string(ctx, Object::get_property(ctx, result_object, sdk_version_name));
}
else {
// FIXME: When debugging RN apps, _createPlatformDescription() cannot be found
config.platform = "N/A";
config.platform_version = "N/A";
config.sdk_version = "N/A";
}

SyncClientConfig client_config;
client_config.base_file_path = default_realm_file_directory();
Expand Down
46 changes: 46 additions & 0 deletions src/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static const char * const RealmObjectTypesRealm = "realm";
static const char * const RealmObjectTypesUser = "user";
static const char * const RealmObjectTypesSession = "session";
static const char * const RealmObjectTypesAsyncOpenTask = "asyncopentask";
static const char * const RealmObjectTypesApp = "app";
static const char * const RealmObjectTypesUndefined = "undefined";

json serialize_object_schema(const realm::ObjectSchema &object_schema) {
Expand Down Expand Up @@ -248,6 +249,15 @@ RPCServer::RPCServer() {
setIncludesNativeCallStack(m_context, false);
}

m_requests["/create_session"] = [this](const json dict) {
RJSInitializeInContext(m_context);

jsc::String realm_string = "Realm";
JSObjectRef realm_constructor = jsc::Object::validated_get_constructor(m_context, JSContextGetGlobalObject(m_context), realm_string);

m_session_id = store_object(realm_constructor);
return (json){{"result", m_session_id}};
};
m_requests["/create_realm"] = [this](const json dict) {
JSObjectRef realm_constructor = get_realm_constructor();

Expand All @@ -270,6 +280,21 @@ RPCServer::RPCServer() {

return (json){{"result", serialize_json_value(realm_object)}};
};
m_requests["/create_app"] = [this](const json dict) {
JSObjectRef realm_constructor = get_realm_constructor();
JSObjectRef app_constructor = (JSObjectRef)jsc::Object::get_property(m_context, realm_constructor, "App");

json::array_t args = dict["arguments"];
size_t arg_count = args.size();
JSValueRef arg_values[arg_count];

for (size_t i = 0; i < arg_count; i++) {
arg_values[i] = deserialize_json_value(args[i]);
}

JSObjectRef app_object = jsc::Function::construct(m_context, app_constructor, arg_count, arg_values);
return (json){{"result", serialize_json_value(app_object)}};
};
m_requests["/create_user"] = [this](const json dict) {
JSObjectRef realm_constructor = get_realm_constructor();

Expand Down Expand Up @@ -321,6 +346,21 @@ RPCServer::RPCServer() {
auto result = jsc::Function::call(m_context, _asyncOpen_method, arg_count, arg_values);
return (json){{"result", serialize_json_value(result)}};
};
m_requests["/_logIn"] = [this](const json dict) {
JSObjectRef app_object = get_object(dict["id"].get<RPCObjectID>());
JSObjectRef _logIn_method = (JSObjectRef)jsc::Object::get_property(m_context, app_object, "_logIn");

json::array_t args = dict["arguments"];
size_t arg_count = args.size();
JSValueRef arg_values[arg_count];

for (size_t i = 0; i < arg_count; i++) {
arg_values[i] = deserialize_json_value(args[i]);
}

auto result = jsc::Function::call(m_context, _logIn_method, arg_count, arg_values);
return (json){{"result", serialize_json_value(result)}};
};
m_requests["/call_method"] = [this](const json dict) {
JSObjectRef object = get_object(dict["id"].get<RPCObjectID>());
std::string method_string = dict["name"].get<std::string>();
Expand Down Expand Up @@ -827,6 +867,12 @@ json RPCServer::serialize_json_value(JSValueRef js_value) {
{"id", store_object(js_object)},
};
}
else if (jsc::Object::is_instance<js::AppClass<jsc::Types>>(m_context, js_object)) {
return {
{"type", RealmObjectTypesApp},
{"id", store_object(js_object)},
};
}
#endif
else if (jsc::Value::is_array(m_context, js_object)) {
uint32_t length = jsc::Object::validated_get_length(m_context, js_object);
Expand Down