1 // Copyright 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.
5 #include "apps/app_shim/app_shim_handler_mac.h"
9 #include "base/logging.h"
10 #include "base/memory/singleton.h"
16 class AppShimHandlerRegistry
{
18 static AppShimHandlerRegistry
* GetInstance() {
19 return Singleton
<AppShimHandlerRegistry
,
20 LeakySingletonTraits
<AppShimHandlerRegistry
> >::get();
23 AppShimHandler
* GetForAppMode(const std::string
& app_mode_id
) const {
24 HandlerMap::const_iterator it
= handlers_
.find(app_mode_id
);
25 if (it
!= handlers_
.end())
28 return default_handler_
;
31 bool SetForAppMode(const std::string
& app_mode_id
, AppShimHandler
* handler
) {
32 bool inserted_or_removed
= handler
?
33 handlers_
.insert(HandlerMap::value_type(app_mode_id
, handler
)).second
:
34 handlers_
.erase(app_mode_id
) == 1;
35 DCHECK(inserted_or_removed
);
36 return inserted_or_removed
;
39 void SetDefaultHandler(AppShimHandler
* handler
) {
40 DCHECK_NE(default_handler_
== NULL
, handler
== NULL
);
41 default_handler_
= handler
;
45 friend struct DefaultSingletonTraits
<AppShimHandlerRegistry
>;
46 typedef std::map
<std::string
, AppShimHandler
*> HandlerMap
;
48 AppShimHandlerRegistry() : default_handler_(NULL
) {}
49 ~AppShimHandlerRegistry() {}
52 AppShimHandler
* default_handler_
;
54 DISALLOW_COPY_AND_ASSIGN(AppShimHandlerRegistry
);
60 void AppShimHandler::RegisterHandler(const std::string
& app_mode_id
,
61 AppShimHandler
* handler
) {
63 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id
, handler
);
67 void AppShimHandler::RemoveHandler(const std::string
& app_mode_id
) {
68 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id
, NULL
);
72 AppShimHandler
* AppShimHandler::GetForAppMode(const std::string
& app_mode_id
) {
73 return AppShimHandlerRegistry::GetInstance()->GetForAppMode(app_mode_id
);
77 void AppShimHandler::SetDefaultHandler(AppShimHandler
* handler
) {
78 AppShimHandlerRegistry::GetInstance()->SetDefaultHandler(handler
);