diff options
author | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-06 10:03:41 +0000 |
---|---|---|
committer | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-06 10:03:41 +0000 |
commit | b32260f92786a8d3f7adef89dda53e044806f00c (patch) | |
tree | d42fa50c63221170a498f952cd8364d58701b7c1 /extensions | |
parent | bce0aaf723befbf7c69d3492964c71bd8dbbd87e (diff) | |
download | chromium_src-b32260f92786a8d3f7adef89dda53e044806f00c.zip chromium_src-b32260f92786a8d3f7adef89dda53e044806f00c.tar.gz chromium_src-b32260f92786a8d3f7adef89dda53e044806f00c.tar.bz2 |
Add ApiActivityMonitor to decouple ActivityLog from low-level extensions code
* Introduce ApiActivityMonitor interface and make ActivityLog implement it
* Use it to replace EventRouter's one-off EventDispatchObserver, which was
kind of ugly.
* Use it for function call notifications from ExtensionFunctionDispatcher
This allows app_shell to use ExtensionFunctionDispatcher without an
ActivityLog and gets us another step closer to moving EFD into src/extensions.
BUG=332986,339595
TEST=browser_tests Extension*
Review URL: https://codereview.chromium.org/155183002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249324 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/browser/api_activity_monitor.h | 39 | ||||
-rw-r--r-- | extensions/browser/event_router.cc | 83 | ||||
-rw-r--r-- | extensions/browser/event_router.h | 30 | ||||
-rw-r--r-- | extensions/browser/extensions_browser_client.h | 6 | ||||
-rw-r--r-- | extensions/extensions.gyp | 1 |
5 files changed, 81 insertions, 78 deletions
diff --git a/extensions/browser/api_activity_monitor.h b/extensions/browser/api_activity_monitor.h new file mode 100644 index 0000000..b8f0cc3 --- /dev/null +++ b/extensions/browser/api_activity_monitor.h @@ -0,0 +1,39 @@ +// Copyright 2014 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 EXTENSIONS_BROWSER_API_ACTIVITY_MONITOR_H_ +#define EXTENSIONS_BROWSER_API_ACTIVITY_MONITOR_H_ + +#include <string> + +#include "base/memory/scoped_ptr.h" + +namespace base { +class ListValue; +} + +namespace extensions { + +// ApiActivityMonitor is used to monitor extension API event dispatch and API +// function calls. An embedder can use this interface to log low-level extension +// activity. +class ApiActivityMonitor { + public: + // Called when an API event is dispatched to an extension. + virtual void OnApiEventDispatched(const std::string& extension_id, + const std::string& event_name, + scoped_ptr<base::ListValue> event_args) = 0; + + // Called when an extension calls an API function. + virtual void OnApiFunctionCalled(const std::string& extension_id, + const std::string& api_name, + scoped_ptr<base::ListValue> args) = 0; + + protected: + virtual ~ApiActivityMonitor() {} +}; + +} // namespace extensions + +#endif // EXTENSIONS_BROWSER_API_ACTIVITY_MONITOR_H_ diff --git a/extensions/browser/event_router.cc b/extensions/browser/event_router.cc index 29661be..d145059 100644 --- a/extensions/browser/event_router.cc +++ b/extensions/browser/event_router.cc @@ -17,6 +17,7 @@ #include "chrome/common/extensions/extension_messages.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_process_host.h" +#include "extensions/browser/api_activity_monitor.h" #include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_system.h" @@ -45,6 +46,35 @@ void DoNothing(ExtensionHost* host) {} // registered from its lazy background page. const char kFilteredEvents[] = "filtered_events"; +// Sends a notification about an event to the API activity monitor on the +// UI thread. Can be called from any thread. +void NotifyApiEventDispatched(void* browser_context_id, + const std::string& extension_id, + const std::string& event_name, + scoped_ptr<ListValue> args) { + // The ApiActivityMonitor can only be accessed from the UI thread. + if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { + BrowserThread::PostTask( + BrowserThread::UI, + FROM_HERE, + base::Bind(&NotifyApiEventDispatched, + browser_context_id, + extension_id, + event_name, + base::Passed(&args))); + return; + } + + // Notify the ApiActivityMonitor about the event dispatch. + BrowserContext* context = static_cast<BrowserContext*>(browser_context_id); + if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) + return; + ApiActivityMonitor* monitor = + ExtensionsBrowserClient::Get()->GetApiActivityMonitor(context); + if (monitor) + monitor->OnApiEventDispatched(extension_id, event_name, args.Pass()); +} + } // namespace const char EventRouter::kRegisteredEvents[] = "events"; @@ -67,32 +97,6 @@ struct EventRouter::ListenerProcess { }; // static -void EventRouter::NotifyExtensionDispatchObserverOnUIThread( - void* browser_context_id, - scoped_ptr<EventDispatchInfo> details) { - if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { - BrowserThread::PostTask( - BrowserThread::UI, - FROM_HERE, - base::Bind(&NotifyExtensionDispatchObserverOnUIThread, - browser_context_id, base::Passed(&details))); - } else { - BrowserContext* context = - reinterpret_cast<BrowserContext*>(browser_context_id); - if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) - return; - ExtensionSystem* extension_system = ExtensionSystem::Get(context); - EventRouter* event_router = extension_system->event_router(); - if (!event_router) - return; - if (event_router->event_dispatch_observer_) { - event_router->event_dispatch_observer_->OnWillDispatchEvent( - details.Pass()); - } - } -} - -// static void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, void* browser_context_id, const std::string& extension_id, @@ -100,12 +104,10 @@ void EventRouter::DispatchExtensionMessage(IPC::Sender* ipc_sender, ListValue* event_args, UserGestureState user_gesture, const EventFilteringInfo& info) { - NotifyExtensionDispatchObserverOnUIThread( - browser_context_id, - make_scoped_ptr(new EventDispatchInfo( - extension_id, - event_name, - make_scoped_ptr(event_args->DeepCopy())))); + NotifyApiEventDispatched(browser_context_id, + extension_id, + event_name, + make_scoped_ptr(event_args->DeepCopy())); ListValue args; args.Set(0, new base::StringValue(event_name)); @@ -160,8 +162,7 @@ EventRouter::EventRouter(BrowserContext* browser_context, ExtensionPrefs* extension_prefs) : browser_context_(browser_context), extension_prefs_(extension_prefs), - listeners_(this), - event_dispatch_observer_(NULL) { + listeners_(this) { registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, content::NotificationService::AllSources()); registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, @@ -209,11 +210,6 @@ void EventRouter::UnregisterObserver(Observer* observer) { observers_.erase(iters_to_remove[i]); } -void EventRouter::SetEventDispatchObserver(EventDispatchObserver* observer) { - CHECK(!event_dispatch_observer_); - event_dispatch_observer_ = observer; -} - void EventRouter::OnListenerAdded(const EventListener* listener) { const EventListenerInfo details( listener->event_name, @@ -773,13 +769,4 @@ EventListenerInfo::EventListenerInfo(const std::string& event_name, extension_id(extension_id), browser_context(browser_context) {} -EventDispatchInfo::EventDispatchInfo(const std::string& extension_id, - const std::string& event_name, - scoped_ptr<ListValue> event_args) - : extension_id(extension_id), - event_name(event_name), - event_args(event_args.Pass()) {} - -EventDispatchInfo::~EventDispatchInfo() {} - } // namespace extensions diff --git a/extensions/browser/event_router.h b/extensions/browser/event_router.h index a42aa68..467e7f3 100644 --- a/extensions/browser/event_router.h +++ b/extensions/browser/event_router.h @@ -68,13 +68,6 @@ class EventRouter : public content::NotificationObserver, virtual void OnListenerRemoved(const EventListenerInfo& details) {} }; - // The EventDispatchObserver is notified on the UI thread whenever - // an event is dispatched. There can be only one EventDispatchObserver. - class EventDispatchObserver { - public: - virtual void OnWillDispatchEvent(scoped_ptr<EventDispatchInfo> details) = 0; - }; - // Converts event names like "foo.onBar/123" into "foo.onBar". Event names // without a "/" are returned unchanged. static std::string GetBaseEventName(const std::string& full_event_name); @@ -117,10 +110,6 @@ class EventRouter : public content::NotificationObserver, // Unregisters an observer from all events. void UnregisterObserver(Observer* observer); - // Sets the observer to be notified whenever an event is dispatched to an - // extension. - void SetEventDispatchObserver(EventDispatchObserver* observer); - // Add or remove the extension as having a lazy background page that listens // to the event. The difference from the above methods is that these will be // remembered even after the process goes away. We use this list to decide @@ -192,12 +181,6 @@ class EventRouter : public content::NotificationObserver, typedef std::pair<const content::BrowserContext*, std::string> EventDispatchIdentifier; - // Sends a notification about an event to the event dispatch observer on the - // UI thread. Can be called from any thread. - static void NotifyExtensionDispatchObserverOnUIThread( - void* browser_context_id, - scoped_ptr<EventDispatchInfo> details); - // TODO(gdk): Document this. static void DispatchExtensionMessage( IPC::Sender* ipc_sender, @@ -300,8 +283,6 @@ class EventRouter : public content::NotificationObserver, typedef base::hash_map<std::string, Observer*> ObserverMap; ObserverMap observers_; - EventDispatchObserver* event_dispatch_observer_; - DISALLOW_COPY_AND_ASSIGN(EventRouter); }; @@ -377,17 +358,6 @@ struct EventListenerInfo { content::BrowserContext* browser_context; }; -struct EventDispatchInfo { - EventDispatchInfo(const std::string& extension_id, - const std::string& event_name, - scoped_ptr<base::ListValue> event_args); - ~EventDispatchInfo(); - - const std::string extension_id; - const std::string event_name; - scoped_ptr<base::ListValue> event_args; -}; - } // namespace extensions #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ diff --git a/extensions/browser/extensions_browser_client.h b/extensions/browser/extensions_browser_client.h index ee7fde3..7cff76d 100644 --- a/extensions/browser/extensions_browser_client.h +++ b/extensions/browser/extensions_browser_client.h @@ -21,6 +21,7 @@ class WebContents; namespace extensions { +class ApiActivityMonitor; class AppSorting; class ExtensionSystem; @@ -96,6 +97,11 @@ class ExtensionsBrowserClient { // does not support JavaScript dialogs. virtual content::JavaScriptDialogManager* GetJavaScriptDialogManager() = 0; + // Returns the embedder's ApiActivityMonitor for |context|. Returns NULL if + // the embedder does not monitor extension API activity. + virtual ApiActivityMonitor* GetApiActivityMonitor( + content::BrowserContext* context) = 0; + // Returns the dependencies of ExtensionSystem. May return an empty list. virtual std::vector<BrowserContextKeyedServiceFactory*> GetExtensionSystemDependencies() = 0; diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index 45ef054..8fa8921 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -167,6 +167,7 @@ 'sources': [ 'browser/admin_policy.cc', 'browser/admin_policy.h', + 'browser/api_activity_monitor.h', 'browser/app_sorting.h', 'browser/blacklist_state.h', 'browser/error_map.cc', |