diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-08 11:20:36 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-08 11:20:36 +0000 |
commit | f71a3296f11647fe0acfa632daeb5efe852e2cdd (patch) | |
tree | f2ad3dba76836121567d7c39bd732537068824ee /chrome/browser | |
parent | c65f07debbaf0dde8de015591cb6386f0859b1a8 (diff) | |
download | chromium_src-f71a3296f11647fe0acfa632daeb5efe852e2cdd.zip chromium_src-f71a3296f11647fe0acfa632daeb5efe852e2cdd.tar.gz chromium_src-f71a3296f11647fe0acfa632daeb5efe852e2cdd.tar.bz2 |
Implement the webNavigation.onCommitted event.
BUG=50943
TEST=ExtensionApiTest.WebNavigationEvents
Review URL: http://codereview.chromium.org/3369007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58809 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
5 files changed, 159 insertions, 0 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); } |