Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: extensions/browser/event_router.cc

Issue 2943583002: [extension SW] Support lazy events from extension service workers. (Closed)
Patch Set: sync @tott Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/event_router.h ('k') | extensions/browser/events/lazy_event_dispatcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/event_router.h" 5 #include "extensions/browser/event_router.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 content::RenderProcessHost* process, 174 content::RenderProcessHost* process,
175 const std::string& extension_id) { 175 const std::string& extension_id) {
176 listeners_.AddListener( 176 listeners_.AddListener(
177 EventListener::ForExtension(event_name, extension_id, process, nullptr)); 177 EventListener::ForExtension(event_name, extension_id, process, nullptr));
178 } 178 }
179 179
180 void EventRouter::AddServiceWorkerEventListener( 180 void EventRouter::AddServiceWorkerEventListener(
181 const std::string& event_name, 181 const std::string& event_name,
182 content::RenderProcessHost* process, 182 content::RenderProcessHost* process,
183 const ExtensionId& extension_id, 183 const ExtensionId& extension_id,
184 const GURL& service_worker_scope,
184 int worker_thread_id) { 185 int worker_thread_id) {
185 listeners_.AddListener(EventListener::ForExtensionServiceWorker( 186 listeners_.AddListener(EventListener::ForExtensionServiceWorker(
186 event_name, extension_id, process, worker_thread_id, nullptr)); 187 event_name, extension_id, process, service_worker_scope, worker_thread_id,
188 nullptr));
187 } 189 }
188 190
189 void EventRouter::RemoveEventListener(const std::string& event_name, 191 void EventRouter::RemoveEventListener(const std::string& event_name,
190 content::RenderProcessHost* process, 192 content::RenderProcessHost* process,
191 const std::string& extension_id) { 193 const std::string& extension_id) {
192 std::unique_ptr<EventListener> listener = 194 std::unique_ptr<EventListener> listener =
193 EventListener::ForExtension(event_name, extension_id, process, nullptr); 195 EventListener::ForExtension(event_name, extension_id, process, nullptr);
194 listeners_.RemoveListener(listener.get()); 196 listeners_.RemoveListener(listener.get());
195 } 197 }
196 198
197 void EventRouter::RemoveServiceWorkerEventListener( 199 void EventRouter::RemoveServiceWorkerEventListener(
198 const std::string& event_name, 200 const std::string& event_name,
199 content::RenderProcessHost* process, 201 content::RenderProcessHost* process,
200 const ExtensionId& extension_id, 202 const ExtensionId& extension_id,
203 const GURL& service_worker_scope,
201 int worker_thread_id) { 204 int worker_thread_id) {
202 std::unique_ptr<EventListener> listener = 205 std::unique_ptr<EventListener> listener =
203 EventListener::ForExtensionServiceWorker( 206 EventListener::ForExtensionServiceWorker(event_name, extension_id,
204 event_name, extension_id, process, worker_thread_id, nullptr); 207 process, service_worker_scope,
208 worker_thread_id, nullptr);
205 listeners_.RemoveListener(listener.get()); 209 listeners_.RemoveListener(listener.get());
206 } 210 }
207 211
208 void EventRouter::AddEventListenerForURL(const std::string& event_name, 212 void EventRouter::AddEventListenerForURL(const std::string& event_name,
209 content::RenderProcessHost* process, 213 content::RenderProcessHost* process,
210 const GURL& listener_url) { 214 const GURL& listener_url) {
211 listeners_.AddListener( 215 listeners_.AddListener(
212 EventListener::ForURL(event_name, listener_url, process, nullptr)); 216 EventListener::ForURL(event_name, listener_url, process, nullptr));
213 } 217 }
214 218
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 } 278 }
275 279
276 void EventRouter::RenderProcessHostDestroyed(content::RenderProcessHost* host) { 280 void EventRouter::RenderProcessHostDestroyed(content::RenderProcessHost* host) {
277 listeners_.RemoveListenersForProcess(host); 281 listeners_.RemoveListenersForProcess(host);
278 observed_process_set_.erase(host); 282 observed_process_set_.erase(host);
279 host->RemoveObserver(this); 283 host->RemoveObserver(this);
280 } 284 }
281 285
282 void EventRouter::AddLazyEventListener(const std::string& event_name, 286 void EventRouter::AddLazyEventListener(const std::string& event_name,
283 const ExtensionId& extension_id) { 287 const ExtensionId& extension_id) {
284 AddLazyEventListenerImpl(event_name, extension_id, kNonWorkerThreadId); 288 AddLazyEventListenerImpl(
289 EventListener::ForExtension(event_name, extension_id, nullptr, nullptr),
290 RegisteredEventType::kLazy);
285 } 291 }
286 292
287 void EventRouter::RemoveLazyEventListener(const std::string& event_name, 293 void EventRouter::RemoveLazyEventListener(const std::string& event_name,
288 const ExtensionId& extension_id) { 294 const ExtensionId& extension_id) {
289 RemoveLazyEventListenerImpl(event_name, extension_id, kNonWorkerThreadId); 295 RemoveLazyEventListenerImpl(
296 EventListener::ForExtension(event_name, extension_id, nullptr, nullptr),
297 RegisteredEventType::kLazy);
290 } 298 }
291 299
292 void EventRouter::AddLazyServiceWorkerEventListener( 300 void EventRouter::AddLazyServiceWorkerEventListener(
293 const std::string& event_name, 301 const std::string& event_name,
294 const ExtensionId& extension_id, 302 const ExtensionId& extension_id,
295 int worker_thread_id) { 303 const GURL& service_worker_scope) {
296 AddLazyEventListenerImpl(event_name, extension_id, worker_thread_id); 304 std::unique_ptr<EventListener> listener =
305 EventListener::ForExtensionServiceWorker(
306 event_name, extension_id, nullptr, service_worker_scope,
307 kNonWorkerThreadId, // Lazy, without worker thread id.
308 nullptr);
309 AddLazyEventListenerImpl(std::move(listener),
310 RegisteredEventType::kServiceWorker);
297 } 311 }
298 312
299 void EventRouter::RemoveLazyServiceWorkerEventListener( 313 void EventRouter::RemoveLazyServiceWorkerEventListener(
300 const std::string& event_name, 314 const std::string& event_name,
301 const ExtensionId& extension_id, 315 const ExtensionId& extension_id,
302 int worker_thread_id) { 316 const GURL& service_worker_scope) {
303 RemoveLazyEventListenerImpl(event_name, extension_id, worker_thread_id); 317 std::unique_ptr<EventListener> listener =
318 EventListener::ForExtensionServiceWorker(
319 event_name, extension_id, nullptr, service_worker_scope,
320 kNonWorkerThreadId, // Lazy, without worker thread id.
321 nullptr);
322 RemoveLazyEventListenerImpl(std::move(listener),
323 RegisteredEventType::kServiceWorker);
304 } 324 }
305 325
306 // TODO(lazyboy): Support filters for extension SW events. 326 // TODO(lazyboy): Support filters for extension SW events.
307 void EventRouter::AddFilteredEventListener(const std::string& event_name, 327 void EventRouter::AddFilteredEventListener(const std::string& event_name,
308 content::RenderProcessHost* process, 328 content::RenderProcessHost* process,
309 const std::string& extension_id, 329 const std::string& extension_id,
310 const base::DictionaryValue& filter, 330 const base::DictionaryValue& filter,
311 bool add_lazy_listener) { 331 bool add_lazy_listener) {
312 listeners_.AddListener(EventListener::ForExtension( 332 listeners_.AddListener(EventListener::ForExtension(
313 event_name, extension_id, process, 333 event_name, extension_id, process,
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 // so will cause those that are being suspended to cancel that suspension. 475 // so will cause those that are being suspended to cancel that suspension.
456 // As canceling a suspension entails sending an event to the affected 476 // As canceling a suspension entails sending an event to the affected
457 // background page, and as that event needs to be delivered before we dispatch 477 // background page, and as that event needs to be delivered before we dispatch
458 // the event we are dispatching here, we dispatch to the lazy listeners here 478 // the event we are dispatching here, we dispatch to the lazy listeners here
459 // first. 479 // first.
460 for (const EventListener* listener : listeners) { 480 for (const EventListener* listener : listeners) {
461 if (!restrict_to_extension_id.empty() && 481 if (!restrict_to_extension_id.empty() &&
462 restrict_to_extension_id != listener->extension_id()) { 482 restrict_to_extension_id != listener->extension_id()) {
463 continue; 483 continue;
464 } 484 }
465 // TODO(lazyboy): Support lazy listeners for extension SW events. 485 if (listener->IsLazy()) {
466 if (listener->IsLazy() && !listener->IsForServiceWorker()) { 486 if (listener->is_for_service_worker()) {
467 lazy_event_dispatcher.DispatchToEventPage(listener->extension_id(), 487 lazy_event_dispatcher.DispatchToServiceWorker(
468 listener->filter()); 488 listener->extension_id(), listener->listener_url(), nullptr);
489 } else {
490 lazy_event_dispatcher.DispatchToEventPage(listener->extension_id(),
491 listener->filter());
492 }
469 } 493 }
470 } 494 }
471 495
472 for (const EventListener* listener : listeners) { 496 for (const EventListener* listener : listeners) {
473 if (!restrict_to_extension_id.empty() && 497 if (!restrict_to_extension_id.empty() &&
474 restrict_to_extension_id != listener->extension_id()) { 498 restrict_to_extension_id != listener->extension_id()) {
475 continue; 499 continue;
476 } 500 }
477 if (!listener->process()) 501 if (!listener->process())
478 continue; 502 continue;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 event.get(), listener_filter)) { 581 event.get(), listener_filter)) {
558 return; 582 return;
559 } 583 }
560 584
561 int event_id = g_extension_event_id.GetNext(); 585 int event_id = g_extension_event_id.GetNext();
562 DispatchExtensionMessage(process, worker_thread_id, listener_context, 586 DispatchExtensionMessage(process, worker_thread_id, listener_context,
563 extension_id, event_id, event->event_name, 587 extension_id, event_id, event->event_name,
564 event->event_args.get(), event->user_gesture, 588 event->event_args.get(), event->user_gesture,
565 event->filter_info); 589 event->filter_info);
566 590
591 // TODO(lazyboy): This is wrong for extensions SW events. We need to:
592 // 1. Increment worker ref count
593 // 2. Add EventAck IPC to decrement that ref count.
567 if (extension) { 594 if (extension) {
568 ReportEvent(event->histogram_value, extension, did_enqueue); 595 ReportEvent(event->histogram_value, extension, did_enqueue);
569 IncrementInFlightEvents(listener_context, extension, event_id, 596 IncrementInFlightEvents(listener_context, extension, event_id,
570 event->event_name); 597 event->event_name);
571 } 598 }
572 } 599 }
573 600
574 // static 601 // static
575 void EventRouter::DoDispatchEventToSenderBookkeepingOnUI( 602 void EventRouter::DoDispatchEventToSenderBookkeepingOnUI(
576 void* browser_context_id, 603 void* browser_context_id,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 histogram_value, events::ENUM_BOUNDARY); 699 histogram_value, events::ENUM_BOUNDARY);
673 } 700 }
674 } else { 701 } else {
675 UMA_HISTOGRAM_ENUMERATION( 702 UMA_HISTOGRAM_ENUMERATION(
676 "Extensions.Events.DispatchWithRunningEventPage", histogram_value, 703 "Extensions.Events.DispatchWithRunningEventPage", histogram_value,
677 events::ENUM_BOUNDARY); 704 events::ENUM_BOUNDARY);
678 } 705 }
679 } 706 }
680 } 707 }
681 708
682 void EventRouter::DispatchPendingEvent(const linked_ptr<Event>& event, 709 void EventRouter::DispatchPendingEvent(
683 ExtensionHost* host) { 710 const linked_ptr<Event>& event,
684 if (!host) 711 std::unique_ptr<LazyContextTaskQueue::ContextInfo> params) {
712 if (!params)
685 return; 713 return;
686 714
687 if (listeners_.HasProcessListener(host->render_process_host(), 715 if (listeners_.HasProcessListener(params->render_process_host,
688 kNonWorkerThreadId, 716 params->worker_thread_id,
689 host->extension()->id())) { 717 params->extension_id)) {
690 DispatchEventToProcess(host->extension()->id(), host->GetURL(), 718 DispatchEventToProcess(
691 host->render_process_host(), kNonWorkerThreadId, 719 params->extension_id, params->url, params->render_process_host,
692 event, nullptr, true /* did_enqueue */); 720 params->worker_thread_id, event, nullptr, true /* did_enqueue */);
693 } 721 }
694 } 722 }
695 723
696 void EventRouter::SetRegisteredEvents(const std::string& extension_id, 724 void EventRouter::SetRegisteredEvents(const std::string& extension_id,
697 const std::set<std::string>& events, 725 const std::set<std::string>& events,
698 RegisteredEventType type) { 726 RegisteredEventType type) {
699 auto events_value = base::MakeUnique<base::ListValue>(); 727 auto events_value = base::MakeUnique<base::ListValue>();
700 for (std::set<std::string>::const_iterator iter = events.begin(); 728 for (std::set<std::string>::const_iterator iter = events.begin();
701 iter != events.end(); ++iter) { 729 iter != events.end(); ++iter) {
702 events_value->AppendString(*iter); 730 events_value->AppendString(*iter);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events); 766 listeners_.LoadFilteredLazyListeners(extension->id(), *filtered_events);
739 } 767 }
740 768
741 void EventRouter::OnExtensionUnloaded(content::BrowserContext* browser_context, 769 void EventRouter::OnExtensionUnloaded(content::BrowserContext* browser_context,
742 const Extension* extension, 770 const Extension* extension,
743 UnloadedExtensionReason reason) { 771 UnloadedExtensionReason reason) {
744 // Remove all registered listeners from our cache. 772 // Remove all registered listeners from our cache.
745 listeners_.RemoveListenersForExtension(extension->id()); 773 listeners_.RemoveListenersForExtension(extension->id());
746 } 774 }
747 775
748 void EventRouter::AddLazyEventListenerImpl(const std::string& event_name, 776 void EventRouter::AddLazyEventListenerImpl(
749 const ExtensionId& extension_id, 777 std::unique_ptr<EventListener> listener,
750 int worker_thread_id) { 778 RegisteredEventType type) {
751 bool is_for_service_worker = worker_thread_id != kNonWorkerThreadId; 779 const ExtensionId extension_id = listener->extension_id();
752 bool is_new = listeners_.AddListener( 780 const std::string event_name = listener->event_name();
753 is_for_service_worker 781 bool is_new = listeners_.AddListener(std::move(listener));
754 ? EventListener::ForExtensionServiceWorker(
755 event_name, extension_id, nullptr, worker_thread_id, nullptr)
756 : EventListener::ForExtension(event_name, extension_id, nullptr,
757 nullptr));
758
759 if (is_new) { 782 if (is_new) {
760 RegisteredEventType type = is_for_service_worker
761 ? RegisteredEventType::kServiceWorker
762 : RegisteredEventType::kLazy;
763 std::set<std::string> events = GetRegisteredEvents(extension_id, type); 783 std::set<std::string> events = GetRegisteredEvents(extension_id, type);
764 bool prefs_is_new = events.insert(event_name).second; 784 bool prefs_is_new = events.insert(event_name).second;
765 if (prefs_is_new) 785 if (prefs_is_new)
766 SetRegisteredEvents(extension_id, events, type); 786 SetRegisteredEvents(extension_id, events, type);
767 } 787 }
768 } 788 }
769 789
770 void EventRouter::RemoveLazyEventListenerImpl(const std::string& event_name, 790 void EventRouter::RemoveLazyEventListenerImpl(
771 const ExtensionId& extension_id, 791 std::unique_ptr<EventListener> listener,
772 int worker_thread_id) { 792 RegisteredEventType type) {
773 bool is_for_service_worker = worker_thread_id != kNonWorkerThreadId; 793 const ExtensionId extension_id = listener->extension_id();
774 std::unique_ptr<EventListener> listener = 794 const std::string event_name = listener->event_name();
775 is_for_service_worker
776 ? EventListener::ForExtensionServiceWorker(
777 event_name, extension_id, nullptr, worker_thread_id, nullptr)
778 : EventListener::ForExtension(event_name, extension_id, nullptr,
779 nullptr);
780 bool did_exist = listeners_.RemoveListener(listener.get()); 795 bool did_exist = listeners_.RemoveListener(listener.get());
781
782 if (did_exist) { 796 if (did_exist) {
783 RegisteredEventType type = is_for_service_worker
784 ? RegisteredEventType::kServiceWorker
785 : RegisteredEventType::kLazy;
786 std::set<std::string> events = GetRegisteredEvents(extension_id, type); 797 std::set<std::string> events = GetRegisteredEvents(extension_id, type);
787 bool prefs_did_exist = events.erase(event_name) > 0; 798 bool prefs_did_exist = events.erase(event_name) > 0;
788 DCHECK(prefs_did_exist); 799 DCHECK(prefs_did_exist);
789 SetRegisteredEvents(extension_id, events, type); 800 SetRegisteredEvents(extension_id, events, type);
790 } 801 }
791 } 802 }
792 803
793 Event::Event(events::HistogramValue histogram_value, 804 Event::Event(events::HistogramValue histogram_value,
794 const std::string& event_name, 805 const std::string& event_name,
795 std::unique_ptr<base::ListValue> event_args) 806 std::unique_ptr<base::ListValue> event_args)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 const std::string& extension_id, 856 const std::string& extension_id,
846 const GURL& listener_url, 857 const GURL& listener_url,
847 content::BrowserContext* browser_context) 858 content::BrowserContext* browser_context)
848 : event_name(event_name), 859 : event_name(event_name),
849 extension_id(extension_id), 860 extension_id(extension_id),
850 listener_url(listener_url), 861 listener_url(listener_url),
851 browser_context(browser_context) { 862 browser_context(browser_context) {
852 } 863 }
853 864
854 } // namespace extensions 865 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/event_router.h ('k') | extensions/browser/events/lazy_event_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698