11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include "cppgc/platform.h"
111cb0ef41Sopenharmony_ci#include "libplatform/libplatform.h"
121cb0ef41Sopenharmony_ci#include "v8config.h"  // NOLINT(build/include_directory)
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace cppgc {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci/**
171cb0ef41Sopenharmony_ci * Platform provided by cppgc. Uses V8's DefaultPlatform provided by
181cb0ef41Sopenharmony_ci * libplatform internally. Exception: `GetForegroundTaskRunner()`, see below.
191cb0ef41Sopenharmony_ci */
201cb0ef41Sopenharmony_ciclass V8_EXPORT DefaultPlatform : public Platform {
211cb0ef41Sopenharmony_ci public:
221cb0ef41Sopenharmony_ci  using IdleTaskSupport = v8::platform::IdleTaskSupport;
231cb0ef41Sopenharmony_ci  explicit DefaultPlatform(
241cb0ef41Sopenharmony_ci      int thread_pool_size = 0,
251cb0ef41Sopenharmony_ci      IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled,
261cb0ef41Sopenharmony_ci      std::unique_ptr<TracingController> tracing_controller = {})
271cb0ef41Sopenharmony_ci      : v8_platform_(v8::platform::NewDefaultPlatform(
281cb0ef41Sopenharmony_ci            thread_pool_size, idle_task_support,
291cb0ef41Sopenharmony_ci            v8::platform::InProcessStackDumping::kDisabled,
301cb0ef41Sopenharmony_ci            std::move(tracing_controller))) {}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  cppgc::PageAllocator* GetPageAllocator() override {
331cb0ef41Sopenharmony_ci    return v8_platform_->GetPageAllocator();
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  double MonotonicallyIncreasingTime() override {
371cb0ef41Sopenharmony_ci    return v8_platform_->MonotonicallyIncreasingTime();
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  std::shared_ptr<cppgc::TaskRunner> GetForegroundTaskRunner() override {
411cb0ef41Sopenharmony_ci    // V8's default platform creates a new task runner when passed the
421cb0ef41Sopenharmony_ci    // `v8::Isolate` pointer the first time. For non-default platforms this will
431cb0ef41Sopenharmony_ci    // require getting the appropriate task runner.
441cb0ef41Sopenharmony_ci    return v8_platform_->GetForegroundTaskRunner(kNoIsolate);
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  std::unique_ptr<cppgc::JobHandle> PostJob(
481cb0ef41Sopenharmony_ci      cppgc::TaskPriority priority,
491cb0ef41Sopenharmony_ci      std::unique_ptr<cppgc::JobTask> job_task) override {
501cb0ef41Sopenharmony_ci    return v8_platform_->PostJob(priority, std::move(job_task));
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  TracingController* GetTracingController() override {
541cb0ef41Sopenharmony_ci    return v8_platform_->GetTracingController();
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  v8::Platform* GetV8Platform() const { return v8_platform_.get(); }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci protected:
601cb0ef41Sopenharmony_ci  static constexpr v8::Isolate* kNoIsolate = nullptr;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  std::unique_ptr<v8::Platform> v8_platform_;
631cb0ef41Sopenharmony_ci};
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci}  // namespace cppgc
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci#endif  // INCLUDE_CPPGC_DEFAULT_PLATFORM_H_
68