summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_event_router_forwarder.cc
blob: 514745399865d0e8aea995fde7c2a5b96b335fc6 (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
// 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_event_router_forwarder.h"

#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "googleurl/src/gurl.h"

ExtensionEventRouterForwarder::ExtensionEventRouterForwarder() {
}

ExtensionEventRouterForwarder::~ExtensionEventRouterForwarder() {
}

void ExtensionEventRouterForwarder::BroadcastEventToRenderers(
    const std::string& event_name, const std::string& event_args,
    const GURL& event_url) {
  HandleEvent("", event_name, event_args, 0, true, event_url);
}

void ExtensionEventRouterForwarder::DispatchEventToRenderers(
    const std::string& event_name, const std::string& event_args,
    ProfileId profile_id, bool use_profile_to_restrict_events,
    const GURL& event_url) {
  if (profile_id == Profile::kInvalidProfileId)
    return;
  HandleEvent("", event_name, event_args, profile_id,
              use_profile_to_restrict_events, event_url);
}

void ExtensionEventRouterForwarder::BroadcastEventToExtension(
    const std::string& extension_id,
    const std::string& event_name, const std::string& event_args,
    const GURL& event_url) {
  HandleEvent(extension_id, event_name, event_args, 0, true, event_url);
}

void ExtensionEventRouterForwarder::DispatchEventToExtension(
    const std::string& extension_id,
    const std::string& event_name, const std::string& event_args,
    ProfileId profile_id, bool use_profile_to_restrict_events,
    const GURL& event_url) {
  if (profile_id == Profile::kInvalidProfileId)
    return;
  HandleEvent(extension_id, event_name, event_args, profile_id,
              use_profile_to_restrict_events, event_url);
}

void ExtensionEventRouterForwarder::HandleEvent(
    const std::string& extension_id,
    const std::string& event_name, const std::string& event_args,
    ProfileId profile_id, bool use_profile_to_restrict_events,
    const GURL& event_url) {
  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    BrowserThread::PostTask(
        BrowserThread::UI, FROM_HERE,
        NewRunnableMethod(
            this,
            &ExtensionEventRouterForwarder::HandleEvent,
            extension_id, event_name, event_args, profile_id,
            use_profile_to_restrict_events, event_url));
    return;
  }

  if (!g_browser_process || !g_browser_process->profile_manager())
    return;

  ProfileManager* profile_manager = g_browser_process->profile_manager();
  Profile* profile = NULL;
  if (profile_id != Profile::kInvalidProfileId) {
    profile = profile_manager->GetProfileWithId(profile_id);
    if (!profile)
      return;
  }
  if (profile) {
    CallExtensionEventRouter(
        profile, extension_id, event_name, event_args,
        use_profile_to_restrict_events ? profile : NULL, event_url);
  } else {
    ProfileManager::iterator i;
    for (i = profile_manager->begin(); i != profile_manager->end(); ++i) {
      CallExtensionEventRouter(
          *i, extension_id, event_name, event_args,
          use_profile_to_restrict_events ? (*i) : NULL, event_url);
    }
  }
}

void ExtensionEventRouterForwarder::CallExtensionEventRouter(
    Profile* profile, const std::string& extension_id,
    const std::string& event_name, const std::string& event_args,
    Profile* restrict_to_profile, const GURL& event_url) {
#if defined(OS_CHROMEOS)
  // Extension does not exist for chromeos login.  This needs to be
  // removed once we have an extension service for login screen.
  // crosbug.com/12856.
  if (!profile->GetExtensionEventRouter())
    return;
#endif

  if (extension_id.empty()) {
    profile->GetExtensionEventRouter()->
        DispatchEventToRenderers(
            event_name, event_args, restrict_to_profile, event_url);
  } else {
    profile->GetExtensionEventRouter()->
        DispatchEventToExtension(
            extension_id,
            event_name, event_args, restrict_to_profile, event_url);
  }
}