Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_MODULES_MODULE_REGISTRY_H_ | 5 #ifndef GIN_MODULES_MODULE_REGISTRY_H_ |
| 6 #define GIN_MODULES_MODULE_REGISTRY_H_ | 6 #define GIN_MODULES_MODULE_REGISTRY_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | |
| 9 #include <set> | 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 | 12 |
| 13 #include "base/callback.h" | |
| 12 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "gin/per_context_data.h" | 16 #include "gin/per_context_data.h" |
| 15 | 17 |
| 16 namespace gin { | 18 namespace gin { |
| 17 | 19 |
| 18 struct PendingModule; | 20 struct PendingModule; |
| 19 | 21 |
| 22 typedef base::Callback<void (v8::Handle<v8::Value>)> ModuleLoadCallback; | |
|
Aaron Boodman
2013/11/27 19:02:41
Does this need to be namespace scoped?
Aaron Boodman
2013/11/27 19:02:41
Nit: The general style in Chrome is for non-primit
Aaron Boodman
2013/11/27 19:02:41
Nit: LoadModuleCallback, for parallelism with Load
abarth-chromium
2013/11/27 19:18:09
Done.
abarth-chromium
2013/11/27 19:18:09
v8::Handles are just pointers to pointers. Having
abarth-chromium
2013/11/27 19:18:09
Done.
| |
| 23 | |
| 20 // This class implements the Asynchronous Module Definition (AMD) API. | 24 // This class implements the Asynchronous Module Definition (AMD) API. |
| 21 // https://github.com/amdjs/amdjs-api/wiki/AMD | 25 // https://github.com/amdjs/amdjs-api/wiki/AMD |
| 22 // | 26 // |
| 23 // Our implementation isn't complete yet. Missing features: | 27 // Our implementation isn't complete yet. Missing features: |
| 24 // 1) Built-in support for require, exports, and module. | 28 // 1) Built-in support for require, exports, and module. |
| 25 // 2) Path resoltuion in module names. | 29 // 2) Path resoltuion in module names. |
| 26 // | 30 // |
| 27 // For these reasons, we don't have an "amd" property on the "define" | 31 // For these reasons, we don't have an "amd" property on the "define" |
| 28 // function. The spec says we should only add that property once our | 32 // function. The spec says we should only add that property once our |
| 29 // implementation complies with the specification. | 33 // implementation complies with the specification. |
| 30 // | 34 // |
| 31 class ModuleRegistry : public ContextSupplement { | 35 class ModuleRegistry : public ContextSupplement { |
| 32 public: | 36 public: |
| 33 virtual ~ModuleRegistry(); | 37 virtual ~ModuleRegistry(); |
| 34 | 38 |
| 35 static ModuleRegistry* From(v8::Handle<v8::Context> context); | 39 static ModuleRegistry* From(v8::Handle<v8::Context> context); |
| 36 | 40 |
| 37 static void RegisterGlobals(v8::Isolate* isolate, | 41 static void RegisterGlobals(v8::Isolate* isolate, |
| 38 v8::Handle<v8::ObjectTemplate> templ); | 42 v8::Handle<v8::ObjectTemplate> templ); |
| 39 | 43 |
| 40 // The caller must have already entered our context. | 44 // The caller must have already entered our context. |
| 41 void AddBuiltinModule(v8::Isolate* isolate, | 45 void AddBuiltinModule(v8::Isolate* isolate, |
| 42 const std::string& id, | 46 const std::string& id, |
| 43 v8::Handle<v8::ObjectTemplate> templ); | 47 v8::Handle<v8::ObjectTemplate> templ); |
| 44 | 48 |
| 45 // The caller must have already entered our context. | 49 // The caller must have already entered our context. |
| 46 void AddPendingModule(v8::Isolate* isolate, | 50 void AddPendingModule(v8::Isolate* isolate, |
| 47 scoped_ptr<PendingModule> pending); | 51 scoped_ptr<PendingModule> pending); |
| 48 | 52 |
| 53 void LoadModule(v8::Isolate* isolate, | |
| 54 const std::string& id, | |
| 55 ModuleLoadCallback callback); | |
| 56 | |
| 49 // The caller must have already entered our context. | 57 // The caller must have already entered our context. |
| 50 void AttemptToLoadMoreModules(v8::Isolate* isolate); | 58 void AttemptToLoadMoreModules(v8::Isolate* isolate); |
| 51 | 59 |
| 52 const std::set<std::string>& available_modules() const { | 60 const std::set<std::string>& available_modules() const { |
| 53 return available_modules_; | 61 return available_modules_; |
| 54 } | 62 } |
| 55 | 63 |
| 56 const std::set<std::string>& unsatisfied_dependencies() const { | 64 const std::set<std::string>& unsatisfied_dependencies() const { |
| 57 return unsatisfied_dependencies_; | 65 return unsatisfied_dependencies_; |
| 58 } | 66 } |
| 59 | 67 |
| 60 private: | 68 private: |
| 61 typedef ScopedVector<PendingModule> PendingModuleVector; | 69 typedef ScopedVector<PendingModule> PendingModuleVector; |
| 70 typedef std::map<std::string, ModuleLoadCallback> ModuleLoadCallbackMap; | |
| 62 | 71 |
| 63 explicit ModuleRegistry(v8::Isolate* isolate); | 72 explicit ModuleRegistry(v8::Isolate* isolate); |
| 64 | 73 |
| 65 // From ContextSupplement: | 74 // From ContextSupplement: |
| 66 virtual void Detach(v8::Handle<v8::Context> context) OVERRIDE; | 75 virtual void Detach(v8::Handle<v8::Context> context) OVERRIDE; |
| 67 | 76 |
| 68 void Load(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); | 77 void Load(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); |
| 69 void RegisterModule(v8::Isolate* isolate, | 78 void RegisterModule(v8::Isolate* isolate, |
| 70 const std::string& id, | 79 const std::string& id, |
| 71 v8::Handle<v8::Value> module); | 80 v8::Handle<v8::Value> module); |
| 72 | 81 |
| 73 bool CheckDependencies(PendingModule* pending); | 82 bool CheckDependencies(PendingModule* pending); |
| 74 bool AttemptToLoad(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); | 83 bool AttemptToLoad(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); |
| 75 | 84 |
| 85 v8::Handle<v8::Value> GetModule(v8::Isolate* isolate, const std::string& id); | |
| 86 | |
| 76 std::set<std::string> available_modules_; | 87 std::set<std::string> available_modules_; |
| 77 std::set<std::string> unsatisfied_dependencies_; | 88 std::set<std::string> unsatisfied_dependencies_; |
| 78 | 89 |
| 90 ModuleLoadCallbackMap waiting_callbacks_; | |
| 91 | |
| 79 PendingModuleVector pending_modules_; | 92 PendingModuleVector pending_modules_; |
| 80 v8::Persistent<v8::Object> modules_; | 93 v8::Persistent<v8::Object> modules_; |
| 81 | 94 |
| 82 DISALLOW_COPY_AND_ASSIGN(ModuleRegistry); | 95 DISALLOW_COPY_AND_ASSIGN(ModuleRegistry); |
| 83 }; | 96 }; |
| 84 | 97 |
| 85 } // namespace gin | 98 } // namespace gin |
| 86 | 99 |
| 87 #endif // GIN_MODULES_MODULE_REGISTRY_H_ | 100 #endif // GIN_MODULES_MODULE_REGISTRY_H_ |
| OLD | NEW |