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/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 GURL url = renderview->webview()->GetMainFrame()->url(); | 85 GURL url = renderview->webview()->GetMainFrame()->url(); |
86 if (url.SchemeIs(chrome::kExtensionScheme)) | 86 if (url.SchemeIs(chrome::kExtensionScheme)) |
87 return url.host(); | 87 return url.host(); |
88 return std::string(); | 88 return std::string(); |
89 } | 89 } |
90 | 90 |
91 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 91 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
92 v8::Handle<v8::String> name) { | 92 v8::Handle<v8::String> name) { |
93 if (name->Equals(v8::String::New("GetExtensionAPIDefinition"))) { | 93 if (name->Equals(v8::String::New("GetExtensionAPIDefinition"))) { |
94 return v8::FunctionTemplate::New(GetExtensionAPIDefinition); | 94 return v8::FunctionTemplate::New(GetExtensionAPIDefinition); |
95 } else if (name->Equals(v8::String::New("GetViews"))) { | 95 } else if (name->Equals(v8::String::New("GetExtensionViews"))) { |
96 return v8::FunctionTemplate::New(GetViews); | 96 return v8::FunctionTemplate::New(GetExtensionViews); |
97 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { | 97 } else if (name->Equals(v8::String::New("GetNextRequestId"))) { |
98 return v8::FunctionTemplate::New(GetNextRequestId); | 98 return v8::FunctionTemplate::New(GetNextRequestId); |
99 } else if (name->Equals(v8::String::New("OpenChannelToTab"))) { | 99 } else if (name->Equals(v8::String::New("OpenChannelToTab"))) { |
100 return v8::FunctionTemplate::New(OpenChannelToTab); | 100 return v8::FunctionTemplate::New(OpenChannelToTab); |
101 } else if (name->Equals(v8::String::New("GetCurrentPageActions"))) { | 101 } else if (name->Equals(v8::String::New("GetCurrentPageActions"))) { |
102 return v8::FunctionTemplate::New(GetCurrentPageActions); | 102 return v8::FunctionTemplate::New(GetCurrentPageActions); |
103 } else if (name->Equals(v8::String::New("StartRequest"))) { | 103 } else if (name->Equals(v8::String::New("StartRequest"))) { |
104 return v8::FunctionTemplate::New(StartRequest); | 104 return v8::FunctionTemplate::New(StartRequest); |
105 } | 105 } |
106 | 106 |
107 return ExtensionBase::GetNativeFunction(name); | 107 return ExtensionBase::GetNativeFunction(name); |
108 } | 108 } |
109 | 109 |
110 private: | 110 private: |
111 static v8::Handle<v8::Value> GetExtensionAPIDefinition( | 111 static v8::Handle<v8::Value> GetExtensionAPIDefinition( |
112 const v8::Arguments& args) { | 112 const v8::Arguments& args) { |
113 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); | 113 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); |
114 } | 114 } |
115 | 115 |
116 static v8::Handle<v8::Value> GetViews(const v8::Arguments& args) { | 116 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { |
117 std::string extension_id = ExtensionIdForCurrentContext(); | 117 if (args.Length() != 2) |
| 118 return v8::Undefined(); |
118 | 119 |
119 ContextList contexts = | 120 if (!args[0]->IsInt32() || !args[1]->IsString()) |
120 bindings_utils::GetContextsForExtension(extension_id); | 121 return v8::Undefined(); |
121 DCHECK(contexts.size() > 0); | |
122 | 122 |
123 v8::Local<v8::Array> views = v8::Array::New(contexts.size()); | 123 // |browser_window_id| == -1 means getting views attached to any browser |
| 124 // window. |
| 125 int browser_window_id = args[0]->Int32Value(); |
| 126 |
| 127 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); |
| 128 // |view_type| == ViewType::INVALID means getting any type of views. |
| 129 ViewType::Type view_type = ViewType::INVALID; |
| 130 if (view_type_string == "TOOLSTRIP") { |
| 131 view_type = ViewType::EXTENSION_TOOLSTRIP; |
| 132 } else if (view_type_string == "BACKGROUND") { |
| 133 view_type = ViewType::EXTENSION_BACKGROUND_PAGE; |
| 134 } else if (view_type_string == "TAB") { |
| 135 view_type = ViewType::TAB_CONTENTS; |
| 136 } else if (view_type_string != "ALL") { |
| 137 return v8::Undefined(); |
| 138 } |
| 139 |
| 140 v8::Local<v8::Array> views = v8::Array::New(); |
124 int index = 0; | 141 int index = 0; |
125 ContextList::const_iterator it = contexts.begin(); | 142 RenderView::RenderViewSet* render_view_set_pointer = |
126 for (; it != contexts.end(); ++it) { | 143 Singleton<RenderView::RenderViewSet>::get(); |
127 v8::Local<v8::Value> window = (*it)->context->Global()->Get( | 144 DCHECK(render_view_set_pointer->render_view_set_.size() > 0); |
128 v8::String::New("window")); | 145 |
129 DCHECK(!window.IsEmpty()); | 146 v8::Local<v8::Value> window; |
130 views->Set(v8::Integer::New(index), window); | 147 std::string current_extension_id = ExtensionIdForCurrentContext(); |
131 index++; | 148 std::set<RenderView* >::iterator it = |
| 149 render_view_set_pointer->render_view_set_.begin(); |
| 150 for (; it != render_view_set_pointer->render_view_set_.end(); ++it) { |
| 151 if (view_type != ViewType::INVALID && (*it)->view_type() != view_type) |
| 152 continue; |
| 153 |
| 154 GURL url = (*it)->webview()->GetMainFrame()->url(); |
| 155 if (!url.SchemeIs(chrome::kExtensionScheme)) |
| 156 continue; |
| 157 std::string extension_id = url.host(); |
| 158 if (extension_id != current_extension_id) |
| 159 continue; |
| 160 |
| 161 if (browser_window_id != -1 && |
| 162 (*it)->browser_window_id() != browser_window_id) { |
| 163 continue; |
| 164 } |
| 165 |
| 166 v8::Local<v8::Context> context = |
| 167 (*it)->webview()->GetMainFrame()->mainWorldScriptContext(); |
| 168 if (!context.IsEmpty()) { |
| 169 v8::Local<v8::Value> window = context->Global(); |
| 170 DCHECK(!window.IsEmpty()); |
| 171 views->Set(v8::Integer::New(index), window); |
| 172 index++; |
| 173 if (view_type == ViewType::EXTENSION_BACKGROUND_PAGE) |
| 174 break; |
| 175 } |
132 } | 176 } |
133 return views; | 177 return views; |
134 } | 178 } |
135 | 179 |
136 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { | 180 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { |
137 static int next_request_id = 0; | 181 static int next_request_id = 0; |
138 return v8::Integer::New(next_request_id++); | 182 return v8::Integer::New(next_request_id++); |
139 } | 183 } |
140 | 184 |
141 // Creates a new messaging channel to the tab with the given ID. | 185 // Creates a new messaging channel to the tab with the given ID. |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 381 |
338 v8::Local<v8::Function> function = | 382 v8::Local<v8::Function> function = |
339 v8::Local<v8::Function>::Cast(console_error); | 383 v8::Local<v8::Function>::Cast(console_error); |
340 v8::Local<v8::Value> argv[] = { v8::String::New(error_msg.c_str()) }; | 384 v8::Local<v8::Value> argv[] = { v8::String::New(error_msg.c_str()) }; |
341 if (!function.IsEmpty()) | 385 if (!function.IsEmpty()) |
342 function->Call(console->ToObject(), arraysize(argv), argv); | 386 function->Call(console->ToObject(), arraysize(argv), argv); |
343 | 387 |
344 return v8::Undefined(); | 388 return v8::Undefined(); |
345 #endif | 389 #endif |
346 } | 390 } |
OLD | NEW |