diff options
15 files changed, 258 insertions, 1 deletions
diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index c5248cf..ba85956 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -23,6 +23,7 @@ #include "chrome/browser/extensions/extension_page_actions_module_constants.h" #include "chrome/browser/extensions/extension_popup_api.h" #include "chrome/browser/extensions/extension_process_manager.h" +#include "chrome/browser/extensions/extension_processes_api.h" #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/extensions/extension_tabs_module_constants.h" #include "chrome/browser/extensions/extension_test_api.h" @@ -159,6 +160,9 @@ void FactoryRegistry::ResetFunctions() { // Popup API. RegisterFunction<PopupShowFunction>(); + // Processes. + RegisterFunction<GetProcessForTabFunction>(); + // Test. RegisterFunction<ExtensionTestPassFunction>(); RegisterFunction<ExtensionTestFailFunction>(); diff --git a/chrome/browser/extensions/extension_processes_api.cc b/chrome/browser/extensions/extension_processes_api.cc new file mode 100644 index 0000000..3330cdd --- /dev/null +++ b/chrome/browser/extensions/extension_processes_api.cc @@ -0,0 +1,34 @@ +// Copyright (c) 2010 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_processes_api.h" + +#include "chrome/browser/extensions/extension_tabs_module.h" +#include "chrome/browser/extensions/extension_processes_api_constants.h" +#include "chrome/browser/renderer_host/render_process_host.h" +#include "chrome/browser/tab_contents/tab_contents.h" + +namespace keys = extension_processes_api_constants; + +DictionaryValue* CreateProcessValue(int process_id) { + DictionaryValue* result = new DictionaryValue(); + result->SetInteger(keys::kIdKey, process_id); + + return result; +} + +bool GetProcessForTabFunction::RunImpl() { + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&tab_id)); + + TabContents* contents = NULL; + int tab_index = -1; + if (!ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, + &contents, &tab_index)) + return false; + + int process_id = contents->process()->id(); + result_.reset(CreateProcessValue(process_id)); + return true; +} diff --git a/chrome/browser/extensions/extension_processes_api.h b/chrome/browser/extensions/extension_processes_api.h new file mode 100644 index 0000000..08b983b --- /dev/null +++ b/chrome/browser/extensions/extension_processes_api.h @@ -0,0 +1,18 @@ +// Copyright (c) 2010 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_PROCESSES_API_H__ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ + +#include "chrome/browser/extensions/extension_function.h" + +// This extension function returns the Process object for the renderer process +// currently in use by the specified Tab. +class GetProcessForTabFunction : public SyncExtensionFunction { + virtual ~GetProcessForTabFunction() {} + virtual bool RunImpl(); + DECLARE_EXTENSION_FUNCTION_NAME("experimental.processes.getProcessForTab") +}; + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_H__ diff --git a/chrome/browser/extensions/extension_processes_api_constants.cc b/chrome/browser/extensions/extension_processes_api_constants.cc new file mode 100644 index 0000000..20d0dd3 --- /dev/null +++ b/chrome/browser/extensions/extension_processes_api_constants.cc @@ -0,0 +1,11 @@ +// Copyright (c) 2010 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_processes_api_constants.h" + +namespace extension_processes_api_constants { + +const wchar_t kIdKey[] = L"id"; + +} // namespace extension_processes_api_constants diff --git a/chrome/browser/extensions/extension_processes_api_constants.h b/chrome/browser/extensions/extension_processes_api_constants.h new file mode 100644 index 0000000..ae25b63 --- /dev/null +++ b/chrome/browser/extensions/extension_processes_api_constants.h @@ -0,0 +1,17 @@ +// Copyright (c) 2010 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. + +// Constants used for the Processes API. + +#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_CONSTANTS_H_ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_CONSTANTS_H_ + +namespace extension_processes_api_constants { + +// Keys used in serializing tab data & events. +extern const wchar_t kIdKey[]; + +}; // namespace extension_processes_api_constants + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESSES_API_CONSTANTS_H_ diff --git a/chrome/browser/extensions/extension_processes_apitest.cc b/chrome/browser/extensions/extension_processes_apitest.cc new file mode 100644 index 0000000..f5531d0 --- /dev/null +++ b/chrome/browser/extensions/extension_processes_apitest.cc @@ -0,0 +1,14 @@ +// Copyright (c) 2010 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 "base/command_line.h" +#include "chrome/browser/extensions/extension_apitest.h" +#include "chrome/common/chrome_switches.h" + +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Processes) { + CommandLine::ForCurrentProcess()->AppendSwitch( + switches::kEnableExperimentalExtensionApis); + + ASSERT_TRUE(RunExtensionTest("processes")) << message_; +} diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 4b62edd..cb6fbbb 100755 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -766,6 +766,10 @@ 'browser/extensions/extension_prefs.h', 'browser/extensions/extension_process_manager.cc', 'browser/extensions/extension_process_manager.h', + 'browser/extensions/extension_processes_api.cc', + 'browser/extensions/extension_processes_api.h', + 'browser/extensions/extension_processes_api_constants.cc', + 'browser/extensions/extension_processes_api_constants.h', 'browser/extensions/extension_protocols.cc', 'browser/extensions/extension_protocols.h', 'browser/extensions/extension_shelf_model.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index c6441df..89df02a 100755 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1111,6 +1111,7 @@ 'browser/extensions/extension_management_tests.cc', 'browser/extensions/extension_messages_apitest.cc', 'browser/extensions/extension_override_apitest.cc', + 'browser/extensions/extension_processes_apitest.cc', 'browser/extensions/extension_toolstrip_apitest.cc', 'browser/extensions/incognito_noscript_apitest.cc', 'browser/extensions/isolated_world_apitest.cc', diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index e1f0d14..ab9de52 100755 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -1876,6 +1876,50 @@ ] }, { + "namespace": "experimental.processes", + "nodoc": true, + "types": [ + { + "id": "Process", + "type": "object", + "description": "An object containing information about one of the browser's processes.", + "properties": { + "id": { + "type": "integer", + "description": "The internal ID of the process." + } + } + } + ], + "functions": [ + { + "name": "getProcessForTab", + "type": "function", + "description": "Returns details about the current renderer process of the specified tab.", + "nodoc": "true", + "parameters": [ + { + "name": "tabId", + "type": "integer", + "minimum": 0 + }, + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "process", + "$ref": "Process", + "description": "Details about the tab's process." + } + ] + } + ] + } + ], + "events": [] + }, + { "namespace": "test", "types": [], "functions": [ diff --git a/chrome/renderer/resources/renderer_extension_bindings.js b/chrome/renderer/resources/renderer_extension_bindings.js index 870d43e..13122c5 100644 --- a/chrome/renderer/resources/renderer_extension_bindings.js +++ b/chrome/renderer/resources/renderer_extension_bindings.js @@ -243,7 +243,8 @@ var chrome = chrome || {}; // Entire namespaces. "bookmarks", "browserAction", "devtools", "experimental.bookmarkManager", "experimental.extension", "experimental.history", "experimental.popup", - "pageAction", "pageActions", "tabs", "test", "toolstrip", "windows", + "experimental.processes", "pageAction", "pageActions", "tabs", "test", + "toolstrip", "windows", // Functions/events/properties within the extension namespace. "extension.getBackgroundPage", "extension.getExtensionTabs", diff --git a/chrome/test/data/extensions/api_test/processes/a.html b/chrome/test/data/extensions/api_test/processes/a.html new file mode 100644 index 0000000..dcd442e --- /dev/null +++ b/chrome/test/data/extensions/api_test/processes/a.html @@ -0,0 +1,5 @@ +<html> + <body> + <h1>A</h1> + </body> +</html>
\ No newline at end of file diff --git a/chrome/test/data/extensions/api_test/processes/b.html b/chrome/test/data/extensions/api_test/processes/b.html new file mode 100644 index 0000000..7bff50a --- /dev/null +++ b/chrome/test/data/extensions/api_test/processes/b.html @@ -0,0 +1,5 @@ +<html> + <body> + <h1>B</h1> + </body> +</html>
\ No newline at end of file diff --git a/chrome/test/data/extensions/api_test/processes/manifest.json b/chrome/test/data/extensions/api_test/processes/manifest.json new file mode 100644 index 0000000..44fcdfb --- /dev/null +++ b/chrome/test/data/extensions/api_test/processes/manifest.json @@ -0,0 +1,7 @@ +{ + "name": "chrome.experimental.processes", + "version": "0.1", + "description": "end-to-end browser test for chrome.experimental.processes API", + "background_page": "test.html", + "permissions": ["experimental", "tabs"] +} diff --git a/chrome/test/data/extensions/api_test/processes/test.html b/chrome/test/data/extensions/api_test/processes/test.html new file mode 100644 index 0000000..46f4d74 --- /dev/null +++ b/chrome/test/data/extensions/api_test/processes/test.html @@ -0,0 +1 @@ +<script src="test.js"></script> diff --git a/chrome/test/data/extensions/api_test/processes/test.js b/chrome/test/data/extensions/api_test/processes/test.js new file mode 100644 index 0000000..ecdcaf8 --- /dev/null +++ b/chrome/test/data/extensions/api_test/processes/test.js @@ -0,0 +1,91 @@ +// Processes API test for Chrome. +// browser_tests.exe --gtest_filter=ExtensionApiTest.Processes + +var pass = chrome.test.callbackPass; +var fail = chrome.test.callbackFail; +var assertEq = chrome.test.assertEq; +var assertTrue = chrome.test.assertTrue; + +var tabs = []; + +function createTab(index, url) { + chrome.tabs.create({"url": url}, pass(function(tab) { + tabs[index] = tab; + })); +} + +var getProcess = chrome.experimental.processes.getProcessForTab; + +function pageUrl(letter) { + return chrome.extension.getURL(letter + ".html"); +} + +chrome.test.runTests([ + function setupProcessTests() { + // Open 4 tabs for test, then wait and create a 5th + createTab(0, "about:blank"); + createTab(1, pageUrl("a")); + createTab(2, pageUrl("b")); + createTab(3, "chrome://newtab/"); + + // Wait for all loads to complete. + var completedCount = 0; + var onUpdatedCompleted = chrome.test.listenForever( + chrome.tabs.onUpdated, + function(changedTabId, changeInfo, changedTab) { + if (changedTab.status == "complete") { + completedCount++; + + // Once the NTP finishes loading, create another one. This ensures + // both NTPs end up in the same process. + if (changedTabId == tabs[3].id) { + createTab(4, "chrome://newtab/"); + } + } + + // Once all tabs are done loading, continue with the next test. + if (completedCount == 4) { + onUpdatedCompleted(); + } + } + ); + + }, + + function extensionPageInOwnProcess() { + getProcess(tabs[0].id, pass(function(process0) { + getProcess(tabs[1].id, pass(function(process1) { + // about:blank and extension page should not share a process + assertTrue(process0.id != process1.id); + })); + })); + }, + + function extensionPagesShareProcess() { + getProcess(tabs[1].id, pass(function(process1) { + getProcess(tabs[2].id, pass(function(process2) { + // Pages from same extension should share a process + assertEq(process1.id, process2.id); + })); + })); + }, + + function newTabPageInOwnProcess() { + getProcess(tabs[0].id, pass(function(process0) { + getProcess(tabs[3].id, pass(function(process3) { + // NTP should not share a process with current tabs + assertTrue(process0.id != process3.id); + })); + })); + }, + + function newTabPagesShareProcess() { + getProcess(tabs[3].id, pass(function(process3) { + getProcess(tabs[4].id, pass(function(process4) { + // Multiple NTPs should share a process + assertEq(process3.id, process4.id); + })); + })); + }, + +]); |