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

Side by Side Diff: extensions/browser/events/lazy_event_dispatcher.cc

Issue 2940883007: Move lazy event dispatching code out of EventRouter. (Closed)
Patch Set: compile Created 3 years, 6 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/events/lazy_event_dispatcher.h ('k') | extensions/browser/lazy_context_id.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "extensions/browser/events/lazy_event_dispatcher.h"
6
7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
9 #include "extensions/browser/event_router.h"
10 #include "extensions/browser/extension_registry.h"
11 #include "extensions/browser/extensions_browser_client.h"
12 #include "extensions/browser/lazy_background_task_queue.h"
13 #include "extensions/browser/lazy_context_id.h"
14 #include "extensions/common/manifest_handlers/incognito_info.h"
15
16 using content::BrowserContext;
17
18 namespace extensions {
19
20 LazyEventDispatcher::LazyEventDispatcher(
21 BrowserContext* browser_context,
22 const linked_ptr<Event>& event,
23 const DispatchFunction& dispatch_function)
24 : browser_context_(browser_context),
25 event_(event),
26 dispatch_function_(dispatch_function) {}
27
28 LazyEventDispatcher::~LazyEventDispatcher() {}
29
30 void LazyEventDispatcher::DispatchToEventPage(
31 const ExtensionId& extension_id,
32 const base::DictionaryValue* listener_filter) {
33 LazyContextId dispatch_context(browser_context_, extension_id);
34 DispatchToLazyContext(&dispatch_context, listener_filter);
35 }
36
37 bool LazyEventDispatcher::HasAlreadyDispatched(
38 BrowserContext* context,
39 const EventListener* listener) const {
40 auto dispatch_context =
41 base::MakeUnique<LazyContextId>(context, listener->extension_id());
42 return HasAlreadyDispatchedImpl(dispatch_context.get());
43 }
44
45 void LazyEventDispatcher::DispatchToLazyContext(
46 LazyContextId* dispatch_context,
47 const base::DictionaryValue* listener_filter) {
48 const Extension* extension = ExtensionRegistry::Get(browser_context_)
49 ->enabled_extensions()
50 .GetByID(dispatch_context->extension_id());
51 if (!extension)
52 return;
53
54 // Check both the original and the incognito browser context to see if we
55 // should load a non-peristent context (a lazy background page or an
56 // extension service worker) to handle the event. The latter case
Devlin 2017/06/15 15:09:50 nit: with the addition of the "a lazy background p
lazyboy 2017/06/15 17:33:51 Done.
57 // occurs in the case of split-mode extensions.
58 if (QueueEventDispatch(dispatch_context, extension, listener_filter))
59 RecordAlreadyDispatched(dispatch_context);
60
61 BrowserContext* additional_context = GetIncognitoContext(extension);
62 if (!additional_context)
63 return;
64
65 dispatch_context->set_browser_context(additional_context);
66 if (QueueEventDispatch(dispatch_context, extension, listener_filter))
67 RecordAlreadyDispatched(dispatch_context);
68 }
69
70 bool LazyEventDispatcher::QueueEventDispatch(
71 LazyContextId* dispatch_context,
72 const Extension* extension,
73 const base::DictionaryValue* listener_filter) {
74 if (!EventRouter::CanDispatchEventToBrowserContext(
75 dispatch_context->browser_context(), extension, *event_)) {
76 return false;
77 }
78
79 if (HasAlreadyDispatchedImpl(dispatch_context))
80 return false;
81
82 LazyBackgroundTaskQueue* queue = dispatch_context->GetTaskQueue();
83 if (!queue->ShouldEnqueueTask(dispatch_context->browser_context(),
84 extension)) {
85 return false;
86 }
87
88 linked_ptr<Event> dispatched_event(event_);
89
90 // If there's a dispatch callback, call it now (rather than dispatch time)
91 // to avoid lifetime issues. Use a separate copy of the event args, so they
92 // last until the event is dispatched.
93 if (!event_->will_dispatch_callback.is_null()) {
94 dispatched_event.reset(event_->DeepCopy());
95 if (!dispatched_event->will_dispatch_callback.Run(
96 dispatch_context->browser_context(), extension,
97 dispatched_event.get(), listener_filter)) {
98 // The event has been canceled.
99 return true;
100 }
101 // Ensure we don't call it again at dispatch time.
102 dispatched_event->will_dispatch_callback.Reset();
103 }
104
105 queue->AddPendingTask(dispatch_context->browser_context(),
106 dispatch_context->extension_id(),
107 base::Bind(dispatch_function_, dispatched_event));
108
109 return true;
110 }
111
112 bool LazyEventDispatcher::HasAlreadyDispatchedImpl(
113 const LazyContextId* dispatch_context) const {
114 DCHECK(dispatch_context->is_for_event_page());
115 EventPageDispatchIdentifier dispatch_id(dispatch_context->browser_context(),
116 dispatch_context->extension_id());
117 return base::ContainsKey(dispatched_ids_for_event_page_, dispatch_id);
118 }
119
120 void LazyEventDispatcher::RecordAlreadyDispatched(
121 LazyContextId* dispatch_context) {
122 DCHECK(dispatch_context->is_for_event_page());
123 dispatched_ids_for_event_page_.insert(std::make_pair(
124 dispatch_context->browser_context(), dispatch_context->extension_id()));
125 }
126
127 BrowserContext* LazyEventDispatcher::GetIncognitoContext(
128 const Extension* extension) {
129 if (!IncognitoInfo::IsSplitMode(extension))
130 return nullptr;
131 ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get();
132 if (!browser_client->HasOffTheRecordContext(browser_context_))
133 return nullptr;
134 return browser_client->GetOffTheRecordContext(browser_context_);
135 }
136
137 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/events/lazy_event_dispatcher.h ('k') | extensions/browser/lazy_context_id.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698