| 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 <map> | 5 #include <map> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "chrome/renderer/extensions/extension_process_bindings.h" | 10 #include "chrome/renderer/extensions/extension_process_bindings.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 static L10nMessagesMap* GetL10nMessagesMap(const std::string extension_id) { | 91 static L10nMessagesMap* GetL10nMessagesMap(const std::string extension_id) { |
| 92 ExtensionToL10nMessagesMap::iterator it = | 92 ExtensionToL10nMessagesMap::iterator it = |
| 93 Singleton<SingletonData>()->extension_l10n_messages_map_.find(extension_id); | 93 Singleton<SingletonData>()->extension_l10n_messages_map_.find(extension_id); |
| 94 if (it != Singleton<SingletonData>()->extension_l10n_messages_map_.end()) { | 94 if (it != Singleton<SingletonData>()->extension_l10n_messages_map_.end()) { |
| 95 return &(it->second); | 95 return &(it->second); |
| 96 } else { | 96 } else { |
| 97 return NULL; | 97 return NULL; |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Used to accumulate the list of views associated with an extension. |
| 102 class ExtensionViewAccumulator : public RenderViewVisitor { |
| 103 public: |
| 104 ExtensionViewAccumulator(const std::string& extension_id, |
| 105 int browser_window_id, |
| 106 ViewType::Type view_type) |
| 107 : extension_id_(extension_id), |
| 108 browser_window_id_(browser_window_id), |
| 109 view_type_(view_type), |
| 110 views_(v8::Array::New()), |
| 111 index_(0) { |
| 112 } |
| 113 |
| 114 virtual bool Visit(RenderView* render_view) { |
| 115 if (!ViewTypeMatches(render_view->view_type(), view_type_)) |
| 116 return true; |
| 117 |
| 118 GURL url = render_view->webview()->mainFrame()->url(); |
| 119 if (!url.SchemeIs(chrome::kExtensionScheme)) |
| 120 return true; |
| 121 const std::string& extension_id = url.host(); |
| 122 if (extension_id != extension_id_) |
| 123 return true; |
| 124 |
| 125 if (browser_window_id_ != -1 && |
| 126 render_view->browser_window_id() != browser_window_id_) |
| 127 return true; |
| 128 |
| 129 v8::Local<v8::Context> context = |
| 130 render_view->webview()->mainFrame()->mainWorldScriptContext(); |
| 131 if (!context.IsEmpty()) { |
| 132 v8::Local<v8::Value> window = context->Global(); |
| 133 DCHECK(!window.IsEmpty()); |
| 134 views_->Set(v8::Integer::New(index_), window); |
| 135 index_++; |
| 136 if (view_type_ == ViewType::EXTENSION_BACKGROUND_PAGE) |
| 137 return false; // There can be only one... |
| 138 } |
| 139 return true; |
| 140 } |
| 141 |
| 142 v8::Local<v8::Array> views() { return views_; } |
| 143 |
| 144 private: |
| 145 // Returns true is |type| "isa" |match|. |
| 146 static bool ViewTypeMatches(ViewType::Type type, ViewType::Type match) { |
| 147 if (type == match) |
| 148 return true; |
| 149 |
| 150 // INVALID means match all. |
| 151 if (match == ViewType::INVALID) |
| 152 return true; |
| 153 |
| 154 // TODO(erikkay) for now, special case mole as a type of toolstrip. |
| 155 // Perhaps this isn't the right long-term thing to do. |
| 156 if (match == ViewType::EXTENSION_TOOLSTRIP && |
| 157 type == ViewType::EXTENSION_MOLE) { |
| 158 return true; |
| 159 } |
| 160 |
| 161 return false; |
| 162 } |
| 163 |
| 164 std::string extension_id_; |
| 165 int browser_window_id_; |
| 166 ViewType::Type view_type_; |
| 167 v8::Local<v8::Array> views_; |
| 168 int index_; |
| 169 }; |
| 170 |
| 101 class ExtensionImpl : public ExtensionBase { | 171 class ExtensionImpl : public ExtensionBase { |
| 102 public: | 172 public: |
| 103 ExtensionImpl() : ExtensionBase( | 173 ExtensionImpl() : ExtensionBase( |
| 104 kExtensionName, GetStringResource<IDR_EXTENSION_PROCESS_BINDINGS_JS>(), | 174 kExtensionName, GetStringResource<IDR_EXTENSION_PROCESS_BINDINGS_JS>(), |
| 105 arraysize(kExtensionDeps), kExtensionDeps) {} | 175 arraysize(kExtensionDeps), kExtensionDeps) {} |
| 106 | 176 |
| 107 static void SetFunctionNames(const std::vector<std::string>& names) { | 177 static void SetFunctionNames(const std::vector<std::string>& names) { |
| 108 std::set<std::string>* name_set = GetFunctionNameSet(); | 178 std::set<std::string>* name_set = GetFunctionNameSet(); |
| 109 for (size_t i = 0; i < names.size(); ++i) { | 179 for (size_t i = 0; i < names.size(); ++i) { |
| 110 name_set->insert(names[i]); | 180 name_set->insert(names[i]); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 217 |
| 148 return ExtensionBase::GetNativeFunction(name); | 218 return ExtensionBase::GetNativeFunction(name); |
| 149 } | 219 } |
| 150 | 220 |
| 151 private: | 221 private: |
| 152 static v8::Handle<v8::Value> GetExtensionAPIDefinition( | 222 static v8::Handle<v8::Value> GetExtensionAPIDefinition( |
| 153 const v8::Arguments& args) { | 223 const v8::Arguments& args) { |
| 154 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); | 224 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); |
| 155 } | 225 } |
| 156 | 226 |
| 157 // Returns true is |type| "isa" |match|. | |
| 158 static bool ViewTypeMatches(ViewType::Type type, ViewType::Type match) { | |
| 159 if (type == match) | |
| 160 return true; | |
| 161 | |
| 162 // INVALID means match all. | |
| 163 if (match == ViewType::INVALID) | |
| 164 return true; | |
| 165 | |
| 166 // TODO(erikkay) for now, special case mole as a type of toolstrip. | |
| 167 // Perhaps this isn't the right long-term thing to do. | |
| 168 if (match == ViewType::EXTENSION_TOOLSTRIP && | |
| 169 type == ViewType::EXTENSION_MOLE) { | |
| 170 return true; | |
| 171 } | |
| 172 | |
| 173 return false; | |
| 174 } | |
| 175 | |
| 176 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { | 227 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { |
| 177 if (args.Length() != 2) | 228 if (args.Length() != 2) |
| 178 return v8::Undefined(); | 229 return v8::Undefined(); |
| 179 | 230 |
| 180 if (!args[0]->IsInt32() || !args[1]->IsString()) | 231 if (!args[0]->IsInt32() || !args[1]->IsString()) |
| 181 return v8::Undefined(); | 232 return v8::Undefined(); |
| 182 | 233 |
| 183 // |browser_window_id| == -1 means getting views attached to any browser | 234 // |browser_window_id| == -1 means getting views attached to any browser |
| 184 // window. | 235 // window. |
| 185 int browser_window_id = args[0]->Int32Value(); | 236 int browser_window_id = args[0]->Int32Value(); |
| 186 | 237 |
| 187 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); | 238 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); |
| 188 // |view_type| == ViewType::INVALID means getting any type of views. | 239 // |view_type| == ViewType::INVALID means getting any type of views. |
| 189 ViewType::Type view_type = ViewType::INVALID; | 240 ViewType::Type view_type = ViewType::INVALID; |
| 190 if (view_type_string == "TOOLSTRIP") { | 241 if (view_type_string == "TOOLSTRIP") { |
| 191 view_type = ViewType::EXTENSION_TOOLSTRIP; | 242 view_type = ViewType::EXTENSION_TOOLSTRIP; |
| 192 } else if (view_type_string == "MOLE") { | 243 } else if (view_type_string == "MOLE") { |
| 193 view_type = ViewType::EXTENSION_MOLE; | 244 view_type = ViewType::EXTENSION_MOLE; |
| 194 } else if (view_type_string == "BACKGROUND") { | 245 } else if (view_type_string == "BACKGROUND") { |
| 195 view_type = ViewType::EXTENSION_BACKGROUND_PAGE; | 246 view_type = ViewType::EXTENSION_BACKGROUND_PAGE; |
| 196 } else if (view_type_string == "TAB") { | 247 } else if (view_type_string == "TAB") { |
| 197 view_type = ViewType::TAB_CONTENTS; | 248 view_type = ViewType::TAB_CONTENTS; |
| 198 } else if (view_type_string != "ALL") { | 249 } else if (view_type_string != "ALL") { |
| 199 return v8::Undefined(); | 250 return v8::Undefined(); |
| 200 } | 251 } |
| 201 | 252 |
| 202 v8::Local<v8::Array> views = v8::Array::New(); | 253 ExtensionViewAccumulator accumulator( |
| 203 int index = 0; | 254 ExtensionIdForCurrentContext(), browser_window_id, view_type); |
| 204 RenderView::RenderViewSet* render_view_set_pointer = | 255 RenderView::ForEach(&accumulator); |
| 205 Singleton<RenderView::RenderViewSet>::get(); | 256 return accumulator.views(); |
| 206 DCHECK(render_view_set_pointer->render_view_set_.size() > 0); | |
| 207 | |
| 208 v8::Local<v8::Value> window; | |
| 209 std::string current_extension_id = ExtensionIdForCurrentContext(); | |
| 210 std::set<RenderView* >::iterator it = | |
| 211 render_view_set_pointer->render_view_set_.begin(); | |
| 212 for (; it != render_view_set_pointer->render_view_set_.end(); ++it) { | |
| 213 if (!ViewTypeMatches((*it)->view_type(), view_type)) | |
| 214 continue; | |
| 215 | |
| 216 GURL url = (*it)->webview()->mainFrame()->url(); | |
| 217 if (!url.SchemeIs(chrome::kExtensionScheme)) | |
| 218 continue; | |
| 219 std::string extension_id = url.host(); | |
| 220 if (extension_id != current_extension_id) | |
| 221 continue; | |
| 222 | |
| 223 if (browser_window_id != -1 && | |
| 224 (*it)->browser_window_id() != browser_window_id) { | |
| 225 continue; | |
| 226 } | |
| 227 | |
| 228 v8::Local<v8::Context> context = | |
| 229 (*it)->webview()->mainFrame()->mainWorldScriptContext(); | |
| 230 if (!context.IsEmpty()) { | |
| 231 v8::Local<v8::Value> window = context->Global(); | |
| 232 DCHECK(!window.IsEmpty()); | |
| 233 views->Set(v8::Integer::New(index), window); | |
| 234 index++; | |
| 235 if (view_type == ViewType::EXTENSION_BACKGROUND_PAGE) | |
| 236 break; | |
| 237 } | |
| 238 } | |
| 239 return views; | |
| 240 } | 257 } |
| 241 | 258 |
| 242 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { | 259 static v8::Handle<v8::Value> GetNextRequestId(const v8::Arguments& args) { |
| 243 static int next_request_id = 0; | 260 static int next_request_id = 0; |
| 244 return v8::Integer::New(next_request_id++); | 261 return v8::Integer::New(next_request_id++); |
| 245 } | 262 } |
| 246 | 263 |
| 247 // Creates a new messaging channel to the tab with the given ID. | 264 // Creates a new messaging channel to the tab with the given ID. |
| 248 static v8::Handle<v8::Value> OpenChannelToTab(const v8::Arguments& args) { | 265 static v8::Handle<v8::Value> OpenChannelToTab(const v8::Arguments& args) { |
| 249 // Get the current RenderView so that we can send a routed IPC message from | 266 // Get the current RenderView so that we can send a routed IPC message from |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 return; | 597 return; |
| 581 | 598 |
| 582 v8::HandleScope handle_scope; | 599 v8::HandleScope handle_scope; |
| 583 WebFrame* frame = view->mainFrame(); | 600 WebFrame* frame = view->mainFrame(); |
| 584 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 601 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 585 v8::Handle<v8::Value> argv[1]; | 602 v8::Handle<v8::Value> argv[1]; |
| 586 argv[0] = v8::String::New(type_str); | 603 argv[0] = v8::String::New(type_str); |
| 587 bindings_utils::CallFunctionInContext(context, "setViewType", | 604 bindings_utils::CallFunctionInContext(context, "setViewType", |
| 588 arraysize(argv), argv); | 605 arraysize(argv), argv); |
| 589 } | 606 } |
| OLD | NEW |