2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <folly/dynamic.h>
|
|
|
|
|
|
|
|
#include "Executor.h"
|
|
|
|
#include "ExecutorToken.h"
|
|
|
|
#include "JSModulesUnbundle.h"
|
|
|
|
#include "MessageQueueThread.h"
|
|
|
|
#include "MethodCall.h"
|
|
|
|
#include "NativeModule.h"
|
|
|
|
#include "Value.h"
|
|
|
|
|
|
|
|
namespace folly {
|
|
|
|
|
|
|
|
struct dynamic;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
struct InstanceCallback;
|
|
|
|
class ModuleRegistry;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
class ExecutorRegistration {
|
|
|
|
public:
|
|
|
|
ExecutorRegistration(
|
|
|
|
std::unique_ptr<JSExecutor> executor,
|
|
|
|
std::shared_ptr<MessageQueueThread> executorMessageQueueThread) :
|
|
|
|
executor_(std::move(executor)),
|
|
|
|
messageQueueThread_(executorMessageQueueThread) {}
|
|
|
|
|
|
|
|
std::unique_ptr<JSExecutor> executor_;
|
|
|
|
std::shared_ptr<MessageQueueThread> messageQueueThread_;
|
|
|
|
};
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
class JsToNativeBridge;
|
|
|
|
|
|
|
|
// This class manages calls from native code to JS. It also manages
|
|
|
|
// executors and their threads. This part is used by both bridges for
|
|
|
|
// now, but further refactorings should separate the bridges more
|
|
|
|
// fully #11247981.
|
|
|
|
class NativeToJsBridge {
|
2016-05-04 02:29:58 +00:00
|
|
|
public:
|
2016-05-18 19:46:01 +00:00
|
|
|
friend class JsToNativeBridge;
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
/**
|
|
|
|
* This must be called on the main JS thread.
|
|
|
|
*/
|
2016-05-18 19:46:01 +00:00
|
|
|
NativeToJsBridge(
|
2016-05-04 02:29:58 +00:00
|
|
|
JSExecutorFactory* jsExecutorFactory,
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<ModuleRegistry> registry,
|
2016-05-04 02:29:58 +00:00
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue,
|
2016-05-18 19:46:01 +00:00
|
|
|
std::unique_ptr<MessageQueueThread> nativeQueue,
|
|
|
|
std::shared_ptr<InstanceCallback> callback);
|
|
|
|
virtual ~NativeToJsBridge();
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a function with the module ID and method ID and any additional
|
|
|
|
* arguments in JS.
|
|
|
|
*/
|
|
|
|
void callFunction(
|
|
|
|
ExecutorToken executorToken,
|
2016-07-07 15:44:01 +00:00
|
|
|
std::string&& module,
|
|
|
|
std::string&& method,
|
|
|
|
folly::dynamic&& args);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invokes a callback with the cbID, and optional additional arguments in JS.
|
|
|
|
*/
|
2016-07-07 15:44:01 +00:00
|
|
|
void invokeCallback(ExecutorToken executorToken, double callbackId, folly::dynamic&& args);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the JS application from an "bundle", i.e. a JavaScript file that
|
|
|
|
* contains code for all modules and a runtime that resolves and
|
|
|
|
* executes modules.
|
|
|
|
*/
|
2016-05-14 00:15:06 +00:00
|
|
|
void loadApplicationScript(std::unique_ptr<const JSBigString> script, std::string sourceURL);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-07-14 22:23:53 +00:00
|
|
|
/**
|
|
|
|
* Similar to loading a "bundle", but instead of passing js source this method accepts
|
|
|
|
* path to a directory containing files prepared for particular JSExecutor.
|
|
|
|
*/
|
|
|
|
void loadOptimizedApplicationScript(std::string bundlePath, std::string sourceURL, int flags);
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
/**
|
|
|
|
* An "unbundle" is a backend that stores and injects JavaScript modules as
|
|
|
|
* individual scripts, rather than bundling all of them into a single scrupt.
|
|
|
|
*
|
|
|
|
* Loading an unbundle means setting the storage backend and executing the
|
|
|
|
* startup script.
|
|
|
|
*/
|
|
|
|
void loadApplicationUnbundle(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> unbundle,
|
2016-05-14 00:15:06 +00:00
|
|
|
std::unique_ptr<const JSBigString> startupCode,
|
|
|
|
std::string sourceURL);
|
|
|
|
void setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue);
|
2016-05-04 02:29:58 +00:00
|
|
|
void* getJavaScriptContext();
|
|
|
|
bool supportsProfiling();
|
|
|
|
void startProfiler(const std::string& title);
|
|
|
|
void stopProfiler(const std::string& title, const std::string& filename);
|
2016-05-26 18:07:41 +00:00
|
|
|
void handleMemoryPressureUiHidden();
|
2016-05-04 02:29:58 +00:00
|
|
|
void handleMemoryPressureModerate();
|
|
|
|
void handleMemoryPressureCritical();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ExecutorToken corresponding to the main JSExecutor.
|
|
|
|
*/
|
|
|
|
ExecutorToken getMainExecutorToken() const;
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
/**
|
|
|
|
* Synchronously tears down the bridge and the main executor.
|
|
|
|
*/
|
|
|
|
void destroy();
|
|
|
|
private:
|
2016-05-04 02:29:58 +00:00
|
|
|
/**
|
|
|
|
* Registers the given JSExecutor which runs on the given MessageQueueThread
|
2016-05-18 19:46:01 +00:00
|
|
|
* with the NativeToJsBridge. Part of this registration is transfering
|
|
|
|
* ownership of this JSExecutor to the NativeToJsBridge for the duration of
|
|
|
|
* the registration.
|
2016-05-04 02:29:58 +00:00
|
|
|
*
|
|
|
|
* Returns a ExecutorToken which can be used to refer to this JSExecutor
|
2016-05-18 19:46:01 +00:00
|
|
|
* in the NativeToJsBridge.
|
2016-05-04 02:29:58 +00:00
|
|
|
*/
|
|
|
|
ExecutorToken registerExecutor(
|
2016-05-18 19:46:01 +00:00
|
|
|
ExecutorToken token,
|
2016-05-04 02:29:58 +00:00
|
|
|
std::unique_ptr<JSExecutor> executor,
|
|
|
|
std::shared_ptr<MessageQueueThread> executorMessageQueueThread);
|
|
|
|
|
|
|
|
/**
|
2016-05-18 19:46:01 +00:00
|
|
|
* Unregisters a JSExecutor that was previously registered with this NativeToJsBridge
|
|
|
|
* using registerExecutor.
|
2016-05-04 02:29:58 +00:00
|
|
|
*/
|
2016-05-18 19:46:01 +00:00
|
|
|
std::unique_ptr<JSExecutor> unregisterExecutor(JSExecutor& executorToken);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
void runOnExecutorQueue(ExecutorToken token, std::function<void(JSExecutor*)> task);
|
2016-05-18 19:46:01 +00:00
|
|
|
|
|
|
|
// This is used to avoid a race condition where a proxyCallback gets queued
|
|
|
|
// after ~NativeToJsBridge(), on the same thread. In that case, the callback
|
|
|
|
// will try to run the task on m_callback which will have been destroyed
|
|
|
|
// within ~NativeToJsBridge(), thus causing a SIGSEGV.
|
2016-05-04 02:29:58 +00:00
|
|
|
std::shared_ptr<bool> m_destroyed;
|
|
|
|
JSExecutor* m_mainExecutor;
|
2016-05-18 19:46:01 +00:00
|
|
|
ExecutorToken m_mainExecutorToken;
|
|
|
|
std::shared_ptr<JsToNativeBridge> m_delegate;
|
2016-05-04 02:29:58 +00:00
|
|
|
std::unordered_map<JSExecutor*, ExecutorToken> m_executorTokenMap;
|
2016-05-18 19:46:01 +00:00
|
|
|
std::unordered_map<ExecutorToken, ExecutorRegistration> m_executorMap;
|
2016-05-04 02:29:58 +00:00
|
|
|
std::mutex m_registrationMutex;
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
std::atomic_uint_least32_t m_systraceCookie = ATOMIC_VAR_INIT();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MessageQueueThread* getMessageQueueThread(const ExecutorToken& executorToken);
|
|
|
|
JSExecutor* getExecutor(const ExecutorToken& executorToken);
|
2016-05-18 19:46:01 +00:00
|
|
|
ExecutorToken getTokenForExecutor(JSExecutor& executor);
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} }
|