diff options
Diffstat (limited to 'chrome')
21 files changed, 437 insertions, 94 deletions
diff --git a/chrome/browser/browser.scons b/chrome/browser/browser.scons index fa9dba8..e9931dc 100644 --- a/chrome/browser/browser.scons +++ b/chrome/browser/browser.scons @@ -506,15 +506,19 @@ input_files = ChromeFileList([ MSVSFilter('Extensions', [ 'extensions/extension.cc', 'extensions/extension.h', - 'extensions/extension_api_handler.cc', - 'extensions/extension_api_handler.h', - 'extensions/extension_view.cc', - 'extensions/extension_view.h', + 'extensions/extension_function.cc', + 'extensions/extension_function.h', + 'extensions/extension_function_dispatcher.cc', + 'extensions/extension_function_dispatcher.h', 'extensions/extension_error_reporter.cc', 'extensions/extension_error_reporter.h', 'extensions/extension_message_service.cc', 'extensions/extension_message_service.h', 'extensions/extension_protocols.h', + 'extensions/extension_tabs_module.cc', + 'extensions/extension_tabs_module.h', + 'extensions/extension_view.cc', + 'extensions/extension_view.h', 'extensions/extensions_service.cc', 'extensions/extensions_service.h', 'extensions/extensions_ui.cc', @@ -530,6 +534,7 @@ input_files = ChromeFileList([ 'renderer_host/backing_store.h', 'renderer_host/backing_store.cc', 'renderer_host/backing_store_win.cc', + 'renderer_host/backing_store_win.cc', 'renderer_host/browser_render_process_host.cc', 'renderer_host/browser_render_process_host.h', 'renderer_host/buffered_resource_handler.cc', diff --git a/chrome/browser/browser.vcproj b/chrome/browser/browser.vcproj index ff76afb..63934d7 100644 --- a/chrome/browser/browser.vcproj +++ b/chrome/browser/browser.vcproj @@ -822,11 +822,11 @@ > </File> <File - RelativePath=".\automation\automation_provider_list_generic.cc" + RelativePath=".\automation\automation_provider_list.h" > </File> <File - RelativePath=".\automation\automation_provider_list.h" + RelativePath=".\automation\automation_provider_list_generic.cc" > </File> <File @@ -1906,19 +1906,27 @@ > </File> <File - RelativePath=".\extensions\extension_api_handler.cc" + RelativePath=".\extensions\extension_error_reporter.cc" > </File> <File - RelativePath=".\extensions\extension_api_handler.h" + RelativePath=".\extensions\extension_error_reporter.h" > </File> <File - RelativePath=".\extensions\extension_error_reporter.cc" + RelativePath=".\extensions\extension_function.cc" > </File> <File - RelativePath=".\extensions\extension_error_reporter.h" + RelativePath=".\extensions\extension_function.h" + > + </File> + <File + RelativePath=".\extensions\extension_function_dispatcher.cc" + > + </File> + <File + RelativePath=".\extensions\extension_function_dispatcher.h" > </File> <File @@ -1938,6 +1946,14 @@ > </File> <File + RelativePath=".\extensions\extension_tabs_module.cc" + > + </File> + <File + RelativePath=".\extensions\extension_tabs_module.h" + > + </File> + <File RelativePath=".\extensions\extension_view.cc" > </File> diff --git a/chrome/browser/extensions/extension_api_handler.cc b/chrome/browser/extensions/extension_api_handler.cc deleted file mode 100644 index 702917b..0000000 --- a/chrome/browser/extensions/extension_api_handler.cc +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/extensions/extension_api_handler.h" - -#include "base/json_reader.h" -#include "base/json_writer.h" -#include "base/values.h" -#include "chrome/browser/browser.h" -#include "chrome/browser/browser_list.h" -#include "chrome/browser/renderer_host/render_view_host.h" - -ExtensionAPIHandler::ExtensionAPIHandler(RenderViewHost* render_view_host) - : render_view_host_(render_view_host) {} - -void ExtensionAPIHandler::HandleRequest(const std::string& name, - const std::string& args, - int callback_id) { - scoped_ptr<Value> value; - if (!args.empty()) { - value.reset(JSONReader::Read(args, false)); - DCHECK(value.get()); - } - - // TODO(aa): This will probably dispatch to per-module specialized classes. - // Consider refactoring similar work in dom_ui to reuse. - if (name == "CreateTab") { - Browser* browser = BrowserList::GetLastActive(); - if (browser) { - DCHECK(value->IsType(Value::TYPE_DICTIONARY)); - std::string url; - static_cast<DictionaryValue*>(value.get())->GetString(L"url", &url); - browser->AddTabWithURL(GURL(url), GURL(), PageTransition::TYPED, true, - NULL); - - static int response_count = 0; - scoped_ptr<Value> response(Value::CreateIntegerValue(response_count++)); - std::string json; - JSONWriter::Write(response.get(), false, &json); - - render_view_host_->SendExtensionResponse(callback_id, json); - } - } -} diff --git a/chrome/browser/extensions/extension_api_handler.h b/chrome/browser/extensions/extension_api_handler.h deleted file mode 100644 index 3cd6efd..0000000 --- a/chrome/browser/extensions/extension_api_handler.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_APIS_H_ -#define CHROME_BROWSER_EXTENSIONS_EXTENSION_APIS_H_ - -#include <string> - -class RenderViewHost; - -// ExtensionAPIHandler is the top-level entry point for extension callbacks -// in the browser process. It lives on the UI thread. -class ExtensionAPIHandler { - public: - ExtensionAPIHandler(RenderViewHost* render_view_host); - - // Handle a request to perform some synchronous API. - // TODO(aa): args should be a Value object. - void HandleRequest(const std::string& name, const std::string& args, - int callback_id); - - private: - // TODO(aa): Once there can be APIs that are asynchronous wrt the browser's UI - // thread, we may have to have to do something about this raw pointer. - RenderViewHost* render_view_host_; -}; - -#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_APIS_H_ diff --git a/chrome/browser/extensions/extension_function.cc b/chrome/browser/extensions/extension_function.cc new file mode 100644 index 0000000..8eb562f --- /dev/null +++ b/chrome/browser/extensions/extension_function.cc @@ -0,0 +1,16 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/extension_function.h" + +#include "chrome/browser/extensions/extension_function_dispatcher.h" + +void ExtensionFunction::SendResponse(bool success) { + if (success) { + dispatcher_->SendResponse(this); + } else { + // TODO(aa): In case of failure, send the error message to an error + // callback. + } +} diff --git a/chrome/browser/extensions/extension_function.h b/chrome/browser/extensions/extension_function.h new file mode 100644 index 0000000..421df81 --- /dev/null +++ b/chrome/browser/extensions/extension_function.h @@ -0,0 +1,81 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ + +#include <string> + +#include "base/values.h" +#include "base/scoped_ptr.h" + +class ExtensionFunctionDispatcher; + +// Base class for an extension function. +// TODO(aa): This will have to become reference counted when we introduce APIs +// that live beyond a single stack frame. +class ExtensionFunction { + public: + virtual ~ExtensionFunction() {} + + void set_dispatcher(ExtensionFunctionDispatcher* dispatcher) { + dispatcher_ = dispatcher; + } + void set_args(Value* args) { args_ = args; } + + void set_callback_id(int callback_id) { callback_id_ = callback_id; } + int callback_id() { return callback_id_; } + + Value* result() { return result_.get(); } + const std::string& error() { return error_; } + + // Whether the extension has registered a callback and is waiting for a + // response. APIs can use this to avoid doing unnecessary work in the case + // that the extension is not expecting a response. + bool has_callback() { return callback_id_ != -1; } + + // Execute the API. Clients should call set_args() and set_callback_id() + // before calling this method. Derived classes should populate result_ and + // error_ before returning. + virtual void Run() = 0; + + protected: + void SendResponse(bool success); + + // The arguments to the API. Only non-null if argument were specfied. + Value* args_; + + // The result of the API. This should be populated by the derived class before + // Run() returns. + scoped_ptr<Value> result_; + + // Any detailed error from the API. This should be populated by the derived + // class before Run() returns. + std::string error_; + + private: + ExtensionFunctionDispatcher* dispatcher_; + int callback_id_; +}; + + +// A SyncExtensionFunction is an ExtensionFunction that runs synchronously +// *relative to the browser's UI thread*. Note that this has nothing to do with +// running synchronously relative to the extension process. From the extension +// process's point of view, the function is still asynchronous. +// +// This kind of function is convenient for implementing simple APIs that just +// need to interact with things on the browser UI thread. +class SyncExtensionFunction : public ExtensionFunction { + public: + // Derived classes should implement this method to do their work and return + // success/failure. + virtual bool RunImpl() = 0; + + virtual void Run() { + SendResponse(RunImpl()); + } +}; + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_H_ diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc new file mode 100644 index 0000000..f1f1379 --- /dev/null +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -0,0 +1,115 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/extension_function_dispatcher.h" + +#include "base/json_reader.h" +#include "base/json_writer.h" +#include "base/singleton.h" +#include "base/values.h" +#include "chrome/browser/extensions/extension_function.h" +#include "chrome/browser/extensions/extension_tabs_module.h" +#include "chrome/browser/renderer_host/render_view_host.h" + +// FactoryRegistry ------------------------------------------------------------- + +namespace { + +// A pointer to a function that create an instance of an ExtensionFunction. +typedef ExtensionFunction* (*ExtensionFunctionFactory)(); + +// Contains a list of all known extension functions and allows clients to create +// instances of them. +class FactoryRegistry { + public: + static FactoryRegistry* instance(); + FactoryRegistry(); + void GetAllNames(std::vector<std::string>* names); + ExtensionFunction* NewFunction(const std::string& name); + + private: + typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; + FactoryMap factories_; +}; + +// Template for defining ExtensionFunctionFactory. +template<class T> +ExtensionFunction* NewExtensionFunction() { + return new T(); +} + +FactoryRegistry* FactoryRegistry::instance() { + return Singleton<FactoryRegistry>::get(); +} + +FactoryRegistry::FactoryRegistry() { + // Register all functions here. + factories_["GetTabsForWindow"] = + &NewExtensionFunction<GetTabsForWindowFunction>; + factories_["CreateTab"] = &NewExtensionFunction<CreateTabFunction>; +} + +void FactoryRegistry::GetAllNames( + std::vector<std::string>* names) { + for (FactoryMap::iterator iter = factories_.begin(); iter != factories_.end(); + ++iter) { + names->push_back(iter->first); + } +} + +ExtensionFunction* FactoryRegistry::NewFunction(const std::string& name) { + FactoryMap::iterator iter = factories_.find(name); + DCHECK(iter != factories_.end()); + return iter->second(); +} + +}; + + +// ExtensionFunctionDispatcher ------------------------------------------------- + +void ExtensionFunctionDispatcher::GetAllFunctionNames( + std::vector<std::string>* names) { + FactoryRegistry::instance()->GetAllNames(names); +} + +ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( + RenderViewHost* render_view_host) + : render_view_host_(render_view_host) {} + +void ExtensionFunctionDispatcher::HandleRequest(const std::string& name, + const std::string& args, + int callback_id) { + scoped_ptr<Value> value; + if (!args.empty()) { + JSONReader reader; + value.reset(reader.JsonToValue(args, false, false)); + + // Since we do the serialization in the v8 extension, we should always get + // valid JSON. + if (!value.get()) { + DCHECK(false); + return; + } + } + + // TODO(aa): This will get a bit more complicated when we support functions + // that live longer than the stack frame. + scoped_ptr<ExtensionFunction> function( + FactoryRegistry::instance()->NewFunction(name)); + function->set_dispatcher(this); + function->set_args(value.get()); + function->set_callback_id(callback_id); + function->Run(); +} + +void ExtensionFunctionDispatcher::SendResponse(ExtensionFunction* function) { + std::string json; + + // Some functions might not need to return any results. + if (function->result()) + JSONWriter::Write(function->result(), false, &json); + + render_view_host_->SendExtensionResponse(function->callback_id(), json); +} diff --git a/chrome/browser/extensions/extension_function_dispatcher.h b/chrome/browser/extensions/extension_function_dispatcher.h new file mode 100644 index 0000000..2b9c49e --- /dev/null +++ b/chrome/browser/extensions/extension_function_dispatcher.h @@ -0,0 +1,37 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ + +#include <string> +#include <vector> + +#include "base/values.h" + +class ExtensionFunction; +class RenderViewHost; + +// ExtensionFunctionDispatcher receives requests to execute functions from +// Chromium extensions running in a RenderViewHost and dispatches them to the +// appropriate handler. It lives entirely on the UI thread. +class ExtensionFunctionDispatcher { + public: + // Gets a list of all known extension function names. + static void GetAllFunctionNames(std::vector<std::string>* names); + + ExtensionFunctionDispatcher(RenderViewHost* render_view_host); + + // Handle a request to execute an extension function. + void HandleRequest(const std::string& name, const std::string& args, + int callback_id); + + // Send a response to a function. + void SendResponse(ExtensionFunction* api); + + private: + RenderViewHost* render_view_host_; +}; + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_DISPATCHER_H_ diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc new file mode 100644 index 0000000..8568b43 --- /dev/null +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -0,0 +1,79 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/extension_tabs_module.h" + +#include "base/string_util.h" +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_list.h" +#include "chrome/browser/extensions/extension_function_dispatcher.h" +#include "chrome/browser/tab_contents/navigation_entry.h" + +// Forward declare static helper functions defined below. +static DictionaryValue* CreateTabValue(TabStripModel* tab_strip_model, + int tab_index); + +bool GetTabsForWindowFunction::RunImpl() { + if (!args_->IsType(Value::TYPE_NULL)) + return false; + + Browser* browser = BrowserList::GetLastActive(); + if (!browser) + return false; + + TabStripModel* tab_strip = browser->tabstrip_model(); + result_.reset(new ListValue()); + for (int i = 0; i < tab_strip->count(); ++i) { + static_cast<ListValue*>(result_.get())->Append( + CreateTabValue(tab_strip, i)); + } + + return true; +} + +bool CreateTabFunction::RunImpl() { + // TODO(aa): Do data-driven validation in JS. + if (!args_->IsType(Value::TYPE_DICTIONARY)) + return false; + + Browser* browser = BrowserList::GetLastActive(); + if (!browser) + return false; + + // TODO(aa): Handle all the other properties of the new tab. + std::string url; + static_cast<const DictionaryValue*>(args_)->GetString(L"url", &url); + browser->AddTabWithURL(GURL(url), GURL(), PageTransition::TYPED, true, NULL); + + // Return data about the newly created tab. + if (has_callback()) + result_.reset(CreateTabValue(browser->tabstrip_model(), + browser->tabstrip_model()->count() - 1)); + + return true; +} + + +// static helpers +static DictionaryValue* CreateTabValue(TabStripModel* tab_strip, + int tab_index) { + TabContents* contents = tab_strip->GetTabContentsAt(tab_index); + NavigationController* controller = contents->controller(); + DCHECK(controller); // TODO(aa): Is this a valid assumption? + + DictionaryValue* result = new DictionaryValue(); + result->SetInteger(L"id", controller->session_id().id()); + result->SetInteger(L"windowId", controller->window_id().id()); + result->SetString(L"url", contents->GetURL().spec()); + result->SetString(L"title", UTF16ToWide(contents->GetTitle())); + result->SetBoolean(L"selected", tab_index == tab_strip->selected_index()); + + NavigationEntry* entry = controller->GetActiveEntry(); + if (entry) { + if (entry->favicon().is_valid()) + result->SetString(L"favIconUrl", entry->favicon().url().spec()); + } + + return result; +} diff --git a/chrome/browser/extensions/extension_tabs_module.h b/chrome/browser/extensions/extension_tabs_module.h new file mode 100644 index 0000000..6dfffa5 --- /dev/null +++ b/chrome/browser/extensions/extension_tabs_module.h @@ -0,0 +1,17 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TABS_MODULE_H__ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TABS_MODULE_H__ + +#include "chrome/browser/extensions/extension_function.h" + +class GetTabsForWindowFunction : public SyncExtensionFunction { + virtual bool RunImpl(); +}; +class CreateTabFunction : public SyncExtensionFunction { + virtual bool RunImpl(); +}; + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TABS_MODULE_H__ diff --git a/chrome/browser/renderer_host/browser_render_process_host.cc b/chrome/browser/renderer_host/browser_render_process_host.cc index 4d6d917..af974f5 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.cc +++ b/chrome/browser/renderer_host/browser_render_process_host.cc @@ -346,6 +346,7 @@ bool BrowserRenderProcessHost::Init() { InitVisitedLinks(); InitUserScripts(); + InitExtensions(); if (max_page_id_ != -1) channel_->Send(new ViewMsg_SetNextPageID(max_page_id_ + 1)); @@ -448,6 +449,14 @@ void BrowserRenderProcessHost::InitUserScripts() { SendUserScriptsUpdate(user_script_master->GetSharedMemory()); } +void BrowserRenderProcessHost::InitExtensions() { + // TODO(aa): Should only bother sending these function names if this is an + // extension process. + std::vector<std::string> function_names; + ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names); + Send(new ViewMsg_Extension_SetFunctionNames(function_names)); +} + void BrowserRenderProcessHost::SendUserScriptsUpdate( base::SharedMemory *shared_memory) { base::SharedMemoryHandle handle_for_process; diff --git a/chrome/browser/renderer_host/browser_render_process_host.h b/chrome/browser/renderer_host/browser_render_process_host.h index d6f821e..c6517b8 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.h +++ b/chrome/browser/renderer_host/browser_render_process_host.h @@ -109,6 +109,10 @@ class BrowserRenderProcessHost : public RenderProcessHost, // set of scripts and listen for updates to scripts. void InitUserScripts(); + // Initialize support for extension APIs. Send the list of registered API + // functions to thre renderer process. + void InitExtensions(); + // Sends the renderer process a new set of user scripts. void SendUserScriptsUpdate(base::SharedMemory* shared_memory); diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index 35582d4..8222ec3 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -100,7 +100,7 @@ RenderViewHost::RenderViewHost(SiteInstance* instance, has_unload_listener_(false), is_waiting_for_unload_ack_(false), are_javascript_messages_suppressed_(false), - ALLOW_THIS_IN_INITIALIZER_LIST(extension_api_handler_(this)) { + ALLOW_THIS_IN_INITIALIZER_LIST(extension_function_dispatcher_(this)) { DCHECK(instance_); DCHECK(delegate_); if (modal_dialog_event == NULL) @@ -1354,7 +1354,7 @@ void RenderViewHost::OnExtensionRequest(const std::string& name, int callback_id) { // TODO(aa): Here is where we can check that this renderer was supposed to be // able to call extension APIs. - extension_api_handler_.HandleRequest(name, args, callback_id); + extension_function_dispatcher_.HandleRequest(name, args, callback_id); } void RenderViewHost::SendExtensionResponse(int callback_id, diff --git a/chrome/browser/renderer_host/render_view_host.h b/chrome/browser/renderer_host/render_view_host.h index 3142c6c..718c585 100644 --- a/chrome/browser/renderer_host/render_view_host.h +++ b/chrome/browser/renderer_host/render_view_host.h @@ -9,7 +9,7 @@ #include <vector> #include "base/scoped_ptr.h" -#include "chrome/browser/extensions/extension_api_handler.h" +#include "chrome/browser/extensions/extension_function_dispatcher.h" #include "chrome/browser/renderer_host/render_view_host_delegate.h" #include "chrome/browser/renderer_host/render_widget_host.h" #include "chrome/common/modal_dialog_event.h" @@ -641,9 +641,8 @@ class RenderViewHost : public RenderWidgetHost { bool are_javascript_messages_suppressed_; - // Handler for extension API requests. - // Handles processing IPC messages related to the extension system. - ExtensionAPIHandler extension_api_handler_; + // Handles processing IPC messages request extension functions be executed. + ExtensionFunctionDispatcher extension_function_dispatcher_; DISALLOW_EVIL_CONSTRUCTORS(RenderViewHost); }; diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index db1fca9..473bfe2 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -619,14 +619,18 @@ 'browser/encoding_menu_controller_delegate.h', 'browser/extensions/extension.cc', 'browser/extensions/extension.h', - 'browser/extensions/extension_api_handler.cc', - 'browser/extensions/extension_api_handler.h', 'browser/extensions/extension_error_reporter.cc', 'browser/extensions/extension_error_reporter.h', + 'browser/extensions/extension_function.cc', + 'browser/extensions/extension_function.h', + 'browser/extensions/extension_function_dispatcher.cc', + 'browser/extensions/extension_function_dispatcher.h', 'browser/extensions/extension_message_service.cc', 'browser/extensions/extension_message_service.h', 'browser/extensions/extension_protocols.cc', 'browser/extensions/extension_protocols.h', + 'browser/extensions/extension_tabs_module.cc', + 'browser/extensions/extension_tabs_module.h', 'browser/extensions/extension_view.cc', 'browser/extensions/extension_view.h', 'browser/extensions/extensions_service.cc', diff --git a/chrome/common/render_messages_internal.h b/chrome/common/render_messages_internal.h index 27e241e..cc0ad9e 100644 --- a/chrome/common/render_messages_internal.h +++ b/chrome/common/render_messages_internal.h @@ -529,6 +529,10 @@ IPC_BEGIN_MESSAGES(View) std::string /* message */, int /* channel_id */) + // Tell the renderer process all known extension function names. + IPC_MESSAGE_CONTROL1(ViewMsg_Extension_SetFunctionNames, + std::vector<std::string>) + // Changes the text direction of a selected input field. // * direction (int) // Represents the new text direction. diff --git a/chrome/renderer/extensions/extension_process_bindings.cc b/chrome/renderer/extensions/extension_process_bindings.cc index 63c0562..1802a66 100644 --- a/chrome/renderer/extensions/extension_process_bindings.cc +++ b/chrome/renderer/extensions/extension_process_bindings.cc @@ -19,11 +19,19 @@ class ExtensionProcessBindingsWrapper : public v8::Extension { ExtensionProcessBindingsWrapper() : v8::Extension(kExtensionProcessExtensionName, GetSource()) {} + static void SetFunctionNames(const std::vector<std::string>& names) { + function_names_ = new std::set<std::string>(); + for (size_t i = 0; i < names.size(); ++i) { + function_names_->insert(names[i]); + } + } + virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( v8::Handle<v8::String> name) { if (name->Equals(v8::String::New("GetNextCallbackId"))) return v8::FunctionTemplate::New(GetNextCallbackId); - else if (name->Equals(v8::String::New("CreateTab"))) + else if (function_names_->find(*v8::String::AsciiValue(name)) != + function_names_->end()) return v8::FunctionTemplate::New(StartRequest, name); return v8::Handle<v8::FunctionTemplate>(); @@ -53,7 +61,7 @@ class ExtensionProcessBindingsWrapper : public v8::Extension { static v8::Handle<v8::Value> StartRequest(const v8::Arguments& args) { WebFrame* webframe = WebFrame::RetrieveActiveFrame(); DCHECK(webframe) << "There should be an active frame since we just got " - "an API called."; + "a native function called."; if (!webframe) return v8::Undefined(); WebView* webview = webframe->GetView(); @@ -76,9 +84,11 @@ class ExtensionProcessBindingsWrapper : public v8::Extension { } static std::string* source_; + static std::set<std::string>* function_names_; }; std::string* ExtensionProcessBindingsWrapper::source_; +std::set<std::string>* ExtensionProcessBindingsWrapper::function_names_; // static @@ -87,6 +97,12 @@ v8::Extension* ExtensionProcessBindings::Get() { } // static +void ExtensionProcessBindings::SetFunctionNames( + const std::vector<std::string>& names) { + ExtensionProcessBindingsWrapper::SetFunctionNames(names); +} + +// static void ExtensionProcessBindings::ExecuteCallbackInFrame( WebFrame* frame, int callback_id, const std::string& response) { std::string code = "chromium._dispatchCallback("; diff --git a/chrome/renderer/extensions/extension_process_bindings.h b/chrome/renderer/extensions/extension_process_bindings.h index cbdd0bd..53fe17d 100644 --- a/chrome/renderer/extensions/extension_process_bindings.h +++ b/chrome/renderer/extensions/extension_process_bindings.h @@ -8,6 +8,7 @@ #define CHROME_RENDERER_EXTENSIONS_EXTENSION_PROCESS_BINDINGS_H_
#include <string>
+#include <vector>
#include "v8/include/v8.h"
@@ -17,6 +18,7 @@ namespace extensions_v8 { class ExtensionProcessBindings {
public:
+ static void SetFunctionNames(const std::vector<std::string>& names);
static v8::Extension* Get();
static void ExecuteCallbackInFrame(WebFrame* frame, int callback_id,
const std::string& response);
diff --git a/chrome/renderer/render_thread.cc b/chrome/renderer/render_thread.cc index 165af40..01259db 100644 --- a/chrome/renderer/render_thread.cc +++ b/chrome/renderer/render_thread.cc @@ -147,6 +147,11 @@ void RenderThread::OnUpdateUserScripts( user_script_slave_->UpdateScripts(scripts); } +void RenderThread::OnSetExtensionFunctionNames( + const std::vector<std::string>& names) { + extensions_v8::ExtensionProcessBindings::SetFunctionNames(names); +} + void RenderThread::OnControlMessageReceived(const IPC::Message& msg) { IPC_BEGIN_MESSAGE_MAP(RenderThread, msg) IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks) @@ -161,6 +166,8 @@ void RenderThread::OnControlMessageReceived(const IPC::Message& msg) { OnGetCacheResourceStats) IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_NewScripts, OnUpdateUserScripts) + IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames, + OnSetExtensionFunctionNames) IPC_END_MESSAGE_MAP() } diff --git a/chrome/renderer/render_thread.h b/chrome/renderer/render_thread.h index 1f215dd..0c1290c 100644 --- a/chrome/renderer/render_thread.h +++ b/chrome/renderer/render_thread.h @@ -107,6 +107,7 @@ class RenderThread : public RenderThreadBase, void OnUpdateVisitedLinks(base::SharedMemoryHandle table); void OnUpdateUserScripts(base::SharedMemoryHandle table); + void OnSetExtensionFunctionNames(const std::vector<std::string>& names); void OnSetNextPageID(int32 next_page_id); void OnCreateNewView(gfx::NativeViewId parent_hwnd, ModalDialogEvent modal_dialog_event, diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js index 3031d85..b109a76 100644 --- a/chrome/renderer/resources/extension_process_bindings.js +++ b/chrome/renderer/resources/extension_process_bindings.js @@ -64,6 +64,11 @@ var chromium; // Tabs chromium.tabs = {}; + // TODO(aa): This should eventually take an optional windowId param. + chromium.tabs.getTabsForWindow = function(callback) { + native function GetTabsForWindow(); + sendRequest(GetTabsForWindow, null, callback); + }; chromium.tabs.createTab = function(tab, callback) { native function CreateTab(); sendRequest(CreateTab, tab, callback); |