Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(425)

Side by Side Diff: chrome/renderer/extensions/schema_generated_bindings.cc

Issue 9386001: Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: re-jigged to use ModuleSystem Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/renderer/extensions/schema_generated_bindings.h" 5 #include "chrome/renderer/extensions/schema_generated_bindings.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 #include "v8/include/v8.h" 44 #include "v8/include/v8.h"
45 #include "webkit/glue/webkit_glue.h" 45 #include "webkit/glue/webkit_glue.h"
46 46
47 using content::V8ValueConverter; 47 using content::V8ValueConverter;
48 using extensions::ExtensionAPI; 48 using extensions::ExtensionAPI;
49 using WebKit::WebFrame; 49 using WebKit::WebFrame;
50 using WebKit::WebSecurityOrigin; 50 using WebKit::WebSecurityOrigin;
51 51
52 namespace { 52 namespace {
53 53
54 const char* kExtensionDeps[] = {
55 "extensions/event.js",
56 "extensions/json_schema.js",
57 "extensions/miscellaneous_bindings.js",
58 "extensions/apitest.js"
59 };
60
61 // Contains info relevant to a pending API request. 54 // Contains info relevant to a pending API request.
62 struct PendingRequest { 55 struct PendingRequest {
63 public : 56 public :
64 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name, 57 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name,
65 const std::string& extension_id) 58 const std::string& extension_id)
66 : context(context), name(name), extension_id(extension_id) { 59 : context(context), name(name), extension_id(extension_id) {
67 } 60 }
68 v8::Persistent<v8::Context> context; 61 v8::Persistent<v8::Context> context;
69 std::string name; 62 std::string name;
70 std::string extension_id; 63 std::string extension_id;
71 }; 64 };
72 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap; 65 typedef std::map<int, linked_ptr<PendingRequest> > PendingRequestMap;
73 66
74 base::LazyInstance<PendingRequestMap> g_pending_requests = 67 base::LazyInstance<PendingRequestMap> g_pending_requests =
75 LAZY_INSTANCE_INITIALIZER; 68 LAZY_INSTANCE_INITIALIZER;
76 69
77 class ExtensionImpl : public ChromeV8Extension { 70 class ExtensionImpl : public ChromeV8Extension {
78 public: 71 public:
79 explicit ExtensionImpl(ExtensionDispatcher* extension_dispatcher) 72 explicit ExtensionImpl(ExtensionDispatcher* extension_dispatcher)
80 : ChromeV8Extension("extensions/schema_generated_bindings.js", 73 : ChromeV8Extension(extension_dispatcher) {
81 IDR_SCHEMA_GENERATED_BINDINGS_JS, 74 RouteStaticFunction("GetExtensionAPIDefinition",
82 arraysize(kExtensionDeps), 75 &GetExtensionAPIDefinition);
83 kExtensionDeps, 76 RouteStaticFunction("GetNextRequestId", &GetNextRequestId);
84 extension_dispatcher) { 77 RouteStaticFunction("StartRequest", &StartRequest);
78 RouteStaticFunction("SetIconCommon", &SetIconCommon);
85 } 79 }
86 80
87 ~ExtensionImpl() { 81 ~ExtensionImpl() {
88 // TODO(aa): It seems that v8 never deletes us, so this doesn't get called. 82 // TODO(aa): It seems that v8 never deletes us, so this doesn't get called.
89 // Leaving this in here in case v8's implementation ever changes. 83 // Leaving this in here in case v8's implementation ever changes.
90 for (CachedSchemaMap::iterator it = schemas_.begin(); it != schemas_.end(); 84 for (CachedSchemaMap::iterator it = schemas_.begin(); it != schemas_.end();
91 ++it) { 85 ++it) {
92 if (!it->second.IsEmpty()) 86 if (!it->second.IsEmpty())
93 it->second.Dispose(); 87 it->second.Dispose();
94 } 88 }
95 } 89 }
96 90
97 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
98 v8::Handle<v8::String> name) OVERRIDE {
99 if (name->Equals(v8::String::New("GetExtensionAPIDefinition"))) {
100 return v8::FunctionTemplate::New(GetExtensionAPIDefinition,
101 v8::External::New(this));
102 } else if (name->Equals(v8::String::New("GetNextRequestId"))) {
103 return v8::FunctionTemplate::New(GetNextRequestId);
104 } else if (name->Equals(v8::String::New("StartRequest"))) {
105 return v8::FunctionTemplate::New(StartRequest,
106 v8::External::New(this));
107 } else if (name->Equals(v8::String::New("SetIconCommon"))) {
108 return v8::FunctionTemplate::New(SetIconCommon,
109 v8::External::New(this));
110 }
111
112 return ChromeV8Extension::GetNativeFunction(name);
113 }
114
115 private: 91 private:
116 static v8::Handle<v8::Value> GetV8SchemaForAPI( 92 static v8::Handle<v8::Value> GetV8SchemaForAPI(
117 ExtensionImpl* self, 93 ExtensionImpl* self,
118 v8::Handle<v8::Context> context, 94 v8::Handle<v8::Context> context,
119 const std::string& api_name) { 95 const std::string& api_name) {
120 CachedSchemaMap::iterator maybe_api = self->schemas_.find(api_name); 96 CachedSchemaMap::iterator maybe_api = self->schemas_.find(api_name);
121 if (maybe_api != self->schemas_.end()) 97 if (maybe_api != self->schemas_.end())
122 return maybe_api->second; 98 return maybe_api->second;
123 99
124 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create()); 100 scoped_ptr<V8ValueConverter> v8_value_converter(V8ValueConverter::create());
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 // We store this so that we don't have to parse it over and over again for 317 // We store this so that we don't have to parse it over and over again for
342 // every context that uses it. 318 // every context that uses it.
343 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap; 319 typedef std::map<std::string, v8::Persistent<v8::Object> > CachedSchemaMap;
344 CachedSchemaMap schemas_; 320 CachedSchemaMap schemas_;
345 }; 321 };
346 322
347 } // namespace 323 } // namespace
348 324
349 namespace extensions { 325 namespace extensions {
350 326
351 v8::Extension* SchemaGeneratedBindings::Get( 327 ChromeV8Extension* SchemaGeneratedBindings::Get(
352 ExtensionDispatcher* extension_dispatcher) { 328 ExtensionDispatcher* extension_dispatcher) {
353 static v8::Extension* extension = new ExtensionImpl(extension_dispatcher); 329 return new ExtensionImpl(extension_dispatcher);
354 CHECK_EQ(extension_dispatcher,
355 static_cast<ExtensionImpl*>(extension)->extension_dispatcher());
356 return extension;
357 } 330 }
358 331
359 // static 332 // static
360 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts, 333 void SchemaGeneratedBindings::HandleResponse(const ChromeV8ContextSet& contexts,
361 int request_id, 334 int request_id,
362 bool success, 335 bool success,
363 const std::string& response, 336 const std::string& response,
364 const std::string& error, 337 const std::string& error,
365 std::string* extension_id) { 338 std::string* extension_id) {
366 PendingRequestMap::iterator request = 339 PendingRequestMap::iterator request =
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 const std::string& extension_id) { 385 const std::string& extension_id) {
413 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin(); 386 for (PendingRequestMap::const_iterator it = g_pending_requests.Get().begin();
414 it != g_pending_requests.Get().end(); ++it) { 387 it != g_pending_requests.Get().end(); ++it) {
415 if (it->second->extension_id == extension_id) 388 if (it->second->extension_id == extension_id)
416 return true; 389 return true;
417 } 390 }
418 return false; 391 return false;
419 } 392 }
420 393
421 } // namespace 394 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698