diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-17 08:35:22 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-17 08:35:22 +0000 |
commit | a9c0bfe7d2d3f58cb5844c7470578327a55e4536 (patch) | |
tree | b4bb784c9a004f17b51fd1db9478df4543ed812b | |
parent | e35a0ca8a20827e1217ab1e929d17d270a6931bd (diff) | |
download | chromium_src-a9c0bfe7d2d3f58cb5844c7470578327a55e4536.zip chromium_src-a9c0bfe7d2d3f58cb5844c7470578327a55e4536.tar.gz chromium_src-a9c0bfe7d2d3f58cb5844c7470578327a55e4536.tar.bz2 |
Reland r59641. Add the onBeforeNavigate and onErrorOccured events to the webNavigation API.
Also, rewrite the onCommitted event such that AUTO_SUBFRAME events are also registered.
BUG=50943
TEST=WebNavigationEvents
Review URL: http://codereview.chromium.org/3383008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59777 0039d316-1c4b-4281-b951-d872f2087c98
7 files changed, 339 insertions, 99 deletions
diff --git a/chrome/browser/extensions/extension_webnavigation_api.cc b/chrome/browser/extensions/extension_webnavigation_api.cc index 4a545fd..589d5b6 100644 --- a/chrome/browser/extensions/extension_webnavigation_api.cc +++ b/chrome/browser/extensions/extension_webnavigation_api.cc @@ -13,9 +13,11 @@ #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/browser/tab_contents/navigation_controller.h" +#include "chrome/browser/tab_contents/provisional_load_details.h" #include "chrome/common/notification_type.h" #include "chrome/common/notification_service.h" +#include "net/base/net_errors.h" namespace keys = extension_webnavigation_api_constants; @@ -28,7 +30,13 @@ ExtensionWebNavigationEventRouter::GetInstance() { void ExtensionWebNavigationEventRouter::Init() { if (registrar_.IsEmpty()) { registrar_.Add(this, - NotificationType::NAV_ENTRY_COMMITTED, + NotificationType::FRAME_PROVISIONAL_LOAD_START, + NotificationService::AllSources()); + registrar_.Add(this, + NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED, + NotificationService::AllSources()); + registrar_.Add(this, + NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR, NotificationService::AllSources()); } } @@ -38,35 +46,66 @@ void ExtensionWebNavigationEventRouter::Observe( const NotificationSource& source, const NotificationDetails& details) { switch (type.value) { - case NotificationType::NAV_ENTRY_COMMITTED: - NavEntryCommitted( + case NotificationType::FRAME_PROVISIONAL_LOAD_START: + FrameProvisionalLoadStart( + Source<NavigationController>(source).ptr(), + Details<ProvisionalLoadDetails>(details).ptr()); + break; + case NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED: + FrameProvisionalLoadCommitted( + Source<NavigationController>(source).ptr(), + Details<ProvisionalLoadDetails>(details).ptr()); + break; + case NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR: + FailProvisionalLoadWithError( Source<NavigationController>(source).ptr(), - Details<NavigationController::LoadCommittedDetails>(details).ptr()); + Details<ProvisionalLoadDetails>(details).ptr()); break; default: NOTREACHED(); } } +void ExtensionWebNavigationEventRouter::FrameProvisionalLoadStart( + NavigationController* controller, + ProvisionalLoadDetails* details) { + ListValue args; + DictionaryValue* dict = new DictionaryValue(); + dict->SetInteger(keys::kTabIdKey, + ExtensionTabUtil::GetTabId(controller->tab_contents())); + dict->SetString(keys::kUrlKey, + details->url().spec()); + dict->SetInteger(keys::kFrameIdKey, 0); + dict->SetInteger(keys::kRequestIdKey, 0); + dict->SetReal(keys::kTimeStampKey, + static_cast<double>( + (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds())); + args.Append(dict); -void ExtensionWebNavigationEventRouter::NavEntryCommitted( + std::string json_args; + base::JSONWriter::Write(&args, false, &json_args); + DispatchEvent(controller->profile(), keys::kOnBeforeNavigate, json_args); +} + +void ExtensionWebNavigationEventRouter::FrameProvisionalLoadCommitted( NavigationController* controller, - NavigationController::LoadCommittedDetails* details) { + ProvisionalLoadDetails* 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()); + details->url().spec()); + dict->SetInteger(keys::kFrameIdKey, 0); dict->SetString(keys::kTransitionTypeKey, PageTransition::CoreTransitionString( - details->entry->transition_type())); + details->transition_type())); dict->SetString(keys::kTransitionQualifiersKey, PageTransition::QualifierString( - details->entry->transition_type())); - dict->SetReal(keys::kTimeStampKey, base::Time::Now().ToDoubleT()); + details->transition_type())); + dict->SetReal(keys::kTimeStampKey, + static_cast<double>( + (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds())); args.Append(dict); std::string json_args; @@ -74,6 +113,28 @@ void ExtensionWebNavigationEventRouter::NavEntryCommitted( DispatchEvent(controller->profile(), keys::kOnCommitted, json_args); } +void ExtensionWebNavigationEventRouter::FailProvisionalLoadWithError( + NavigationController* controller, + ProvisionalLoadDetails* details) { + ListValue args; + DictionaryValue* dict = new DictionaryValue(); + dict->SetInteger(keys::kTabIdKey, + ExtensionTabUtil::GetTabId(controller->tab_contents())); + dict->SetString(keys::kUrlKey, + details->url().spec()); + dict->SetInteger(keys::kFrameIdKey, 0); + dict->SetString(keys::kErrorKey, + std::string(net::ErrorToString(details->error_code()))); + dict->SetReal(keys::kTimeStampKey, + static_cast<double>( + (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds())); + args.Append(dict); + + std::string json_args; + base::JSONWriter::Write(&args, false, &json_args); + DispatchEvent(controller->profile(), keys::kOnErrorOccurred, json_args); +} + void ExtensionWebNavigationEventRouter::DispatchEvent( Profile* profile, const char* event_name, diff --git a/chrome/browser/extensions/extension_webnavigation_api.h b/chrome/browser/extensions/extension_webnavigation_api.h index 77182e1..95ba032 100644 --- a/chrome/browser/extensions/extension_webnavigation_api.h +++ b/chrome/browser/extensions/extension_webnavigation_api.h @@ -12,10 +12,13 @@ #include "base/singleton.h" #include "chrome/browser/extensions/extension_function.h" -#include "chrome/browser/tab_contents/navigation_controller.h" +#include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" #include "googleurl/src/gurl.h" +class NavigationController; +class ProvisionalLoadDetails; + // Observes navigation notifications and routes them as events to the extension // system. class ExtensionWebNavigationEventRouter : public NotificationObserver { @@ -36,11 +39,23 @@ class ExtensionWebNavigationEventRouter : public NotificationObserver { 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); + // Handler for the FRAME_PROVISIONAL_LOAD_START event. The method takes the + // details of such an event and constructs a suitable JSON formatted extension + // event from it. + void FrameProvisionalLoadStart(NavigationController* controller, + ProvisionalLoadDetails* details); + + // Handler for the FRAME_PROVISIONAL_LOAD_COMMITTED event. The method takes + // the details of such an event and constructs a suitable JSON formatted + // extension event from it. + void FrameProvisionalLoadCommitted(NavigationController* controller, + ProvisionalLoadDetails* details); + + // Handler for the FAIL_PROVISIONAL_LOAD_WITH_ERROR event. The method takes + // the details of such an event and constructs a suitable JSON formatted + // extension event from it. + void FailProvisionalLoadWithError(NavigationController* controller, + ProvisionalLoadDetails* details); // This method dispatches events to the extension message service. void DispatchEvent(Profile* context, diff --git a/chrome/browser/tab_contents/provisional_load_details.cc b/chrome/browser/tab_contents/provisional_load_details.cc index 3bea977..e1e85d2 100644 --- a/chrome/browser/tab_contents/provisional_load_details.cc +++ b/chrome/browser/tab_contents/provisional_load_details.cc @@ -13,6 +13,7 @@ ProvisionalLoadDetails::ProvisionalLoadDetails(bool is_main_frame, const std::string& security_info, bool is_content_filtered) : error_code_(net::OK), + transition_type_(PageTransition::LINK), url_(url), is_main_frame_(is_main_frame), is_in_page_navigation_(is_in_page_navigation), diff --git a/chrome/browser/tab_contents/provisional_load_details.h b/chrome/browser/tab_contents/provisional_load_details.h index 92ac9b1..2073db0 100644 --- a/chrome/browser/tab_contents/provisional_load_details.h +++ b/chrome/browser/tab_contents/provisional_load_details.h @@ -9,6 +9,7 @@ #include <string> #include "base/basictypes.h" +#include "chrome/common/page_transition_types.h" #include "googleurl/src/gurl.h" // This class captures some of the information associated to the provisional @@ -32,6 +33,13 @@ class ProvisionalLoadDetails { void set_error_code(int error_code) { error_code_ = error_code; } int error_code() const { return error_code_; } + void set_transition_type(PageTransition::Type transition_type) { + transition_type_ = transition_type; + } + PageTransition::Type transition_type() const { + return transition_type_; + } + const GURL& url() const { return url_; } bool main_frame() const { return is_main_frame_; } @@ -50,6 +58,7 @@ class ProvisionalLoadDetails { private: int error_code_; + PageTransition::Type transition_type_; GURL url_; bool is_main_frame_; bool is_in_page_navigation_; diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc index 67428ce..1d702bd 100644 --- a/chrome/browser/tab_contents/tab_contents.cc +++ b/chrome/browser/tab_contents/tab_contents.cc @@ -2452,6 +2452,30 @@ void TabContents::DidNavigate(RenderViewHost* rvh, bool did_navigate = controller_.RendererDidNavigate( params, extra_invalidate_flags, &details); + // Send notification about committed provisional loads. This notification is + // different from the NAV_ENTRY_COMMITTED notification which doesn't include + // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations. + if (details.type != NavigationType::NAV_IGNORE) { + ProvisionalLoadDetails load_details(details.is_main_frame, + details.is_in_page, + params.url, std::string(), false); + load_details.set_transition_type(params.transition); + // Whether or not a page transition was triggered by going backward or + // forward in the history is only stored in the navigation controller's + // entry list. + if (did_navigate && + (controller_.GetActiveEntry()->transition_type() & + PageTransition::FORWARD_BACK)) { + load_details.set_transition_type( + params.transition | PageTransition::FORWARD_BACK); + } + NotificationService::current()->Notify( + NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED, + Source<NavigationController>(&controller_), + Details<ProvisionalLoadDetails>(&load_details)); + + } + // Update history. Note that this needs to happen after the entry is complete, // which WillNavigate[Main,Sub]Frame will do before this function is called. if (params.should_update_history) { diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h index fa1febb..f502a00 100644 --- a/chrome/common/notification_type.h +++ b/chrome/common/notification_type.h @@ -103,10 +103,17 @@ class NotificationType { // A frame is staring a provisional load. The source is a // Source<NavigationController> corresponding to the tab in which the load - // occurs. Details is a bool specifying if the load occurs in the main - // frame (or a sub-frame if false). + // occurs. Details is a ProvisionalLoadDetails object. FRAME_PROVISIONAL_LOAD_START, + // The provisional load for a frame was committed. The source is a + // NavigationController corresponding to the tab in which the load occured. + // Details is a ProvisionalLoadDetails object. In contrast to + // NAV_ENTRY_COMMITTED, this notification is sent when the load was + // committed, even if no navigation entry was committed (such as + // AUTO_SUBFRAME navigations). + FRAME_PROVISIONAL_LOAD_COMMITTED, + // Content was loaded from an in-memory cache. The source will be a // Source<NavigationController> corresponding to the tab in which the load // occurred. Details in the form of a LoadFromMemoryCacheDetails object diff --git a/chrome/test/data/extensions/api_test/webnavigation/navigation/test.html b/chrome/test/data/extensions/api_test/webnavigation/navigation/test.html index 4ac358d1..431b4e7 100644 --- a/chrome/test/data/extensions/api_test/webnavigation/navigation/test.html +++ b/chrome/test/data/extensions/api_test/webnavigation/navigation/test.html @@ -16,98 +16,221 @@ function checkExpectations() { chrome.test.succeed(); } +chrome.experimental.webNavigation.onBeforeNavigate.addListener( + function(details) { + console.log('---onBeforeNavigate: ' + details.url); + // normalize details. + details.timeStamp = 0; + if (details.frameId != 0) { + details.frameId = 1; + } + capturedEventData.push(["onBeforeNavigate", details]); + checkExpectations(); +}); + chrome.experimental.webNavigation.onCommitted.addListener(function(details) { - console.log('---onCommitted: ' + details.url); + console.log('---onCommitted: ' + details.url); + // normalize details. + details.timeStamp = 0; + if (details.frameId != 0) { + details.frameId = 1; + } + capturedEventData.push(["onCommitted", details]); + checkExpectations(); +}); + +chrome.experimental.webNavigation.onErrorOccurred.addListener( + function(details) { + console.log('---onErrorOccurred: ' + details.url); // normalize details. details.timeStamp = 0; if (details.frameId != 0) { details.frameId = 1; } - capturedEventData.push(details); + capturedEventData.push(["onErrorOccurred", details]); checkExpectations(); }); var getURL = chrome.extension.getURL; chrome.tabs.getSelected(null, function(tab) { - var tabId = tab.id; + var tabId = tab.id; + + chrome.test.runTests([ + /* Navigates to an URL */ + function simpleLoad() { + expect([ + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('simpleLoad/a.html') }], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('simpleLoad/a.html') }]]); + chrome.tabs.update(tabId, { url: getURL('simpleLoad/a.html') }); + }, - 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([ + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('clientRedirect/a.html') }], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('clientRedirect/a.html') }], + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('clientRedirect/b.html') }], + [ "onCommitted", + { 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') }); + }, - /* 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 which redirects to to b.html which uses + history.back() to navigate back to a.html */ + function forwardBack() { + expect([ + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('forwardBack/a.html') }], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('forwardBack/a.html') }], + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('forwardBack/b.html') }], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "client_redirect", + transitionType: "link", + url: getURL('forwardBack/b.html') }], + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('forwardBack/a.html') }], + [ "onCommitted", + { 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') }); + }, - /* First navigates to a.html which redirects to 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: "client_redirect", - 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') }); - }, + /* Navigates to a.html which includes b.html as an iframe. b.html + redirects to c.html. */ + function iframe() { + expect([ + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('iframe/a.html') }], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('iframe/a.html') }], + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('iframe/b.html') }], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "auto_subframe", + url: getURL('iframe/b.html') }], + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('iframe/c.html') }], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "manual_subframe", + url: getURL('iframe/c.html') }]]); + chrome.tabs.update(tabId, { url: getURL('iframe/a.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') }); - }, - ]); + /* Navigates to a non-existant page. */ + function nonExistant() { + expect([ + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('nonexistant.html') }], + [ "onErrorOccurred", + { error: "net::ERR_FILE_NOT_FOUND", + frameId: 0, + tabId: tabId, + timeStamp: 0, + url: getURL('nonexistant.html') }], + [ "onBeforeNavigate", + { frameId: 0, + requestId: 0, + tabId: tabId, + timeStamp: 0, + url: "chrome://chromewebdata/"}], + [ "onCommitted", + { frameId: 0, + tabId: tabId, + timeStamp: 0, + transitionQualifiers: "", + transitionType: "link", + url: getURL('nonexistant.html') }]]); + chrome.tabs.update(tabId, { url: getURL('nonexistant.html') }); + }, + ]); }); </script> |