OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/api/identity/identity_event_router.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/stl_util.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/event_router.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" |
| 14 #include "chrome/common/extensions/api/identity.h" |
| 15 |
| 16 namespace extensions { |
| 17 |
| 18 IdentityEventRouter::IdentityEventRouter(Profile* profile) |
| 19 : profile_(profile) {} |
| 20 |
| 21 IdentityEventRouter::~IdentityEventRouter() {} |
| 22 |
| 23 void IdentityEventRouter::DispatchSignInEvent(const std::string& id, |
| 24 const std::string& email, |
| 25 bool is_signed_in) { |
| 26 const EventListenerMap::ListenerList& listeners = |
| 27 extensions::ExtensionSystem::Get(profile_)->event_router()->listeners() |
| 28 .GetEventListenersByName(api::identity::OnSignInChanged::kEventName); |
| 29 |
| 30 ExtensionService* service = |
| 31 ExtensionSystem::Get(profile_)->extension_service(); |
| 32 EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router(); |
| 33 |
| 34 api::identity::AccountInfo account_info; |
| 35 account_info.id = id; |
| 36 |
| 37 api::identity::AccountInfo account_info_email; |
| 38 account_info_email.id = id; |
| 39 account_info_email.email = scoped_ptr<std::string>(new std::string(email)); |
| 40 |
| 41 std::set<std::string> already_dispatched; |
| 42 |
| 43 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); |
| 44 it != listeners.end(); |
| 45 ++it) { |
| 46 |
| 47 const std::string extension_id = (*it)->extension_id; |
| 48 const Extension* extension = service->extensions()->GetByID(extension_id); |
| 49 |
| 50 if (ContainsKey(already_dispatched, extension_id)) |
| 51 continue; |
| 52 |
| 53 already_dispatched.insert(extension_id); |
| 54 |
| 55 // Add the email address to AccountInfo only for extensions that |
| 56 // have APIPermission::kIdentityEmail. |
| 57 scoped_ptr<base::ListValue> args; |
| 58 if (extension->HasAPIPermission(APIPermission::kIdentityEmail)) { |
| 59 args = api::identity::OnSignInChanged::Create(account_info_email, |
| 60 is_signed_in); |
| 61 } else { |
| 62 args = api::identity::OnSignInChanged::Create(account_info, |
| 63 is_signed_in); |
| 64 } |
| 65 |
| 66 scoped_ptr<Event> event( |
| 67 new Event(api::identity::OnSignInChanged::kEventName, args.Pass())); |
| 68 event->restrict_to_profile = profile_; |
| 69 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
| 70 } |
| 71 } |
| 72 |
| 73 } // namespace extensions |
OLD | NEW |