summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_event_router.cc
blob: 07e0ee3560ad05a42c43a550b8fce88421aaf53f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright (c) 2012 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.

#include "chrome/browser/extensions/extension_event_router.h"

#include "base/bind.h"
#include "base/command_line.h"
#include "base/message_loop.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/runtime/runtime_api.h"
#include "chrome/browser/extensions/api/web_request/web_request_api.h"
#include "chrome/browser/extensions/extension_devtools_manager.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_process_manager.h"
#include "chrome/browser/extensions/extension_processes_api.h"
#include "chrome/browser/extensions/extension_processes_api_constants.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/lazy_background_task_queue.h"
#include "chrome/browser/extensions/process_map.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/extensions/api/extension_api.h"
#include "chrome/common/view_type.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"

using content::BrowserThread;
using extensions::Extension;
using extensions::ExtensionAPI;

namespace {

const char kDispatchEvent[] = "Event.dispatchJSON";

void NotifyEventListenerRemovedOnIOThread(
    void* profile,
    const std::string& extension_id,
    const std::string& sub_event_name) {
  ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(
      profile, extension_id, sub_event_name);
}

}  // namespace

struct ExtensionEventRouter::ListenerProcess {
  content::RenderProcessHost* process;
  std::string extension_id;

  ListenerProcess(content::RenderProcessHost* process,
                const std::string& extension_id)
      : process(process), extension_id(extension_id) {}

  bool operator<(const ListenerProcess& that) const {
    if (process < that.process)
      return true;
    if (process == that.process && extension_id < that.extension_id)
      return true;
    return false;
  }
};

struct ExtensionEventRouter::ExtensionEvent {
  std::string event_name;
  std::string event_args;
  GURL event_url;
  Profile* restrict_to_profile;
  std::string cross_incognito_args;
  UserGestureState user_gesture;

  ExtensionEvent(const std::string& event_name,
                 const std::string& event_args,
                 const GURL& event_url,
                 Profile* restrict_to_profile,
                 const std::string& cross_incognito_args,
                 UserGestureState user_gesture)
    : event_name(event_name),
      event_args(event_args),
      event_url(event_url),
      restrict_to_profile(restrict_to_profile),
      cross_incognito_args(cross_incognito_args),
      user_gesture(user_gesture) {}
};

// static
void ExtensionEventRouter::DispatchEvent(IPC::Message::Sender* ipc_sender,
                                         const std::string& extension_id,
                                         const std::string& event_name,
                                         const std::string& event_args,
                                         const GURL& event_url,
                                         UserGestureState user_gesture) {
  ListValue args;
  args.Set(0, Value::CreateStringValue(event_name));
  args.Set(1, Value::CreateStringValue(event_args));
  ipc_sender->Send(new ExtensionMsg_MessageInvoke(MSG_ROUTING_CONTROL,
      extension_id, kDispatchEvent, args, event_url,
      user_gesture == USER_GESTURE_ENABLED));
}

ExtensionEventRouter::ExtensionEventRouter(Profile* profile)
    : profile_(profile),
      extension_devtools_manager_(
          ExtensionSystem::Get(profile)->devtools_manager()) {
  registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
                 content::NotificationService::AllSources());
  registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
                 content::NotificationService::AllSources());
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
                 content::Source<Profile>(profile_));
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
                 content::Source<Profile>(profile_));
  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
                 content::Source<Profile>(profile_));
}

ExtensionEventRouter::~ExtensionEventRouter() {}

void ExtensionEventRouter::AddEventListener(
    const std::string& event_name,
    content::RenderProcessHost* process,
    const std::string& extension_id) {
  ListenerProcess listener(process, extension_id);
  DCHECK_EQ(listeners_[event_name].count(listener), 0u) << event_name;
  listeners_[event_name].insert(listener);

  if (extension_devtools_manager_.get())
    extension_devtools_manager_->AddEventListener(event_name,
                                                  process->GetID());

  // We lazily tell the TaskManager to start updating when listeners to the
  // processes.onUpdated or processes.onUpdatedWithMemory events arrive.
  if (event_name.compare(extension_processes_api_constants::kOnUpdated) == 0 ||
      event_name.compare(
          extension_processes_api_constants::kOnUpdatedWithMemory) == 0)
    ExtensionProcessesEventRouter::GetInstance()->ListenerAdded();
}

void ExtensionEventRouter::RemoveEventListener(
    const std::string& event_name,
    content::RenderProcessHost* process,
    const std::string& extension_id) {
  ListenerProcess listener(process, extension_id);
  DCHECK_EQ(listeners_[event_name].count(listener), 1u) <<
      " PID=" << process->GetID() << " extension=" << extension_id <<
      " event=" << event_name;
  listeners_[event_name].erase(listener);
  // Note: extension_id may point to data in the now-deleted listeners_ object.
  // Do not use.

  if (extension_devtools_manager_.get())
    extension_devtools_manager_->RemoveEventListener(event_name,
                                                     process->GetID());

  // If a processes.onUpdated or processes.onUpdatedWithMemory event listener
  // is removed (or a process with one exits), then we let the extension API
  // know that it has one fewer listener.
  if (event_name.compare(extension_processes_api_constants::kOnUpdated) == 0 ||
      event_name.compare(
          extension_processes_api_constants::kOnUpdatedWithMemory) == 0)
    ExtensionProcessesEventRouter::GetInstance()->ListenerRemoved();

  BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      base::Bind(
          &NotifyEventListenerRemovedOnIOThread,
          profile_, listener.extension_id, event_name));
}

void ExtensionEventRouter::AddLazyEventListener(
    const std::string& event_name,
    const std::string& extension_id) {
  ListenerProcess lazy_listener(NULL, extension_id);
  bool is_new = lazy_listeners_[event_name].insert(lazy_listener).second;
  if (is_new) {
    ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs();
    std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
    bool prefs_is_new = events.insert(event_name).second;
    if (prefs_is_new)
      prefs->SetRegisteredEvents(extension_id, events);
  }
}

void ExtensionEventRouter::RemoveLazyEventListener(
    const std::string& event_name,
    const std::string& extension_id) {
  ListenerProcess lazy_listener(NULL, extension_id);
  bool did_exist = lazy_listeners_[event_name].erase(lazy_listener) > 0;
  if (did_exist) {
    ExtensionPrefs* prefs = profile_->GetExtensionService()->extension_prefs();
    std::set<std::string> events = prefs->GetRegisteredEvents(extension_id);
    bool prefs_did_exist = events.erase(event_name) > 0;
    DCHECK(prefs_did_exist);
    prefs->SetRegisteredEvents(extension_id, events);
  }
}

bool ExtensionEventRouter::HasEventListener(const std::string& event_name) {
  return (HasEventListenerImpl(listeners_, "", event_name) ||
          HasEventListenerImpl(lazy_listeners_, "", event_name));
}

bool ExtensionEventRouter::ExtensionHasEventListener(
    const std::string& extension_id, const std::string& event_name) {
  return (HasEventListenerImpl(listeners_, extension_id, event_name) ||
          HasEventListenerImpl(lazy_listeners_, extension_id, event_name));
}

bool ExtensionEventRouter::HasEventListenerImpl(
    const ListenerMap& listener_map,
    const std::string& extension_id,
    const std::string& event_name) {
  ListenerMap::const_iterator it = listener_map.find(event_name);
  if (it == listener_map.end())
    return false;

  const std::set<ListenerProcess>& listeners = it->second;
  if (extension_id.empty())
    return !listeners.empty();

  for (std::set<ListenerProcess>::const_iterator listener = listeners.begin();
       listener != listeners.end(); ++listener) {
    if (listener->extension_id == extension_id)
      return true;
  }
  return false;
}

void ExtensionEventRouter::DispatchEventToRenderers(
    const std::string& event_name,
    const std::string& event_args,
    Profile* restrict_to_profile,
    const GURL& event_url) {
  linked_ptr<ExtensionEvent> event(
      new ExtensionEvent(event_name, event_args, event_url,
                         restrict_to_profile, "", USER_GESTURE_UNKNOWN));
  DispatchEventImpl("", event);
}

void ExtensionEventRouter::DispatchEventToExtension(
    const std::string& extension_id,
    const std::string& event_name,
    const std::string& event_args,
    Profile* restrict_to_profile,
    const GURL& event_url) {
  DCHECK(!extension_id.empty());
  linked_ptr<ExtensionEvent> event(
      new ExtensionEvent(event_name, event_args, event_url,
                         restrict_to_profile, "", USER_GESTURE_UNKNOWN));
  DispatchEventImpl(extension_id, event);
}

void ExtensionEventRouter::DispatchEventToExtension(
    const std::string& extension_id,
    const std::string& event_name,
    const std::string& event_args,
    Profile* restrict_to_profile,
    const GURL& event_url,
    UserGestureState user_gesture) {
  DCHECK(!extension_id.empty());
  linked_ptr<ExtensionEvent> event(
      new ExtensionEvent(event_name, event_args, event_url,
                         restrict_to_profile, "", user_gesture));
  DispatchEventImpl(extension_id, event);
}

void ExtensionEventRouter::DispatchEventsToRenderersAcrossIncognito(
    const std::string& event_name,
    const std::string& event_args,
    Profile* restrict_to_profile,
    const std::string& cross_incognito_args,
    const GURL& event_url) {
  linked_ptr<ExtensionEvent> event(
      new ExtensionEvent(event_name, event_args, event_url,
                         restrict_to_profile, cross_incognito_args,
                         USER_GESTURE_UNKNOWN));
  DispatchEventImpl("", event);
}

void ExtensionEventRouter::DispatchEventImpl(
    const std::string& extension_id,
    const linked_ptr<ExtensionEvent>& event) {
  // We don't expect to get events from a completely different profile.
  DCHECK(!event->restrict_to_profile ||
         profile_->IsSameProfile(event->restrict_to_profile));

  LoadLazyBackgroundPagesForEvent(extension_id, event);

  ListenerMap::iterator it = listeners_.find(event->event_name);
  if (it == listeners_.end())
    return;

  std::set<ListenerProcess>& listeners = it->second;
  for (std::set<ListenerProcess>::iterator listener = listeners.begin();
       listener != listeners.end(); ++listener) {
    if (!extension_id.empty() && extension_id != listener->extension_id)
      continue;

    DispatchEventToListener(*listener, event);
  }
}

void ExtensionEventRouter::DispatchEventToListener(
    const ListenerProcess& listener,
    const linked_ptr<ExtensionEvent>& event) {
  ExtensionService* service = profile_->GetExtensionService();
  const Extension* extension = service->extensions()->GetByID(
      listener.extension_id);

  // The extension could have been removed, but we do not unregister it until
  // the extension process is unloaded.
  if (!extension)
    return;

  Profile* listener_profile = Profile::FromBrowserContext(
      listener.process->GetBrowserContext());
  extensions::ProcessMap* process_map =
      listener_profile->GetExtensionService()->process_map();
  // If the event is privileged, only send to extension processes. Otherwise,
  // it's OK to send to normal renderers (e.g., for content scripts).
  if (ExtensionAPI::GetSharedInstance()->IsPrivileged(event->event_name) &&
      !process_map->Contains(extension->id(), listener.process->GetID())) {
    return;
  }

  const std::string* event_args;
  if (!CanDispatchEventToProfile(listener_profile, extension,
                                 event, &event_args))
    return;

  DispatchEvent(listener.process, listener.extension_id,
                event->event_name, *event_args,
                event->event_url, event->user_gesture);
  IncrementInFlightEvents(listener_profile, extension);
}

bool ExtensionEventRouter::CanDispatchEventToProfile(
    Profile* profile,
    const Extension* extension,
    const linked_ptr<ExtensionEvent>& event,
    const std::string** event_args) {
  *event_args = &event->event_args;

  // Is this event from a different profile than the renderer (ie, an
  // incognito tab event sent to a normal process, or vice versa).
  bool cross_incognito = event->restrict_to_profile &&
      profile != event->restrict_to_profile;
  if (cross_incognito &&
      !profile->GetExtensionService()->CanCrossIncognito(extension)) {
    if (event->cross_incognito_args.empty())
      return false;
    // Send the event with different arguments to extensions that can't
    // cross incognito.
    *event_args = &event->cross_incognito_args;
  }

  return true;
}

void ExtensionEventRouter::LoadLazyBackgroundPagesForEvent(
    const std::string& extension_id,
    const linked_ptr<ExtensionEvent>& event) {
  ExtensionService* service = profile_->GetExtensionService();

  ListenerMap::iterator it = lazy_listeners_.find(event->event_name);
  if (it == lazy_listeners_.end())
    return;

  std::set<ListenerProcess>& listeners = it->second;
  for (std::set<ListenerProcess>::iterator listener = listeners.begin();
       listener != listeners.end(); ++listener) {
    if (!extension_id.empty() && extension_id != listener->extension_id)
      continue;

    // Check both the original and the incognito profile to see if we
    // should load a lazy bg page to handle the event. The latter case
    // occurs in the case of split-mode extensions.
    const Extension* extension = service->extensions()->GetByID(
        listener->extension_id);
    if (extension) {
      MaybeLoadLazyBackgroundPage(profile_, extension, event);
      if (profile_->HasOffTheRecordProfile() &&
          extension->incognito_split_mode()) {
        MaybeLoadLazyBackgroundPage(
            profile_->GetOffTheRecordProfile(), extension, event);
      }
    }
  }
}

void ExtensionEventRouter::MaybeLoadLazyBackgroundPage(
    Profile* profile,
    const Extension* extension,
    const linked_ptr<ExtensionEvent>& event) {
  const std::string* event_args;
  if (!CanDispatchEventToProfile(profile, extension, event, &event_args))
    return;

  extensions::LazyBackgroundTaskQueue* queue =
      ExtensionSystem::Get(profile)->lazy_background_task_queue();
  if (queue->ShouldEnqueueTask(profile, extension)) {
    queue->AddPendingTask(
        profile, extension->id(),
        base::Bind(&ExtensionEventRouter::DispatchPendingEvent,
                   base::Unretained(this), event));
  }
}

void ExtensionEventRouter::IncrementInFlightEvents(
    Profile* profile, const Extension* extension) {
  // Only increment in-flight events if the lazy background page is active,
  // because that's the only time we'll get an ACK.
  if (extension->has_lazy_background_page()) {
    ExtensionProcessManager* pm =
        ExtensionSystem::Get(profile)->process_manager();
    ExtensionHost* host = pm->GetBackgroundHostForExtension(extension->id());
    if (host)
      pm->IncrementLazyKeepaliveCount(extension);
  }
}

void ExtensionEventRouter::OnEventAck(
    Profile* profile, const std::string& extension_id) {
  ExtensionProcessManager* pm =
      ExtensionSystem::Get(profile)->process_manager();
  ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id);
  // The event ACK is routed to the background host, so this should never be
  // NULL.
  CHECK(host);
  // TODO(mpcomplete): We should never get this message unless
  // has_lazy_background_page is true. Find out why we're getting it anyway.
  if (host->extension() && host->extension()->has_lazy_background_page())
    pm->DecrementLazyKeepaliveCount(host->extension());
}

void ExtensionEventRouter::DispatchPendingEvent(
    const linked_ptr<ExtensionEvent>& event, ExtensionHost* host) {
  if (!host)
    return;

  ListenerProcess listener(host->render_process_host(),
                           host->extension()->id());
  if (listeners_[event->event_name].count(listener) > 0u)
    DispatchEventToListener(listener, event);
}

void ExtensionEventRouter::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  switch (type) {
    case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED:
    case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
      content::RenderProcessHost* renderer =
          content::Source<content::RenderProcessHost>(source).ptr();
      // Remove all event listeners associated with this renderer.
      for (ListenerMap::iterator it = listeners_.begin();
           it != listeners_.end(); ) {
        ListenerMap::iterator current_it = it++;
        for (std::set<ListenerProcess>::iterator jt =
                 current_it->second.begin();
             jt != current_it->second.end(); ) {
          std::set<ListenerProcess>::iterator current_jt = jt++;
          if (current_jt->process == renderer) {
            RemoveEventListener(current_it->first,
                                current_jt->process,
                                current_jt->extension_id);
          }
        }
      }
      break;
    }
    case chrome::NOTIFICATION_EXTENSION_LOADED: {
      // Add all registered lazy listeners to our cache.
      const Extension* extension =
          content::Details<const Extension>(details).ptr();
      std::set<std::string> registered_events =
          profile_->GetExtensionService()->extension_prefs()->
              GetRegisteredEvents(extension->id());
      ListenerProcess lazy_listener(NULL, extension->id());
      for (std::set<std::string>::iterator it = registered_events.begin();
           it != registered_events.end(); ++it) {
        lazy_listeners_[*it].insert(lazy_listener);
      }
      break;
    }
    case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
      // Remove all registered lazy listeners from our cache.
      extensions::UnloadedExtensionInfo* unloaded =
          content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
      ListenerProcess lazy_listener(NULL, unloaded->extension->id());
      for (ListenerMap::iterator it = lazy_listeners_.begin();
           it != lazy_listeners_.end(); ++it) {
        it->second.erase(lazy_listener);
      }
      break;
    }
    case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
      // Dispatch the onInstalled event.
      const Extension* extension =
          content::Details<const Extension>(details).ptr();
      MessageLoop::current()->PostTask(FROM_HERE,
          base::Bind(&extensions::RuntimeEventRouter::DispatchOnInstalledEvent,
                     profile_, extension->id()));
      break;
    }
    default:
      NOTREACHED();
      return;
  }
}