// Copyright 2017 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_ #define V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_ #include #include #include #include "include/v8-array-buffer.h" #include "include/v8-inspector.h" #include "include/v8-local-handle.h" #include "include/v8-locker.h" #include "include/v8-script.h" #include "src/base/optional.h" namespace v8 { class Context; class Isolate; class ObjectTemplate; class StartupData; namespace internal { class FrontendChannelImpl; class TaskRunner; enum WithInspector : bool { kWithInspector = true, kNoInspector = false }; class InspectorIsolateData : public v8_inspector::V8InspectorClient { public: class SetupGlobalTask { public: virtual ~SetupGlobalTask() = default; virtual void Run(v8::Isolate* isolate, v8::Local global) = 0; }; using SetupGlobalTasks = std::vector>; InspectorIsolateData(const InspectorIsolateData&) = delete; InspectorIsolateData& operator=(const InspectorIsolateData&) = delete; InspectorIsolateData(TaskRunner* task_runner, SetupGlobalTasks setup_global_tasks, v8::StartupData* startup_data, WithInspector with_inspector); static InspectorIsolateData* FromContext(v8::Local context); ~InspectorIsolateData() override; v8::Isolate* isolate() const { return isolate_.get(); } TaskRunner* task_runner() const { return task_runner_; } // Setting things up. int CreateContextGroup(); V8_NODISCARD bool CreateContext(int context_group_id, v8_inspector::StringView name); void ResetContextGroup(int context_group_id); v8::Local GetDefaultContext(int context_group_id); int GetContextGroupId(v8::Local context); void RegisterModule(v8::Local context, std::vector name, v8::ScriptCompiler::Source* source); // Working with V8Inspector api. int ConnectSession(int context_group_id, const v8_inspector::StringView& state, std::unique_ptr channel); std::vector DisconnectSession(int session_id, TaskRunner* context_task_runner); void SendMessage(int session_id, const v8_inspector::StringView& message); void BreakProgram(int context_group_id, const v8_inspector::StringView& reason, const v8_inspector::StringView& details); void Stop(int session_id); void SchedulePauseOnNextStatement(int context_group_id, const v8_inspector::StringView& reason, const v8_inspector::StringView& details); void CancelPauseOnNextStatement(int context_group_id); void AsyncTaskScheduled(const v8_inspector::StringView& name, void* task, bool recurring); void AsyncTaskStarted(void* task); void AsyncTaskFinished(void* task); v8_inspector::V8StackTraceId StoreCurrentStackTrace( const v8_inspector::StringView& description); void ExternalAsyncTaskStarted(const v8_inspector::V8StackTraceId& parent); void ExternalAsyncTaskFinished(const v8_inspector::V8StackTraceId& parent); void AddInspectedObject(int session_id, v8::Local object); // Test utilities. void SetCurrentTimeMS(double time); void SetMemoryInfo(v8::Local memory_info); void SetLogConsoleApiMessageCalls(bool log); void SetLogMaxAsyncCallStackDepthChanged(bool log); void SetAdditionalConsoleApi(v8_inspector::StringView api_script); void SetMaxAsyncTaskStacksForTest(int limit); void DumpAsyncTaskStacksStateForTest(); void FireContextCreated(v8::Local context, int context_group_id, v8_inspector::StringView name); void FireContextDestroyed(v8::Local context); void FreeContext(v8::Local context); void SetResourceNamePrefix(v8::Local prefix); bool AssociateExceptionData(v8::Local exception, v8::Local key, v8::Local value); void WaitForDebugger(int context_group_id); private: static v8::MaybeLocal ModuleResolveCallback( v8::Local context, v8::Local specifier, v8::Local import_assertions, v8::Local referrer); static void MessageHandler(v8::Local message, v8::Local exception); static void PromiseRejectHandler(v8::PromiseRejectMessage data); static int HandleMessage(v8::Local message, v8::Local exception); std::vector GetSessionIds(int context_group_id); // V8InspectorClient implementation. v8::Local ensureDefaultContextInGroup( int context_group_id) override; double currentTimeMS() override; v8::MaybeLocal memoryInfo(v8::Isolate* isolate, v8::Local) override; void runMessageLoopOnPause(int context_group_id) override; void runIfWaitingForDebugger(int context_group_id) override; void quitMessageLoopOnPause() override; void installAdditionalCommandLineAPI(v8::Local, v8::Local) override; void consoleAPIMessage(int contextGroupId, v8::Isolate::MessageErrorLevel level, const v8_inspector::StringView& message, const v8_inspector::StringView& url, unsigned lineNumber, unsigned columnNumber, v8_inspector::V8StackTrace*) override; bool isInspectableHeapObject(v8::Local) override; void maxAsyncCallStackDepthChanged(int depth) override; std::unique_ptr resourceNameToUrl( const v8_inspector::StringView& resourceName) override; int64_t generateUniqueId() override; // The isolate gets deleted by its {Dispose} method, not by the default // deleter. Therefore we have to define a custom deleter for the unique_ptr to // call {Dispose}. We have to use the unique_ptr so that the isolate get // disposed in the right order, relative to other member variables. struct IsolateDeleter { void operator()(v8::Isolate* isolate) const { // Exit the isolate after it was entered by ~InspectorIsolateData. isolate->Exit(); isolate->Dispose(); } }; TaskRunner* task_runner_; SetupGlobalTasks setup_global_tasks_; std::unique_ptr array_buffer_allocator_; std::unique_ptr isolate_; // The locker_ field has to come after isolate_ because the locker has to // outlive the isolate. base::Optional locker_; std::unique_ptr inspector_; int last_context_group_id_ = 0; std::map>> contexts_; std::map, v8::Global> modules_; int last_session_id_ = 0; std::map> sessions_; std::map context_group_by_session_; std::set session_ids_for_cleanup_; v8::Global memory_info_; bool current_time_set_ = false; double current_time_ = 0.0; bool log_console_api_message_calls_ = false; bool log_max_async_call_stack_depth_changed_ = false; bool waiting_for_debugger_ = false; v8::Global not_inspectable_private_; v8::Global resource_name_prefix_; v8::Global additional_console_api_; }; // Stores all the channels. // // `InspectorIsolateData` is per isolate and a channel connects // the backend Isolate with the frontend Isolate. The backend registers and // sets up the isolate, but the frontend needs it to send responses and // notifications. This is why we use a separate "class" (just a static wrapper // around std::map). class ChannelHolder { public: static void AddChannel(int session_id, std::unique_ptr channel); static FrontendChannelImpl* GetChannel(int session_id); static void RemoveChannel(int session_id); private: static std::map> channels_; }; } // namespace internal } // namespace v8 #endif // V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_