1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/renderer/extensions/extension_custom_bindings.h" 6 7 #include <string> 8 9 #include "base/bind.h" 10 #include "base/strings/string_util.h" 11 #include "chrome/common/extensions/extension.h" 12 #include "chrome/common/url_constants.h" 13 #include "chrome/renderer/extensions/dispatcher.h" 14 #include "chrome/renderer/extensions/extension_helper.h" 15 #include "content/public/renderer/render_view.h" 16 #include "content/public/renderer/v8_value_converter.h" 17 #include "extensions/common/view_type.h" 18 #include "grit/renderer_resources.h" 19 #include "third_party/WebKit/public/web/WebFrame.h" 20 #include "third_party/WebKit/public/web/WebView.h" 21 #include "v8/include/v8.h" 22 #include "webkit/glue/webkit_glue.h" 23 24 namespace extensions { 25 26 namespace { 27 28 } // namespace 29 30 ExtensionCustomBindings::ExtensionCustomBindings(Dispatcher* dispatcher, 31 ChromeV8Context* context) 32 : ChromeV8Extension(dispatcher, context) { 33 RouteFunction("GetExtensionViews", 34 base::Bind(&ExtensionCustomBindings::GetExtensionViews, 35 base::Unretained(this))); 36 } 37 38 void ExtensionCustomBindings::GetExtensionViews( 39 const v8::FunctionCallbackInfo<v8::Value>& args) { 40 if (args.Length() != 2) 41 return; 42 43 if (!args[0]->IsInt32() || !args[1]->IsString()) 44 return; 45 46 // |browser_window_id| == extension_misc::kUnknownWindowId means getting 47 // views attached to any browser window. 48 int browser_window_id = args[0]->Int32Value(); 49 50 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString()); 51 StringToUpperASCII(&view_type_string); 52 // |view_type| == VIEW_TYPE_INVALID means getting any type of 53 // views. 54 ViewType view_type = VIEW_TYPE_INVALID; 55 if (view_type_string == kViewTypeBackgroundPage) { 56 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; 57 } else if (view_type_string == kViewTypeInfobar) { 58 view_type = VIEW_TYPE_EXTENSION_INFOBAR; 59 } else if (view_type_string == kViewTypeNotification) { 60 view_type = VIEW_TYPE_NOTIFICATION; 61 } else if (view_type_string == kViewTypeTabContents) { 62 view_type = VIEW_TYPE_TAB_CONTENTS; 63 } else if (view_type_string == kViewTypePopup) { 64 view_type = VIEW_TYPE_EXTENSION_POPUP; 65 } else if (view_type_string == kViewTypeExtensionDialog) { 66 view_type = VIEW_TYPE_EXTENSION_DIALOG; 67 } else if (view_type_string == kViewTypeAppShell) { 68 view_type = VIEW_TYPE_APP_SHELL; 69 } else if (view_type_string == kViewTypePanel) { 70 view_type = VIEW_TYPE_PANEL; 71 } else if (view_type_string != kViewTypeAll) { 72 return; 73 } 74 75 const Extension* extension = GetExtensionForRenderView(); 76 if (!extension) 77 return; 78 79 std::vector<content::RenderView*> views = ExtensionHelper::GetExtensionViews( 80 extension->id(), browser_window_id, view_type); 81 v8::Local<v8::Array> v8_views = v8::Array::New(); 82 int v8_index = 0; 83 for (size_t i = 0; i < views.size(); ++i) { 84 v8::Local<v8::Context> context = 85 views[i]->GetWebView()->mainFrame()->mainWorldScriptContext(); 86 if (!context.IsEmpty()) { 87 v8::Local<v8::Value> window = context->Global(); 88 DCHECK(!window.IsEmpty()); 89 v8_views->Set(v8::Integer::New(v8_index++), window); 90 } 91 } 92 93 args.GetReturnValue().Set(v8_views); 94 } 95 96 } // namespace extensions 97