|
OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/extension_process_bindings.h" | 5 #include "chrome/renderer/extensions/extension_process_bindings.h" |
6 | 6 |
7 #include "base/singleton.h" | 7 #include "base/singleton.h" |
8 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
9 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
10 #include "chrome/renderer/extensions/bindings_utils.h" | 10 #include "chrome/renderer/extensions/bindings_utils.h" |
11 #include "chrome/renderer/extensions/event_bindings.h" | 11 #include "chrome/renderer/extensions/event_bindings.h" |
12 #include "chrome/renderer/extensions/renderer_extension_bindings.h" | 12 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
13 #include "chrome/renderer/js_only_v8_extensions.h" | 13 #include "chrome/renderer/js_only_v8_extensions.h" |
14 #include "chrome/renderer/render_view.h" | 14 #include "chrome/renderer/render_view.h" |
15 #include "grit/renderer_resources.h" | 15 #include "grit/renderer_resources.h" |
16 #include "webkit/glue/webframe.h" | 16 #include "webkit/glue/webframe.h" |
17 | 17 |
18 using bindings_utils::GetStringResource; | 18 using bindings_utils::GetStringResource; |
19 using bindings_utils::ContextInfo; | 19 using bindings_utils::ContextInfo; |
20 using bindings_utils::ContextList; | 20 using bindings_utils::ContextList; |
21 using bindings_utils::GetContexts; | 21 using bindings_utils::GetContexts; |
22 using bindings_utils::GetPendingRequestMap; | 22 using bindings_utils::GetPendingRequestMap; |
23 using bindings_utils::PendingRequest; | 23 using bindings_utils::PendingRequest; |
24 using bindings_utils::ExtensionBase; | 24 using bindings_utils::ExtensionBase; |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 // A map of extension ID to vector of page action ids. | |
29 typedef std::map< std::string, std::vector<std::string> > PageActionIdMap; | |
30 | |
28 const char kExtensionName[] = "chrome/ExtensionProcessBindings"; | 31 const char kExtensionName[] = "chrome/ExtensionProcessBindings"; |
29 const char* kExtensionDeps[] = { | 32 const char* kExtensionDeps[] = { |
30 BaseJsV8Extension::kName, | 33 BaseJsV8Extension::kName, |
31 EventBindings::kName, | 34 EventBindings::kName, |
32 JsonSchemaJsV8Extension::kName, | 35 JsonSchemaJsV8Extension::kName, |
33 RendererExtensionBindings::kName, | 36 RendererExtensionBindings::kName, |
34 }; | 37 }; |
35 | 38 |
36 struct SingletonData { | 39 struct SingletonData { |
37 std::set<std::string> function_names_; | 40 std::set<std::string> function_names_; |
41 PageActionIdMap page_action_ids_; | |
38 }; | 42 }; |
39 | 43 |
40 static std::set<std::string>* GetFunctionNameSet() { | 44 static std::set<std::string>* GetFunctionNameSet() { |
41 return &Singleton<SingletonData>()->function_names_; | 45 return &Singleton<SingletonData>()->function_names_; |
42 } | 46 } |
43 | 47 |
48 static PageActionIdMap* GetPageActionMap() { | |
49 return &Singleton<SingletonData>()->page_action_ids_; | |
50 } | |
51 | |
44 class ExtensionImpl : public ExtensionBase { | 52 class ExtensionImpl : public ExtensionBase { |
45 public: | 53 public: |
46 ExtensionImpl() : ExtensionBase( | 54 ExtensionImpl() : ExtensionBase( |
47 kExtensionName, GetStringResource<IDR_EXTENSION_PROCESS_BINDINGS_JS>(), | 55 kExtensionName, GetStringResource<IDR_EXTENSION_PROCESS_BINDINGS_JS>(), |
48 arraysize(kExtensionDeps), kExtensionDeps) {} | 56 arraysize(kExtensionDeps), kExtensionDeps) {} |
49 | 57 |
50 static void SetFunctionNames(const std::vector<std::string>& names) { | 58 static void SetFunctionNames(const std::vector<std::string>& names) { |
51 std::set<std::string>* name_set = GetFunctionNameSet(); | 59 std::set<std::string>* name_set = GetFunctionNameSet(); |
52 for (size_t i = 0; i < names.size(); ++i) { | 60 for (size_t i = 0; i < names.size(); ++i) { |
53 name_set->insert(names[i]); | 61 name_set->insert(names[i]); |
54 } | 62 } |
55 } | 63 } |
56 | 64 |
57 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 65 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
58 v8::Handle<v8::String> name) { | 66 v8::Handle<v8::String> name) { |
59 std::set<std::string>* names = GetFunctionNameSet(); | 67 std::set<std::string>* names = GetFunctionNameSet(); |
60 | 68 |
61 if (name->Equals(v8::String::New("GetViews"))) { | 69 if (name->Equals(v8::String::New("GetViews"))) { |
62 return v8::FunctionTemplate::New(GetViews); | 70 return v8::FunctionTemplate::New(GetViews); |
63 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { | 71 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { |
64 return v8::FunctionTemplate::New(GetNextRequestId); | 72 return v8::FunctionTemplate::New(GetNextRequestId); |
73 } else if (name->Equals(v8::String::New("GetCurrentPageActions"))) { | |
74 return v8::FunctionTemplate::New(GetCurrentPageActions); | |
65 } else if (names->find(*v8::String::AsciiValue(name)) != names->end()) { | 75 } else if (names->find(*v8::String::AsciiValue(name)) != names->end()) { |
66 return v8::FunctionTemplate::New(StartRequest, name); | 76 return v8::FunctionTemplate::New(StartRequest, name); |
67 } | 77 } |
68 | 78 |
69 return ExtensionBase::GetNativeFunction(name); | 79 return ExtensionBase::GetNativeFunction(name); |
70 } | 80 } |
71 | 81 |
72 private: | 82 private: |
73 static v8::Handle<v8::Value> GetViews(const v8::Arguments& args) { | 83 static std::string ExtensionIdFromCurrentContext() { |
74 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); | 84 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
75 DCHECK(renderview); | 85 DCHECK(renderview); |
76 GURL url = renderview->webview()->GetMainFrame()->GetURL(); | 86 GURL url = renderview->webview()->GetMainFrame()->GetURL(); |
77 std::string extension_id = url.host(); | 87 return url.host(); |
88 } | |
89 | |
90 static v8::Handle<v8::Value> GetViews(const v8::Arguments& args) { | |
91 std::string extension_id = ExtensionIdFromCurrentContext(); | |
78 | 92 |
79 ContextList contexts = | 93 ContextList contexts = |
80 bindings_utils::GetContextsForExtension(extension_id); | 94 bindings_utils::GetContextsForExtension(extension_id); |
81 DCHECK(contexts.size() > 0); | 95 DCHECK(contexts.size() > 0); |
82 | 96 |
83 v8::Local<v8::Array> views = v8::Array::New(contexts.size()); | 97 v8::Local<v8::Array> views = v8::Array::New(contexts.size()); |
84 int index = 0; | 98 int index = 0; |
85 ContextList::const_iterator it = contexts.begin(); | 99 ContextList::const_iterator it = contexts.begin(); |
86 for (; it != contexts.end(); ++it) { | 100 for (; it != contexts.end(); ++it) { |
87 v8::Local<v8::Value> window = (*it)->context->Global()->Get( | 101 v8::Local<v8::Value> window = (*it)->context->Global()->Get( |
88 v8::String::New("window")); | 102 v8::String::New("window")); |
89 DCHECK(!window.IsEmpty()); | 103 DCHECK(!window.IsEmpty()); |
90 views->Set(v8::Integer::New(index), window); | 104 views->Set(v8::Integer::New(index), window); |
91 index++; | 105 index++; |
92 } | 106 } |
93 return views; | 107 return views; |
94 } | 108 } |
95 | 109 |
96 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { | 110 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { |
97 static int next_request_id = 0; | 111 static int next_request_id = 0; |
98 return v8::Integer::New(next_request_id++); | 112 return v8::Integer::New(next_request_id++); |
99 } | 113 } |
100 | 114 |
115 static v8::Handle<v8::Value> GetCurrentPageActions( | |
116 const v8::Arguments& args) { | |
117 std::string extension_id = ExtensionIdFromCurrentContext(); | |
118 PageActionIdMap* page_action_map = | |
119 GetPageActionMap(); | |
Matt Perry
2009/07/14 21:17:48
nit: no need to wrap this
| |
120 PageActionIdMap::const_iterator it = | |
121 page_action_map->find(extension_id); | |
122 | |
123 std::vector<std::string> page_actions; | |
Matt Perry
2009/07/14 21:17:48
This creates a copy of the vector. Use a pointer
| |
124 size_t size = 0; | |
125 if (it != page_action_map->end()) { | |
126 page_actions = it->second; | |
127 size = page_actions.size(); | |
128 } | |
129 | |
130 v8::Local<v8::Array> page_action_vector = v8::Array::New(size); | |
Matt Perry
2009/07/14 21:17:48
nit: "vector" is lying - it's an array
| |
131 for (size_t i = 0; i < size; ++i) { | |
132 std::string page_action_id = page_actions[i]; | |
Matt Perry
2009/07/14 21:17:48
nit: wasteful copy. use const ref
| |
133 page_action_vector->Set(v8::Integer::New(i), | |
134 v8::String::New(page_action_id.c_str())); | |
135 } | |
136 | |
137 return page_action_vector; | |
138 } | |
139 | |
101 // Starts an API request to the browser, with an optional callback. The | 140 // Starts an API request to the browser, with an optional callback. The |
102 // callback will be dispatched to EventBindings::HandleResponse. | 141 // callback will be dispatched to EventBindings::HandleResponse. |
103 static v8::Handle<v8::Value> StartRequest(const v8::Arguments& args) { | 142 static v8::Handle<v8::Value> StartRequest(const v8::Arguments& args) { |
104 // Get the current RenderView so that we can send a routed IPC message from | 143 // Get the current RenderView so that we can send a routed IPC message from |
105 // the correct source. | 144 // the correct source. |
106 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); | 145 RenderView* renderview = bindings_utils::GetRenderViewForCurrentContext(); |
107 if (!renderview) | 146 if (!renderview) |
108 return v8::Undefined(); | 147 return v8::Undefined(); |
109 | 148 |
110 if (args.Length() != 3 || !args[0]->IsString() || !args[1]->IsInt32() || | 149 if (args.Length() != 3 || !args[0]->IsString() || !args[1]->IsInt32() || |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 argv[0] = v8::Integer::New(request_id); | 191 argv[0] = v8::Integer::New(request_id); |
153 argv[1] = v8::String::New(request->name.c_str()); | 192 argv[1] = v8::String::New(request->name.c_str()); |
154 argv[2] = v8::Boolean::New(success); | 193 argv[2] = v8::Boolean::New(success); |
155 argv[3] = v8::String::New(response.c_str()); | 194 argv[3] = v8::String::New(response.c_str()); |
156 argv[4] = v8::String::New(error.c_str()); | 195 argv[4] = v8::String::New(error.c_str()); |
157 bindings_utils::CallFunctionInContext( | 196 bindings_utils::CallFunctionInContext( |
158 request->context, "handleResponse", arraysize(argv), argv); | 197 request->context, "handleResponse", arraysize(argv), argv); |
159 | 198 |
160 GetPendingRequestMap().erase(request_id); | 199 GetPendingRequestMap().erase(request_id); |
161 } | 200 } |
201 | |
202 // static | |
203 void ExtensionProcessBindings::SetPageActions( | |
204 const std::string& extension_id, | |
205 const std::vector<std::string>& page_actions) { | |
206 PageActionIdMap& page_action_map = *GetPageActionMap(); | |
207 if (!page_actions.empty()) { | |
208 page_action_map[extension_id] = page_actions; | |
209 } else { | |
210 if (page_action_map.find(extension_id) != page_action_map.end()) | |
211 page_action_map.erase(extension_id); | |
212 } | |
213 } | |
OLD | NEW |