// Copyright 2013 The Chromium 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 GIN_MODULES_MODULE_REGISTRY_H_ #define GIN_MODULES_MODULE_REGISTRY_H_ #include #include #include #include #include "base/callback.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "gin/gin_export.h" #include "gin/per_context_data.h" namespace gin { struct PendingModule; // This class implements the Asynchronous Module Definition (AMD) API. // https://github.com/amdjs/amdjs-api/wiki/AMD // // Our implementation isn't complete yet. Missing features: // 1) Built-in support for require, exports, and module. // 2) Path resoltuion in module names. // // For these reasons, we don't have an "amd" property on the "define" // function. The spec says we should only add that property once our // implementation complies with the specification. // class GIN_EXPORT ModuleRegistry : public ContextSupplement { public: typedef base::Callback)> LoadModuleCallback; virtual ~ModuleRegistry(); static ModuleRegistry* From(v8::Handle context); static void RegisterGlobals(v8::Isolate* isolate, v8::Handle templ); // The caller must have already entered our context. void AddBuiltinModule(v8::Isolate* isolate, const std::string& id, v8::Handle module); // The caller must have already entered our context. void AddPendingModule(v8::Isolate* isolate, scoped_ptr pending); void LoadModule(v8::Isolate* isolate, const std::string& id, LoadModuleCallback callback); // The caller must have already entered our context. void AttemptToLoadMoreModules(v8::Isolate* isolate); const std::set& available_modules() const { return available_modules_; } const std::set& unsatisfied_dependencies() const { return unsatisfied_dependencies_; } private: typedef ScopedVector PendingModuleVector; typedef std::map LoadModuleCallbackMap; explicit ModuleRegistry(v8::Isolate* isolate); // From ContextSupplement: virtual void Detach(v8::Handle context) OVERRIDE; void Load(v8::Isolate* isolate, scoped_ptr pending); void RegisterModule(v8::Isolate* isolate, const std::string& id, v8::Handle module); bool CheckDependencies(PendingModule* pending); bool AttemptToLoad(v8::Isolate* isolate, scoped_ptr pending); v8::Handle GetModule(v8::Isolate* isolate, const std::string& id); std::set available_modules_; std::set unsatisfied_dependencies_; LoadModuleCallbackMap waiting_callbacks_; PendingModuleVector pending_modules_; v8::Persistent modules_; DISALLOW_COPY_AND_ASSIGN(ModuleRegistry); }; } // namespace gin #endif // GIN_MODULES_MODULE_REGISTRY_H_