| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 194 |
| 195 // Returns true is |type| "isa" |match|. | 195 // Returns true is |type| "isa" |match|. |
| 196 static bool ViewTypeMatches(ViewType::Type type, ViewType::Type match) { | 196 static bool ViewTypeMatches(ViewType::Type type, ViewType::Type match) { |
| 197 if (type == match) | 197 if (type == match) |
| 198 return true; | 198 return true; |
| 199 | 199 |
| 200 // INVALID means match all. | 200 // INVALID means match all. |
| 201 if (match == ViewType::INVALID) | 201 if (match == ViewType::INVALID) |
| 202 return true; | 202 return true; |
| 203 | 203 |
| 204 // TODO(erikkay) for now, special case mole as a type of toolstrip. | |
| 205 // Perhaps this isn't the right long-term thing to do. | |
| 206 if (match == ViewType::EXTENSION_TOOLSTRIP && | |
| 207 type == ViewType::EXTENSION_MOLE) { | |
| 208 return true; | |
| 209 } | |
| 210 | |
| 211 return false; | 204 return false; |
| 212 } | 205 } |
| 213 | 206 |
| 214 std::string extension_id_; | 207 std::string extension_id_; |
| 215 int browser_window_id_; | 208 int browser_window_id_; |
| 216 ViewType::Type view_type_; | 209 ViewType::Type view_type_; |
| 217 v8::Local<v8::Array> views_; | 210 v8::Local<v8::Array> views_; |
| 218 int index_; | 211 int index_; |
| 219 }; | 212 }; |
| 220 | 213 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 } | 275 } |
| 283 | 276 |
| 284 static v8::Handle<v8::Value> PopupViewFinder( | 277 static v8::Handle<v8::Value> PopupViewFinder( |
| 285 const v8::Arguments& args, | 278 const v8::Arguments& args, |
| 286 ViewType::Type viewtype_to_find) { | 279 ViewType::Type viewtype_to_find) { |
| 287 // TODO(twiz) Correct the logic that ties the ownership of the pop-up view | 280 // TODO(twiz) Correct the logic that ties the ownership of the pop-up view |
| 288 // to the hosting view. At the moment we assume that there may only be | 281 // to the hosting view. At the moment we assume that there may only be |
| 289 // a single pop-up view for a given extension. By doing so, we can find | 282 // a single pop-up view for a given extension. By doing so, we can find |
| 290 // the pop-up view by simply searching for the only pop-up view present. | 283 // the pop-up view by simply searching for the only pop-up view present. |
| 291 // We also assume that if the current view is a pop-up, we can find the | 284 // We also assume that if the current view is a pop-up, we can find the |
| 292 // hosting view by searching for a TOOLSTRIP view. | 285 // hosting view by searching for a tab contents view. |
| 293 if (args.Length() != 0) | 286 if (args.Length() != 0) |
| 294 return v8::Undefined(); | 287 return v8::Undefined(); |
| 295 | 288 |
| 296 if (viewtype_to_find != ViewType::EXTENSION_POPUP && | 289 if (viewtype_to_find != ViewType::EXTENSION_POPUP && |
| 297 viewtype_to_find != ViewType::EXTENSION_TOOLSTRIP) { | 290 viewtype_to_find != ViewType::TAB_CONTENTS) { |
| 298 NOTREACHED() << "Requesting invalid view type."; | 291 NOTREACHED() << "Requesting invalid view type."; |
| 299 } | 292 } |
| 300 | 293 |
| 301 // Disallow searching for a host view if we are a popup view, and likewise | 294 // Disallow searching for the same view type as the current view: |
| 302 // if we are a toolstrip view. | 295 // Popups can only look for hosts, and hosts can only look for popups. |
| 303 RenderView* render_view = bindings_utils::GetRenderViewForCurrentContext(); | 296 RenderView* render_view = bindings_utils::GetRenderViewForCurrentContext(); |
| 304 if (!render_view || | 297 if (!render_view || |
| 305 render_view->view_type() == viewtype_to_find) { | 298 render_view->view_type() == viewtype_to_find) { |
| 306 return v8::Undefined(); | 299 return v8::Undefined(); |
| 307 } | 300 } |
| 308 | 301 |
| 309 int browser_window_id = render_view->browser_window_id(); | 302 int browser_window_id = render_view->browser_window_id(); |
| 310 std::string extension_id = ExtensionIdForCurrentContext(); | 303 std::string extension_id = ExtensionIdForCurrentContext(); |
| 311 if (extension_id.empty()) | 304 if (extension_id.empty()) |
| 312 return v8::Undefined(); | 305 return v8::Undefined(); |
| 313 | 306 |
| 314 ExtensionViewAccumulator popup_matcher(extension_id, | 307 ExtensionViewAccumulator popup_matcher(extension_id, |
| 315 browser_window_id, | 308 browser_window_id, |
| 316 viewtype_to_find); | 309 viewtype_to_find); |
| 317 RenderView::ForEach(&popup_matcher); | 310 RenderView::ForEach(&popup_matcher); |
| 318 | 311 |
| 319 if (0 == popup_matcher.views()->Length()) | 312 if (0 == popup_matcher.views()->Length()) |
| 320 return v8::Undefined(); | 313 return v8::Undefined(); |
| 321 DCHECK(1 == popup_matcher.views()->Length()); | 314 DCHECK(1 == popup_matcher.views()->Length()); |
| 322 | 315 |
| 323 // Return the first view found. | 316 // Return the first view found. |
| 324 return popup_matcher.views()->Get(v8::Integer::New(0)); | 317 return popup_matcher.views()->Get(v8::Integer::New(0)); |
| 325 } | 318 } |
| 326 | 319 |
| 327 static v8::Handle<v8::Value> GetPopupView(const v8::Arguments& args) { | 320 static v8::Handle<v8::Value> GetPopupView(const v8::Arguments& args) { |
| 328 return PopupViewFinder(args, ViewType::EXTENSION_POPUP); | 321 return PopupViewFinder(args, ViewType::EXTENSION_POPUP); |
| 329 } | 322 } |
| 330 | 323 |
| 331 static v8::Handle<v8::Value> GetPopupParentWindow(const v8::Arguments& args) { | 324 static v8::Handle<v8::Value> GetPopupParentWindow(const v8::Arguments& args) { |
| 332 return PopupViewFinder(args, ViewType::EXTENSION_TOOLSTRIP); | 325 return PopupViewFinder(args, ViewType::TAB_CONTENTS); |
| 333 } | 326 } |
| 334 | 327 |
| 335 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { | 328 static v8::Handle<v8::Value> GetExtensionViews(const v8::Arguments& args) { |
| 336 if (args.Length() != 2) | 329 if (args.Length() != 2) |
| 337 return v8::Undefined(); | 330 return v8::Undefined(); |
| 338 | 331 |
| 339 if (!args[0]->IsInt32() || !args[1]->IsString()) | 332 if (!args[0]->IsInt32() || !args[1]->IsString()) |
| 340 return v8::Undefined(); | 333 return v8::Undefined(); |
| 341 | 334 |
| 342 // |browser_window_id| == extension_misc::kUnknownWindowId means getting | 335 // |browser_window_id| == extension_misc::kUnknownWindowId means getting |
| 343 // views attached to any browser window. | 336 // views attached to any browser window. |
| 344 int browser_window_id = args[0]->Int32Value(); | 337 int browser_window_id = args[0]->Int32Value(); |
| 345 | 338 |
| 346 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); | 339 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); |
| 347 StringToUpperASCII(&view_type_string); | 340 StringToUpperASCII(&view_type_string); |
| 348 // |view_type| == ViewType::INVALID means getting any type of views. | 341 // |view_type| == ViewType::INVALID means getting any type of views. |
| 349 ViewType::Type view_type = ViewType::INVALID; | 342 ViewType::Type view_type = ViewType::INVALID; |
| 350 if (view_type_string == ViewType::kToolstrip) { | 343 if (view_type_string == ViewType::kBackgroundPage) { |
| 351 view_type = ViewType::EXTENSION_TOOLSTRIP; | |
| 352 } else if (view_type_string == ViewType::kMole) { | |
| 353 view_type = ViewType::EXTENSION_MOLE; | |
| 354 } else if (view_type_string == ViewType::kBackgroundPage) { | |
| 355 view_type = ViewType::EXTENSION_BACKGROUND_PAGE; | 344 view_type = ViewType::EXTENSION_BACKGROUND_PAGE; |
| 356 } else if (view_type_string == ViewType::kInfobar) { | 345 } else if (view_type_string == ViewType::kInfobar) { |
| 357 view_type = ViewType::EXTENSION_INFOBAR; | 346 view_type = ViewType::EXTENSION_INFOBAR; |
| 358 } else if (view_type_string == ViewType::kNotification) { | 347 } else if (view_type_string == ViewType::kNotification) { |
| 359 view_type = ViewType::NOTIFICATION; | 348 view_type = ViewType::NOTIFICATION; |
| 360 } else if (view_type_string == ViewType::kTabContents) { | 349 } else if (view_type_string == ViewType::kTabContents) { |
| 361 view_type = ViewType::TAB_CONTENTS; | 350 view_type = ViewType::TAB_CONTENTS; |
| 362 } else if (view_type_string == ViewType::kPopup) { | 351 } else if (view_type_string == ViewType::kPopup) { |
| 363 view_type = ViewType::EXTENSION_POPUP; | 352 view_type = ViewType::EXTENSION_POPUP; |
| 364 } else if (view_type_string != ViewType::kAll) { | 353 } else if (view_type_string != ViewType::kAll) { |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 const std::string& function_name) { | 691 const std::string& function_name) { |
| 703 static const char kMessage[] = | 692 static const char kMessage[] = |
| 704 "You do not have permission to use '%s'. Be sure to declare" | 693 "You do not have permission to use '%s'. Be sure to declare" |
| 705 " in your manifest what permissions you need."; | 694 " in your manifest what permissions you need."; |
| 706 std::string error_msg = StringPrintf(kMessage, function_name.c_str()); | 695 std::string error_msg = StringPrintf(kMessage, function_name.c_str()); |
| 707 | 696 |
| 708 return v8::ThrowException(v8::Exception::Error( | 697 return v8::ThrowException(v8::Exception::Error( |
| 709 v8::String::New(error_msg.c_str()))); | 698 v8::String::New(error_msg.c_str()))); |
| 710 } | 699 } |
| 711 | 700 |
| 712 // static | |
| 713 void ExtensionProcessBindings::SetViewType(WebView* view, | |
| 714 ViewType::Type type) { | |
| 715 DCHECK(type == ViewType::EXTENSION_MOLE || | |
| 716 type == ViewType::EXTENSION_TOOLSTRIP); | |
| 717 const char* type_str; | |
| 718 if (type == ViewType::EXTENSION_MOLE) | |
| 719 type_str = "mole"; | |
| 720 else if (type == ViewType::EXTENSION_TOOLSTRIP) | |
| 721 type_str = "toolstrip"; | |
| 722 else | |
| 723 return; | |
| 724 | |
| 725 v8::HandleScope handle_scope; | |
| 726 WebFrame* frame = view->mainFrame(); | |
| 727 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | |
| 728 v8::Handle<v8::Value> argv[1]; | |
| 729 argv[0] = v8::String::New(type_str); | |
| 730 bindings_utils::CallFunctionInContext(context, "setViewType", | |
| 731 arraysize(argv), argv); | |
| 732 } | |
| OLD | NEW |