1// Copyright (c) 2011 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 "chrome/browser/extensions/extension_event_router_forwarder.h"
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/extensions/extension_event_router.h"
9#include "chrome/browser/profiles/profile_manager.h"
10#include "googleurl/src/gurl.h"
11
12ExtensionEventRouterForwarder::ExtensionEventRouterForwarder() {
13}
14
15ExtensionEventRouterForwarder::~ExtensionEventRouterForwarder() {
16}
17
18void ExtensionEventRouterForwarder::BroadcastEventToRenderers(
19    const std::string& event_name, const std::string& event_args,
20    const GURL& event_url) {
21  HandleEvent("", event_name, event_args, 0, true, event_url);
22}
23
24void ExtensionEventRouterForwarder::DispatchEventToRenderers(
25    const std::string& event_name, const std::string& event_args,
26    ProfileId profile_id, bool use_profile_to_restrict_events,
27    const GURL& event_url) {
28  if (profile_id == Profile::kInvalidProfileId)
29    return;
30  HandleEvent("", event_name, event_args, profile_id,
31              use_profile_to_restrict_events, event_url);
32}
33
34void ExtensionEventRouterForwarder::BroadcastEventToExtension(
35    const std::string& extension_id,
36    const std::string& event_name, const std::string& event_args,
37    const GURL& event_url) {
38  HandleEvent(extension_id, event_name, event_args, 0, true, event_url);
39}
40
41void ExtensionEventRouterForwarder::DispatchEventToExtension(
42    const std::string& extension_id,
43    const std::string& event_name, const std::string& event_args,
44    ProfileId profile_id, bool use_profile_to_restrict_events,
45    const GURL& event_url) {
46  if (profile_id == Profile::kInvalidProfileId)
47    return;
48  HandleEvent(extension_id, event_name, event_args, profile_id,
49              use_profile_to_restrict_events, event_url);
50}
51
52void ExtensionEventRouterForwarder::HandleEvent(
53    const std::string& extension_id,
54    const std::string& event_name, const std::string& event_args,
55    ProfileId profile_id, bool use_profile_to_restrict_events,
56    const GURL& event_url) {
57  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
58    BrowserThread::PostTask(
59        BrowserThread::UI, FROM_HERE,
60        NewRunnableMethod(
61            this,
62            &ExtensionEventRouterForwarder::HandleEvent,
63            extension_id, event_name, event_args, profile_id,
64            use_profile_to_restrict_events, event_url));
65    return;
66  }
67
68  if (!g_browser_process || !g_browser_process->profile_manager())
69    return;
70
71  ProfileManager* profile_manager = g_browser_process->profile_manager();
72  Profile* profile = NULL;
73  if (profile_id != Profile::kInvalidProfileId) {
74    profile = profile_manager->GetProfileWithId(profile_id);
75    if (!profile)
76      return;
77  }
78  if (profile) {
79    CallExtensionEventRouter(
80        profile, extension_id, event_name, event_args,
81        use_profile_to_restrict_events ? profile : NULL, event_url);
82  } else {
83    std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
84    for (size_t i = 0; i < profiles.size(); ++i) {
85      CallExtensionEventRouter(
86          profiles[i], extension_id, event_name, event_args,
87          use_profile_to_restrict_events ? profiles[i] : NULL, event_url);
88    }
89  }
90}
91
92void ExtensionEventRouterForwarder::CallExtensionEventRouter(
93    Profile* profile, const std::string& extension_id,
94    const std::string& event_name, const std::string& event_args,
95    Profile* restrict_to_profile, const GURL& event_url) {
96#if defined(OS_CHROMEOS)
97  // Extension does not exist for chromeos login.  This needs to be
98  // removed once we have an extension service for login screen.
99  // crosbug.com/12856.
100  if (!profile->GetExtensionEventRouter())
101    return;
102#endif
103
104  if (extension_id.empty()) {
105    profile->GetExtensionEventRouter()->
106        DispatchEventToRenderers(
107            event_name, event_args, restrict_to_profile, event_url);
108  } else {
109    profile->GetExtensionEventRouter()->
110        DispatchEventToExtension(
111            extension_id,
112            event_name, event_args, restrict_to_profile, event_url);
113  }
114}
115