| 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/extensions/url_pattern.h" | 9 #include "chrome/common/extensions/url_pattern.h" |
| 10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "webkit/api/public/WebKit.h" | 21 #include "webkit/api/public/WebKit.h" |
| 22 | 22 |
| 23 using bindings_utils::GetStringResource; | 23 using bindings_utils::GetStringResource; |
| 24 using bindings_utils::ContextInfo; | 24 using bindings_utils::ContextInfo; |
| 25 using bindings_utils::ContextList; | 25 using bindings_utils::ContextList; |
| 26 using bindings_utils::GetContexts; | 26 using bindings_utils::GetContexts; |
| 27 using bindings_utils::GetPendingRequestMap; | 27 using bindings_utils::GetPendingRequestMap; |
| 28 using bindings_utils::PendingRequest; | 28 using bindings_utils::PendingRequest; |
| 29 using bindings_utils::PendingRequestMap; | 29 using bindings_utils::PendingRequestMap; |
| 30 using bindings_utils::ExtensionBase; | 30 using bindings_utils::ExtensionBase; |
| 31 using WebKit::WebFrame; |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 // A map of extension ID to vector of page action ids. | 35 // A map of extension ID to vector of page action ids. |
| 35 typedef std::map< std::string, std::vector<std::string> > PageActionIdMap; | 36 typedef std::map< std::string, std::vector<std::string> > PageActionIdMap; |
| 36 | 37 |
| 37 // A map of permission name to whether its enabled for this extension. | 38 // A map of permission name to whether its enabled for this extension. |
| 38 typedef std::map<std::string, bool> PermissionsMap; | 39 typedef std::map<std::string, bool> PermissionsMap; |
| 39 | 40 |
| 40 // A map of extension ID to permissions map. | 41 // A map of extension ID to permissions map. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 113 |
| 113 return ExtensionBase::GetNativeFunction(name); | 114 return ExtensionBase::GetNativeFunction(name); |
| 114 } | 115 } |
| 115 | 116 |
| 116 private: | 117 private: |
| 117 static v8::Handle<v8::Value> GetExtensionAPIDefinition( | 118 static v8::Handle<v8::Value> GetExtensionAPIDefinition( |
| 118 const v8::Arguments& args) { | 119 const v8::Arguments& args) { |
| 119 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); | 120 return v8::String::New(GetStringResource<IDR_EXTENSION_API_JSON>()); |
| 120 } | 121 } |
| 121 | 122 |
| 123 // Returns true is |type| "isa" |match|. |
| 124 static bool ViewTypeMatches(ViewType::Type type, ViewType::Type match) { |
| 125 if (type == match) |
| 126 return true; |
| 127 |
| 128 // INVALID means match all. |
| 129 if (match == ViewType::INVALID) |
| 130 return true; |
| 131 |
| 132 // TODO(erikkay) for now, special case mole as a type of toolstrip. |
| 133 // Perhaps this isn't the right long-term thing to do. |
| 134 if (match == ViewType::EXTENSION_TOOLSTRIP && |
| 135 type == ViewType::EXTENSION_MOLE) { |
| 136 return true; |
| 137 } |
| 138 |
| 139 return false; |
| 140 } |
| 141 |
| 122 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { | 142 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { |
| 123 if (args.Length() != 2) | 143 if (args.Length() != 2) |
| 124 return v8::Undefined(); | 144 return v8::Undefined(); |
| 125 | 145 |
| 126 if (!args[0]->IsInt32() || !args[1]->IsString()) | 146 if (!args[0]->IsInt32() || !args[1]->IsString()) |
| 127 return v8::Undefined(); | 147 return v8::Undefined(); |
| 128 | 148 |
| 129 // |browser_window_id| == -1 means getting views attached to any browser | 149 // |browser_window_id| == -1 means getting views attached to any browser |
| 130 // window. | 150 // window. |
| 131 int browser_window_id = args[0]->Int32Value(); | 151 int browser_window_id = args[0]->Int32Value(); |
| 132 | 152 |
| 133 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); | 153 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); |
| 134 // |view_type| == ViewType::INVALID means getting any type of views. | 154 // |view_type| == ViewType::INVALID means getting any type of views. |
| 135 ViewType::Type view_type = ViewType::INVALID; | 155 ViewType::Type view_type = ViewType::INVALID; |
| 136 if (view_type_string == "TOOLSTRIP") { | 156 if (view_type_string == "TOOLSTRIP") { |
| 137 view_type = ViewType::EXTENSION_TOOLSTRIP; | 157 view_type = ViewType::EXTENSION_TOOLSTRIP; |
| 158 } else if (view_type_string == "MOLE") { |
| 159 view_type = ViewType::EXTENSION_MOLE; |
| 138 } else if (view_type_string == "BACKGROUND") { | 160 } else if (view_type_string == "BACKGROUND") { |
| 139 view_type = ViewType::EXTENSION_BACKGROUND_PAGE; | 161 view_type = ViewType::EXTENSION_BACKGROUND_PAGE; |
| 140 } else if (view_type_string == "TAB") { | 162 } else if (view_type_string == "TAB") { |
| 141 view_type = ViewType::TAB_CONTENTS; | 163 view_type = ViewType::TAB_CONTENTS; |
| 142 } else if (view_type_string != "ALL") { | 164 } else if (view_type_string != "ALL") { |
| 143 return v8::Undefined(); | 165 return v8::Undefined(); |
| 144 } | 166 } |
| 145 | 167 |
| 146 v8::Local<v8::Array> views = v8::Array::New(); | 168 v8::Local<v8::Array> views = v8::Array::New(); |
| 147 int index = 0; | 169 int index = 0; |
| 148 RenderView::RenderViewSet* render_view_set_pointer = | 170 RenderView::RenderViewSet* render_view_set_pointer = |
| 149 Singleton<RenderView::RenderViewSet>::get(); | 171 Singleton<RenderView::RenderViewSet>::get(); |
| 150 DCHECK(render_view_set_pointer->render_view_set_.size() > 0); | 172 DCHECK(render_view_set_pointer->render_view_set_.size() > 0); |
| 151 | 173 |
| 152 v8::Local<v8::Value> window; | 174 v8::Local<v8::Value> window; |
| 153 std::string current_extension_id = ExtensionIdForCurrentContext(); | 175 std::string current_extension_id = ExtensionIdForCurrentContext(); |
| 154 std::set<RenderView* >::iterator it = | 176 std::set<RenderView* >::iterator it = |
| 155 render_view_set_pointer->render_view_set_.begin(); | 177 render_view_set_pointer->render_view_set_.begin(); |
| 156 for (; it != render_view_set_pointer->render_view_set_.end(); ++it) { | 178 for (; it != render_view_set_pointer->render_view_set_.end(); ++it) { |
| 157 if (view_type != ViewType::INVALID && (*it)->view_type() != view_type) | 179 if (!ViewTypeMatches((*it)->view_type(), view_type)) |
| 158 continue; | 180 continue; |
| 159 | 181 |
| 160 GURL url = (*it)->webview()->GetMainFrame()->url(); | 182 GURL url = (*it)->webview()->GetMainFrame()->url(); |
| 161 if (!url.SchemeIs(chrome::kExtensionScheme)) | 183 if (!url.SchemeIs(chrome::kExtensionScheme)) |
| 162 continue; | 184 continue; |
| 163 std::string extension_id = url.host(); | 185 std::string extension_id = url.host(); |
| 164 if (extension_id != current_extension_id) | 186 if (extension_id != current_extension_id) |
| 165 continue; | 187 continue; |
| 166 | 188 |
| 167 if (browser_window_id != -1 && | 189 if (browser_window_id != -1 && |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 const std::string& function_name) { | 411 const std::string& function_name) { |
| 390 static const char kMessage[] = | 412 static const char kMessage[] = |
| 391 "You do not have permission to use 'chrome.%s'. Be sure to declare" | 413 "You do not have permission to use 'chrome.%s'. Be sure to declare" |
| 392 " in your manifest what permissions you need."; | 414 " in your manifest what permissions you need."; |
| 393 std::string permission_name = GetPermissionName(function_name); | 415 std::string permission_name = GetPermissionName(function_name); |
| 394 std::string error_msg = StringPrintf(kMessage, permission_name.c_str()); | 416 std::string error_msg = StringPrintf(kMessage, permission_name.c_str()); |
| 395 | 417 |
| 396 return v8::ThrowException(v8::Exception::Error( | 418 return v8::ThrowException(v8::Exception::Error( |
| 397 v8::String::New(error_msg.c_str()))); | 419 v8::String::New(error_msg.c_str()))); |
| 398 } | 420 } |
| 421 |
| 422 // static |
| 423 void ExtensionProcessBindings::SetViewType(WebView* view, |
| 424 ViewType::Type type) { |
| 425 DCHECK(type == ViewType::EXTENSION_MOLE || |
| 426 type == ViewType::EXTENSION_TOOLSTRIP); |
| 427 const char* type_str; |
| 428 if (type == ViewType::EXTENSION_MOLE) |
| 429 type_str = "mole"; |
| 430 else if (type == ViewType::EXTENSION_TOOLSTRIP) |
| 431 type_str = "toolstrip"; |
| 432 else |
| 433 return; |
| 434 |
| 435 v8::HandleScope handle_scope; |
| 436 WebFrame* frame = view->GetMainFrame(); |
| 437 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 438 v8::Handle<v8::Value> argv[1]; |
| 439 argv[0] = v8::String::New(type_str); |
| 440 bindings_utils::CallFunctionInContext(context, "setViewType", |
| 441 arraysize(argv), argv); |
| 442 } |
| OLD | NEW |