diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-08 09:37:23 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-08 09:37:23 +0000 |
commit | 9220f6c63876425d2a8925e2eeb755064f5ba0d9 (patch) | |
tree | 43dd17582732d884926425efa9e77ffd22babac8 | |
parent | feeac1b5044abf164fee557f7853a5dcf96a0a6e (diff) | |
download | chromium_src-9220f6c63876425d2a8925e2eeb755064f5ba0d9.zip chromium_src-9220f6c63876425d2a8925e2eeb755064f5ba0d9.tar.gz chromium_src-9220f6c63876425d2a8925e2eeb755064f5ba0d9.tar.bz2 |
Implement the webNavigation.onCommitted event.
BUG=50943
TEST=ExtensionApiTest.WebNavigationEvents
Review URL: http://codereview.chromium.org/3307013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58802 0039d316-1c4b-4281-b951-d872f2087c98
20 files changed, 326 insertions, 4 deletions
diff --git a/chrome/browser/extensions/extension_webnavigation_api.cc b/chrome/browser/extensions/extension_webnavigation_api.cc new file mode 100644 index 0000000..4a545fd --- /dev/null +++ b/chrome/browser/extensions/extension_webnavigation_api.cc @@ -0,0 +1,85 @@ +// 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. + +// Implements the Chrome Extensions WebNavigation API. + +#include "chrome/browser/extensions/extension_webnavigation_api.h" + +#include "base/json/json_writer.h" +#include "base/time.h" +#include "base/values.h" +#include "chrome/browser/extensions/extension_message_service.h" +#include "chrome/browser/extensions/extension_tabs_module.h" +#include "chrome/browser/extensions/extension_webnavigation_api_constants.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/tab_contents/navigation_entry.h" +#include "chrome/common/notification_type.h" +#include "chrome/common/notification_service.h" + +namespace keys = extension_webnavigation_api_constants; + +// static +ExtensionWebNavigationEventRouter* +ExtensionWebNavigationEventRouter::GetInstance() { + return Singleton<ExtensionWebNavigationEventRouter>::get(); +} + +void ExtensionWebNavigationEventRouter::Init() { + if (registrar_.IsEmpty()) { + registrar_.Add(this, + NotificationType::NAV_ENTRY_COMMITTED, + NotificationService::AllSources()); + } +} + +void ExtensionWebNavigationEventRouter::Observe( + NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + switch (type.value) { + case NotificationType::NAV_ENTRY_COMMITTED: + NavEntryCommitted( + Source<NavigationController>(source).ptr(), + Details<NavigationController::LoadCommittedDetails>(details).ptr()); + break; + + default: + NOTREACHED(); + } +} + +void ExtensionWebNavigationEventRouter::NavEntryCommitted( + NavigationController* controller, + NavigationController::LoadCommittedDetails* details) { + ListValue args; + DictionaryValue* dict = new DictionaryValue(); + dict->SetInteger(keys::kTabIdKey, + ExtensionTabUtil::GetTabId(controller->tab_contents())); + dict->SetString(keys::kUrlKey, + details->entry->url().spec()); + dict->SetInteger(keys::kFrameIdKey, + details->is_main_frame ? 0 : details->entry->page_id()); + dict->SetString(keys::kTransitionTypeKey, + PageTransition::CoreTransitionString( + details->entry->transition_type())); + dict->SetString(keys::kTransitionQualifiersKey, + PageTransition::QualifierString( + details->entry->transition_type())); + dict->SetReal(keys::kTimeStampKey, base::Time::Now().ToDoubleT()); + args.Append(dict); + + std::string json_args; + base::JSONWriter::Write(&args, false, &json_args); + DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); +} + +void ExtensionWebNavigationEventRouter::DispatchEvent( + Profile* profile, + const char* event_name, + const std::string& json_args) { + if (profile && profile->GetExtensionMessageService()) { + profile->GetExtensionMessageService()->DispatchEventToRenderers( + event_name, json_args, profile, GURL()); + } +} diff --git a/chrome/browser/extensions/extension_webnavigation_api.h b/chrome/browser/extensions/extension_webnavigation_api.h new file mode 100644 index 0000000..77182e1 --- /dev/null +++ b/chrome/browser/extensions/extension_webnavigation_api.h @@ -0,0 +1,56 @@ +// 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. + +// Defines the Chrome Extensions WebNavigation API functions for observing and +// intercepting navigation events, as specified in +// chrome/common/extensions/api/extension_api.json. + +#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ +#define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ +#pragma once + +#include "base/singleton.h" +#include "chrome/browser/extensions/extension_function.h" +#include "chrome/browser/tab_contents/navigation_controller.h" +#include "chrome/common/notification_registrar.h" +#include "googleurl/src/gurl.h" + +// Observes navigation notifications and routes them as events to the extension +// system. +class ExtensionWebNavigationEventRouter : public NotificationObserver { + public: + // Single instance of the event router. + static ExtensionWebNavigationEventRouter* GetInstance(); + + void Init(); + + private: + friend struct DefaultSingletonTraits<ExtensionWebNavigationEventRouter>; + + ExtensionWebNavigationEventRouter() {} + virtual ~ExtensionWebNavigationEventRouter() {} + + // NotificationObserver implementation. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + // Handler for the NAV_ENTRY_COMMITTED event. The method takes the details of + // such an event and constructs a suitable JSON formatted extension event + // from it. + void NavEntryCommitted(NavigationController* controller, + NavigationController::LoadCommittedDetails* details); + + // This method dispatches events to the extension message service. + void DispatchEvent(Profile* context, + const char* event_name, + const std::string& json_args); + + // Used for tracking registrations to navigation notifications. + NotificationRegistrar registrar_; + + DISALLOW_COPY_AND_ASSIGN(ExtensionWebNavigationEventRouter); +}; + +#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBNAVIGATION_API_H_ diff --git a/chrome/browser/extensions/extension_webnavigation_apitest.cc b/chrome/browser/extensions/extension_webnavigation_apitest.cc index b0ccd77..087a8b8a 100644 --- a/chrome/browser/extensions/extension_webnavigation_apitest.cc +++ b/chrome/browser/extensions/extension_webnavigation_apitest.cc @@ -12,3 +12,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigation) { ASSERT_TRUE(RunExtensionTest("webnavigation/api")) << message_; } + +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebNavigationEvents) { + CommandLine::ForCurrentProcess()->AppendSwitch( + switches::kEnableExperimentalExtensionApis); + + ASSERT_TRUE(RunExtensionTest("webnavigation/navigation")) << message_; +} diff --git a/chrome/browser/extensions/extensions_service.cc b/chrome/browser/extensions/extensions_service.cc index 06198387..9a59114 100644 --- a/chrome/browser/extensions/extensions_service.cc +++ b/chrome/browser/extensions/extensions_service.cc @@ -34,6 +34,7 @@ #include "chrome/browser/extensions/extension_management_api.h" #include "chrome/browser/extensions/extension_process_manager.h" #include "chrome/browser/extensions/extension_updater.h" +#include "chrome/browser/extensions/extension_webnavigation_api.h" #include "chrome/browser/extensions/external_extension_provider.h" #include "chrome/browser/extensions/external_pref_extension_provider.h" #include "chrome/browser/net/chrome_url_request_context.h" @@ -251,6 +252,7 @@ void ExtensionsService::InitEventRouters() { profile_->GetBookmarkModel()); ExtensionCookiesEventRouter::GetInstance()->Init(); ExtensionManagementEventRouter::GetInstance()->Init(); + ExtensionWebNavigationEventRouter::GetInstance()->Init(); } void ExtensionsService::Init() { diff --git a/chrome/browser/tab_contents/navigation_controller.cc b/chrome/browser/tab_contents/navigation_controller.cc index a416bc9..0aa86f2 100644 --- a/chrome/browser/tab_contents/navigation_controller.cc +++ b/chrome/browser/tab_contents/navigation_controller.cc @@ -352,6 +352,9 @@ void NavigationController::GoBack() { DiscardNonCommittedEntries(); pending_entry_index_ = current_index - 1; + entries_[pending_entry_index_]->set_transition_type( + entries_[pending_entry_index_]->transition_type() | + PageTransition::FORWARD_BACK); NavigateToPendingEntry(NO_RELOAD); } @@ -382,6 +385,9 @@ void NavigationController::GoForward() { if (!transient) pending_entry_index_++; + entries_[pending_entry_index_]->set_transition_type( + entries_[pending_entry_index_]->transition_type() | + PageTransition::FORWARD_BACK); NavigateToPendingEntry(NO_RELOAD); } @@ -420,6 +426,9 @@ void NavigationController::GoToIndex(int index) { DiscardNonCommittedEntries(); pending_entry_index_ = index; + entries_[pending_entry_index_]->set_transition_type( + entries_[pending_entry_index_]->transition_type() | + PageTransition::FORWARD_BACK); NavigateToPendingEntry(NO_RELOAD); } diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index f864796..ecae835 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1464,6 +1464,8 @@ 'browser/extensions/extension_updater.h', 'browser/extensions/extension_webrequest_api_constants.cc', 'browser/extensions/extension_webrequest_api_constants.h', + 'browser/extensions/extension_webnavigation_api.cc', + 'browser/extensions/extension_webnavigation_api.h', 'browser/extensions/extension_webnavigation_api_constants.cc', 'browser/extensions/extension_webnavigation_api_constants.h', 'browser/extensions/extension_webstore_private_api.cc', diff --git a/chrome/common/extensions/api/extension_api.json b/chrome/common/extensions/api/extension_api.json index 0ca4808..4727108 100644 --- a/chrome/common/extensions/api/extension_api.json +++ b/chrome/common/extensions/api/extension_api.json @@ -3192,7 +3192,7 @@ "url": {"type": "string"}, "frameId": {"type": "integer", "description": "0 indicates the navigation happens in the tab content window; positive value indicates navigation in a subframe."}, "transitionType": {"type": "string", "enum": ["link", "typed", "auto_bookmark", "auto_subframe", "manual_subframe", "generated", "start_page", "form_submit", "reload", "keyword", "keyword_generated"], "description": "Cause of the navigation. The same transition types as defined in the history API are used."}, - "transitionQualifiers": {"type": "string", "enum": ["client_redirect", "server_redirect", "forward_back"], "description": "Zero or more transition qualifiers delimited by \"|\". Possible qualifiers are \"client_redirect\", \"server_redirect\", and \"forward_backward\"."}, + "transitionQualifiers": {"type": "string", "description": "Zero or more transition qualifiers delimited by \"|\". Possible qualifiers are \"client_redirect\", \"server_redirect\", and \"forward_back\"."}, "timeStamp": {"type": "number", "description": "The time when the navigation was committed, in milliseconds since the epoch."} } } diff --git a/chrome/common/extensions/docs/experimental.webNavigation.html b/chrome/common/extensions/docs/experimental.webNavigation.html index ee897d5..b71aac33 100644 --- a/chrome/common/extensions/docs/experimental.webNavigation.html +++ b/chrome/common/extensions/docs/experimental.webNavigation.html @@ -1392,7 +1392,7 @@ <div style="display:inline"> ( <span class="optional" style="display: none; ">optional</span> - <span class="enum">enumerated</span> + <span class="enum" style="display: none; ">enumerated</span> <span id="typeTemplate"> <span style="display: none; "> <a> Type</a> @@ -1402,7 +1402,7 @@ array of <span><span></span></span> </span> <span>string</span> - <span>["client_redirect", "server_redirect", "forward_back"]</span> + <span style="display: none; "></span> </span> </span> ) @@ -1413,7 +1413,7 @@ <dd class="todo" style="display: none; "> Undocumented. </dd> - <dd>Zero or more transition qualifiers delimited by "|". Possible qualifiers are "client_redirect", "server_redirect", and "forward_backward".</dd> + <dd>Zero or more transition qualifiers delimited by "|". Possible qualifiers are "client_redirect", "server_redirect", and "forward_back".</dd> <dd style="display: none; "> This parameter was added in version <b><span></span></b>. diff --git a/chrome/common/page_transition_types.cc b/chrome/common/page_transition_types.cc index bf686f9..1079ea0 100644 --- a/chrome/common/page_transition_types.cc +++ b/chrome/common/page_transition_types.cc @@ -34,3 +34,20 @@ const char* PageTransition::CoreTransitionString(Type type) { } return NULL; } + +// static +const char* PageTransition::QualifierString(Type type) { + DCHECK_NE((int)(type & (CLIENT_REDIRECT | SERVER_REDIRECT)), + (int)(CLIENT_REDIRECT | SERVER_REDIRECT)); + + switch (type & (CLIENT_REDIRECT | SERVER_REDIRECT | FORWARD_BACK)) { + case CLIENT_REDIRECT: return "client_redirect"; + case SERVER_REDIRECT: return "server_redirect"; + case FORWARD_BACK: return "forward_back"; + case (CLIENT_REDIRECT | FORWARD_BACK): + return "client_redirect|forward_back"; + case (SERVER_REDIRECT | FORWARD_BACK): + return "server_redirect|forward_back"; + } + return ""; +} diff --git a/chrome/common/page_transition_types.h b/chrome/common/page_transition_types.h index 71cd386..8ca7042 100644 --- a/chrome/common/page_transition_types.h +++ b/chrome/common/page_transition_types.h @@ -102,6 +102,9 @@ class PageTransition { // Any of the core values above can be augmented by one or more qualifiers. // These qualifiers further define the transition. + // User used the Forward or Back button to navigate among browsing history. + FORWARD_BACK = 0x01000000, + // The beginning of a navigation chain. CHAIN_START = 0x10000000, @@ -157,6 +160,9 @@ class PageTransition { // Return a string version of the core type values. static const char* CoreTransitionString(Type type); + + // Return a string version of the qualifier type values. + static const char* QualifierString(Type type); }; #endif // CHROME_COMMON_PAGE_TRANSITION_TYPES_H__ diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/clientRedirect/a.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/clientRedirect/a.html new file mode 100644 index 0000000..3e3e11a --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/clientRedirect/a.html @@ -0,0 +1,3 @@ +<script> + window.setTimeout('document.location = "b.html";', 500); +</script> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/clientRedirect/b.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/clientRedirect/b.html new file mode 100644 index 0000000..18ecdcb --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/clientRedirect/b.html @@ -0,0 +1 @@ +<html></html> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/forwardBack/a.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/forwardBack/a.html new file mode 100644 index 0000000..18ecdcb --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/forwardBack/a.html @@ -0,0 +1 @@ +<html></html> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/forwardBack/b.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/forwardBack/b.html new file mode 100644 index 0000000..a4270ed --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/forwardBack/b.html @@ -0,0 +1,3 @@ +<script> + window.setTimeout('history.back();', 500); +</script> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/a.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/a.html new file mode 100644 index 0000000..c5c157d --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/a.html @@ -0,0 +1 @@ +<html><body><iframe src="b.html"></iframe></body></html> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/b.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/b.html new file mode 100644 index 0000000..d6e86d8 --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/b.html @@ -0,0 +1,3 @@ +<script> + window.setTimeout('document.location = "c.html";', 500); +</script> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/c.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/c.html new file mode 100644 index 0000000..18ecdcb --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/iframe/c.html @@ -0,0 +1 @@ +<html></html> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/manifest.json b/chrome/test/data/extensions/api_test/webnavigation/navigation/manifest.json new file mode 100644 index 0000000..c5538ce --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/manifest.json @@ -0,0 +1,7 @@ +{ + "name": "navigation", + "version": "1.0", + "description": "Tests the webNavigation API events.", + "permissions": ["experimental", "tabs"], + "background_page": "test.html" +} diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/simpleLoad/a.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/simpleLoad/a.html new file mode 100644 index 0000000..18ecdcb --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/simpleLoad/a.html @@ -0,0 +1 @@ +<html></html> diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/test.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/test.html new file mode 100644 index 0000000..6525a28 --- /dev/null +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/test.html @@ -0,0 +1,117 @@ +<script> +var expectedEventData; +var capturedEventData; + +function expect(data) { + expectedEventData = data; + capturedEventData = []; +} + +function checkExpectations() { + if (capturedEventData.length < expectedEventData.length) { + return; + } + chrome.test.assertEq(JSON.stringify(expectedEventData), + JSON.stringify(capturedEventData)); + chrome.test.succeed(); +} + +chrome.experimental.webNavigation.onCommitted.addListener(function(details) { + console.log('---onCommitted: ' + details.url); + // normalize details. + details.timeStamp = 0; + if (details.frameId != 0) { + details.frameId = 1; + } + capturedEventData.push(details); + checkExpectations(); +}); + +var getURL = chrome.extension.getURL; +chrome.tabs.getSelected(null, function(tab) { + var tabId = tab.id; + + chrome.test.runTests([ + /* Navigates to an URL */ + function simpleLoad() { + expect([ + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('simpleLoad/a.html') }]); + chrome.tabs.update(tabId, { url: getURL('simpleLoad/a.html') }); + }, + + /* Navigates to a.html that redirects to b.html (using javascript) + after a delay of 500ms, so the initial navigation is completed and + the redirection is marked as client_redirect */ + function clientRedirect() { + expect([ + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('clientRedirect/a.html') }, + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "client_redirect", + transitionType: "link", + url: getURL('clientRedirect/b.html') }]); + chrome.tabs.update(tabId, { url: getURL('clientRedirect/a.html') }); + }, + + /* First navigates to a.html, and then to b.html which uses + history.back() to navigate back to a.html */ + function forwardBack() { + expect([ + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('forwardBack/a.html') }, + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('forwardBack/b.html') }, + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "forward_back", + transitionType: "link", + url: getURL('forwardBack/a.html') }]); + chrome.tabs.update(tabId, { url: getURL('forwardBack/a.html') }, + function (tab) { + chrome.tabs.update(tabId, + { url: getURL('forwardBack/b.html') }); + }); + }, + + /* Navigates to a.html which includes b.html as an iframe. b.html + redirects to c.html. Note that all navigation entries are for + a.html. Also, b.html does not generate a navigation entry. */ + function iframe() { + expect([ + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('iframe/a.html') }, + { frameId: 1, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('iframe/a.html') }]); + chrome.tabs.update(tabId, { url: getURL('iframe/a.html') }); + }, + ]); +}); +</script> |