Chromium Code Reviews| 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 bool SetDefaultHandler(AppShimHandler* handler) { | |
| 40 AppShimHandler* old_default_handler = default_handler_; | |
|
tapted
2013/05/21 06:15:37
I think we can omit the return and add an appropri
jackhou1
2013/05/22 23:54:44
Done.
| |
| 41 default_handler_ = handler; | |
| 42 return default_handler_ == old_default_handler; | |
| 43 } | |
| 44 | |
| 39 private: | 45 private: |
| 40 friend struct DefaultSingletonTraits<AppShimHandlerRegistry>; | 46 friend struct DefaultSingletonTraits<AppShimHandlerRegistry>; |
| 41 typedef std::map<std::string, AppShimHandler*> HandlerMap; | 47 typedef std::map<std::string, AppShimHandler*> HandlerMap; |
| 42 | 48 |
| 43 AppShimHandlerRegistry() {} | 49 AppShimHandlerRegistry() : default_handler_(NULL) {} |
| 44 ~AppShimHandlerRegistry() {} | 50 ~AppShimHandlerRegistry() {} |
| 45 | 51 |
| 46 HandlerMap handlers_; | 52 HandlerMap handlers_; |
| 53 AppShimHandler* default_handler_; | |
| 47 | 54 |
| 48 DISALLOW_COPY_AND_ASSIGN(AppShimHandlerRegistry); | 55 DISALLOW_COPY_AND_ASSIGN(AppShimHandlerRegistry); |
| 49 }; | 56 }; |
| 50 | 57 |
| 51 } // namespace | 58 } // namespace |
| 52 | 59 |
| 53 // static | 60 // static |
| 54 void AppShimHandler::RegisterHandler(const std::string& app_mode_id, | 61 void AppShimHandler::RegisterHandler(const std::string& app_mode_id, |
| 55 AppShimHandler* handler) { | 62 AppShimHandler* handler) { |
| 56 DCHECK(handler); | 63 DCHECK(handler); |
| 57 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, handler); | 64 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, handler); |
| 58 } | 65 } |
| 59 | 66 |
| 60 // static | 67 // static |
| 61 void AppShimHandler::RemoveHandler(const std::string& app_mode_id) { | 68 void AppShimHandler::RemoveHandler(const std::string& app_mode_id) { |
| 62 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, NULL); | 69 AppShimHandlerRegistry::GetInstance()->SetForAppMode(app_mode_id, NULL); |
| 63 } | 70 } |
| 64 | 71 |
| 65 // static | 72 // static |
| 66 AppShimHandler* AppShimHandler::GetForAppMode(const std::string& app_mode_id) { | 73 AppShimHandler* AppShimHandler::GetForAppMode(const std::string& app_mode_id) { |
| 67 return AppShimHandlerRegistry::GetInstance()->GetForAppMode(app_mode_id); | 74 return AppShimHandlerRegistry::GetInstance()->GetForAppMode(app_mode_id); |
| 68 } | 75 } |
| 69 | 76 |
| 77 // static | |
| 78 void AppShimHandler::SetDefaultHandler(AppShimHandler* handler) { | |
| 79 AppShimHandlerRegistry::GetInstance()->SetDefaultHandler(handler); | |
| 80 } | |
| 81 | |
| 70 } // namespace apps | 82 } // namespace apps |
| OLD | NEW |