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

Side by Side Diff: chrome/renderer/module_system.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, 9 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/module_system.h" 5 #include "chrome/renderer/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/renderer/static_v8_external_string_resource.h"
9 #include "grit/renderer_resources.h" 8 #include "grit/renderer_resources.h"
10 #include "ui/base/resource/resource_bundle.h" 9 #include "ui/base/resource/resource_bundle.h"
11 10
12 namespace { 11 namespace {
13 12
14 v8::Handle<v8::String> GetResource(int resourceId) {
15 const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
16 return v8::String::NewExternal(new StaticV8ExternalAsciiStringResource(
17 resource_bundle.GetRawDataResource(resourceId)));
18 }
19
20 } // namespace 13 } // namespace
21 14
22 ModuleSystem::ModuleSystem(const std::map<std::string, std::string>* source_map) 15 ModuleSystem::ModuleSystem(
16 const std::map<std::string, base::StringPiece>* source_map)
23 : source_map_(source_map) { 17 : source_map_(source_map) {
24 RouteFunction("Run", 18 RouteFunction("Run",
25 base::Bind(&ModuleSystem::Run, base::Unretained(this))); 19 base::Bind(&ModuleSystem::Run, base::Unretained(this)));
26 RouteFunction("GetSource", 20 RouteFunction("GetSource",
27 base::Bind(&ModuleSystem::GetSource, base::Unretained(this))); 21 base::Bind(&ModuleSystem::GetSource, base::Unretained(this)));
28 RouteFunction("GetNative", 22 RouteFunction("GetNative",
29 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); 23 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
30 } 24 }
31 25
32 ModuleSystem::~ModuleSystem() { 26 ModuleSystem::~ModuleSystem() {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 require_ = v8::Persistent<v8::Function>::New( 60 require_ = v8::Persistent<v8::Function>::New(
67 v8::Handle<v8::Function>::Cast(require)); 61 v8::Handle<v8::Function>::Cast(require));
68 } 62 }
69 63
70 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code) { 64 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code) {
71 v8::HandleScope handle_scope; 65 v8::HandleScope handle_scope;
72 return handle_scope.Close(v8::Script::New(code)->Run()); 66 return handle_scope.Close(v8::Script::New(code)->Run());
73 } 67 }
74 68
75 v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) { 69 v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) {
76 CHECK_EQ(1, args.Length()); 70 CHECK_EQ(2, args.Length());
77 v8::HandleScope handle_scope; 71 v8::HandleScope handle_scope;
78 return handle_scope.Close(v8::Script::New(args[0]->ToString())->Run()); 72 return handle_scope.Close(
73 v8::Script::New(args[0]->ToString(), args[1])->Run());
79 } 74 }
80 75
81 v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) { 76 v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) {
82 CHECK_EQ(1, args.Length()); 77 CHECK_EQ(1, args.Length());
83 std::string module_name = *v8::String::AsciiValue(args[0]->ToString()); 78 std::string module_name = *v8::String::AsciiValue(args[0]->ToString());
84 std::map<std::string, std::string>::const_iterator p = 79 std::map<std::string, base::StringPiece>::const_iterator p =
85 source_map_->find(module_name); 80 source_map_->find(module_name);
86 if (p == source_map_->end()) 81 if (p == source_map_->end())
87 return v8::Undefined(); 82 return v8::Undefined();
88 return v8::String::New(p->second.c_str()); 83 return ConvertString(p->second);
89 } 84 }
90 85
91 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { 86 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
92 CHECK_EQ(1, args.Length()); 87 CHECK_EQ(1, args.Length());
93 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); 88 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
94 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 89 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
95 if (i == native_handler_map_.end()) 90 if (i == native_handler_map_.end())
96 return v8::Undefined(); 91 return v8::Undefined();
97 return i->second->NewInstance(); 92 return i->second->NewInstance();
98 } 93 }
94
95 v8::Handle<v8::String> ModuleSystem::ConvertString(
96 const base::StringPiece& string) {
97 linked_ptr<StaticV8ExternalAsciiStringResource> external_string(
98 new StaticV8ExternalAsciiStringResource(string));
99 external_strings_.push_back(external_string);
100 return v8::String::NewExternal(external_string.get());
101 }
102
103 v8::Handle<v8::String> ModuleSystem::GetResource(int resourceId) {
104 const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
105 return ConvertString(resource_bundle.GetRawDataResource(resourceId));
106 }
107
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698