summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_io_event_router.cc
blob: 2c9f234f6d029a0d36dfbe7cd454858b18dc40df (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
// Copyright (c) 2011 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_io_event_router.h"

#include "googleurl/src/gurl.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/profiles/profile.h"

ExtensionIOEventRouter::ExtensionIOEventRouter(Profile* profile)
    : profile_(profile) {
}

ExtensionIOEventRouter::~ExtensionIOEventRouter() {
}

void ExtensionIOEventRouter::DispatchEventToExtension(
    const std::string& extension_id,
    const std::string& event_name,
    const std::string& event_args) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
      NewRunnableMethod(this,
                        &ExtensionIOEventRouter::DispatchEventOnUIThread,
                        extension_id, event_name, event_args));
}

void ExtensionIOEventRouter::DispatchEventToRenderers(
    const std::string& event_name,
    const std::string& event_args,
    const GURL& event_url) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
      NewRunnableMethod(this,
          &ExtensionIOEventRouter::DispatchEventToRenderersOnUIThread,
          event_name, event_args, event_url));
}

void ExtensionIOEventRouter::DispatchEventOnUIThread(
    const std::string& extension_id,
    const std::string& event_name,
    const std::string& event_args) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // If the profile has gone away, we're shutting down. If there's no event
  // router, the extension system hasn't been initialized.
  if (!profile_ || !profile_->GetExtensionEventRouter())
    return;

  profile_->GetExtensionEventRouter()->DispatchEventToExtension(
      extension_id, event_name, event_args, profile_, GURL());
}

void ExtensionIOEventRouter::DispatchEventToRenderersOnUIThread(
    const std::string& event_name,
    const std::string& event_args,
    const GURL& event_url) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  // If the profile has gone away, we're shutting down. If there's no event
  // router, the extension system hasn't been initialized.
  if (!profile_ || !profile_->GetExtensionEventRouter())
    return;

  profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
      event_name, event_args, profile_, event_url);
}