OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/app_shim/app_shim_handler_mac.h" | 5 #include "apps/app_shim/app_shim_handler_mac.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
11 | 11 |
12 namespace apps { | 12 namespace apps { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 class AppShimHandlerRegistry { | 16 class AppShimHandlerRegistry { |
17 public: | 17 public: |
18 static AppShimHandlerRegistry* GetInstance() { | 18 static AppShimHandlerRegistry* GetInstance() { |
19 return Singleton<AppShimHandlerRegistry, | 19 return Singleton<AppShimHandlerRegistry, |
20 LeakySingletonTraits<AppShimHandlerRegistry> >::get(); | 20 LeakySingletonTraits<AppShimHandlerRegistry> >::get(); |
21 } | 21 } |
22 | 22 |
23 AppShimHandler* GetForAppMode(const std::string& app_mode_id) const { | 23 AppShimHandler* GetForAppMode(const std::string& app_mode_id) const { |
24 HandlerMap::const_iterator it = handlers_.find(app_mode_id); | 24 HandlerMap::const_iterator it = handlers_.find(app_mode_id); |
25 if (it != handlers_.end()) | 25 if (it != handlers_.end()) |
26 return it->second; | 26 return it->second; |
27 | 27 |
28 return NULL; | 28 return default_handler_; |
29 } | 29 } |
30 | 30 |
31 bool SetForAppMode(const std::string& app_mode_id, AppShimHandler* handler) { | 31 bool SetForAppMode(const std::string& app_mode_id, AppShimHandler* handler) { |
32 bool inserted_or_removed = handler ? | 32 bool inserted_or_removed = handler ? |
33 handlers_.insert(HandlerMap::value_type(app_mode_id, handler)).second : | 33 handlers_.insert(HandlerMap::value_type(app_mode_id, handler)).second : |
34 handlers_.erase(app_mode_id) == 1; | 34 handlers_.erase(app_mode_id) == 1; |
35 DCHECK(inserted_or_removed); | 35 DCHECK(inserted_or_removed); |
36 return inserted_or_removed; | 36 return inserted_or_removed; |
37 } | 37 } |
38 | 38 |
| 39 void SetDefaultHandler(AppShimHandler* handler) { |
| 40 DCHECK_NE(default_handler_ == NULL, handler == NULL); |
| 41 default_handler_ = handler; |
| 42 } |
| 43 |
39 private: | 44 private: |
40 friend struct DefaultSingletonTraits<AppShimHandlerRegistry>; | 45 friend struct DefaultSingletonTraits<AppShimHandlerRegistry>; |
41 typedef std::map<std::string, AppShimHandler*> HandlerMap; | 46 typedef std::map<std::string, AppShimHandler*> HandlerMap; |
42 | 47 |
43 AppShimHandlerRegistry() {} | 48 AppShimHandlerRegistry() : default_handler_(NULL) {} |
44 ~AppShimHandlerRegistry() {} | 49 ~AppShimHandlerRegistry() {} |
45 | 50 |
46 HandlerMap handlers_; | 51 HandlerMap handlers_; |
| 52 AppShimHandler* default_handler_; |
47 | 53 |
48 DISALLOW_COPY_AND_ASSIGN(AppShimHandlerRegistry); | 54 DISALLOW_COPY_AND_ASSIGN(AppShimHandlerRegistry); |
49 }; | 55 }; |
50 | 56 |
51 } // namespace | 57 } // namespace |
52 | 58 |
53 // static | 59 // static |
54 void AppShimHandler::RegisterHandler(const std::string& app_mode_id, | 60 void AppShimHandler::RegisterHandler(const std::string& app_mode_id, |
55 AppShimHandler* handler) { | 61 AppShimHandler* handler) { |
56 DCHECK(handler); | 62 DCHECK(handler); |
57 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, handler); | 63 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, handler); |
58 } | 64 } |
59 | 65 |
60 // static | 66 // static |
61 void AppShimHandler::RemoveHandler(const std::string& app_mode_id) { | 67 void AppShimHandler::RemoveHandler(const std::string& app_mode_id) { |
62 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, NULL); | 68 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, NULL); |
63 } | 69 } |
64 | 70 |
65 // static | 71 // static |
66 AppShimHandler* AppShimHandler::GetForAppMode(const std::string& app_mode_id) { | 72 AppShimHandler* AppShimHandler::GetForAppMode(const std::string& app_mode_id) { |
67 return AppShimHandlerRegistry::GetInstance()->GetForAppMode(app_mode_id); | 73 return AppShimHandlerRegistry::GetInstance()->GetForAppMode(app_mode_id); |
68 } | 74 } |
69 | 75 |
| 76 // static |
| 77 void AppShimHandler::SetDefaultHandler(AppShimHandler* handler) { |
| 78 AppShimHandlerRegistry::GetInstance()->SetDefaultHandler(handler); |
| 79 } |
| 80 |
70 } // namespace apps | 81 } // namespace apps |
OLD | NEW |