summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/identity/identity_event_router.cc
blob: 06a9ac8d53dec614424b0248f64c9f23078e4b55 (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
// Copyright 2013 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/api/identity/identity_event_router.h"

#include <set>

#include "base/stl_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/common/extensions/api/identity.h"

namespace extensions {

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

IdentityEventRouter::~IdentityEventRouter() {}

void IdentityEventRouter::DispatchSignInEvent(const std::string& id,
                                              const std::string& email,
                                              bool is_signed_in) {
  const EventListenerMap::ListenerList& listeners =
      extensions::ExtensionSystem::Get(profile_)->event_router()->listeners()
      .GetEventListenersByName(api::identity::OnSignInChanged::kEventName);

  ExtensionService* service =
      ExtensionSystem::Get(profile_)->extension_service();
  EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();

  api::identity::AccountInfo account_info;
  account_info.id = id;

  api::identity::AccountInfo account_info_email;
  account_info_email.id = id;
  account_info_email.email = scoped_ptr<std::string>(new std::string(email));

  std::set<std::string> already_dispatched;

  for (EventListenerMap::ListenerList::const_iterator it = listeners.begin();
       it != listeners.end();
       ++it) {

    const std::string extension_id = (*it)->extension_id;
    const Extension* extension = service->extensions()->GetByID(extension_id);

    if (ContainsKey(already_dispatched, extension_id))
      continue;

    already_dispatched.insert(extension_id);

    // Add the email address to AccountInfo only for extensions that
    // have APIPermission::kIdentityEmail.
    scoped_ptr<base::ListValue> args;
    if (extension->HasAPIPermission(APIPermission::kIdentityEmail)) {
      args = api::identity::OnSignInChanged::Create(account_info_email,
                                                    is_signed_in);
    } else {
      args = api::identity::OnSignInChanged::Create(account_info,
                                                    is_signed_in);
    }

    scoped_ptr<Event> event(
        new Event(api::identity::OnSignInChanged::kEventName, args.Pass()));
    event->restrict_to_profile = profile_;
    event_router->DispatchEventToExtension(extension_id, event.Pass());
  }
}

}  // namespace extensions