Alexey Baskakov | fd3894e | 2018-10-16 06:09:58 | [diff] [blame^] | 1 | // Copyright 2018 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/web_applications/web_app_registrar.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "chrome/browser/web_applications/web_app.h" |
| 9 | |
| 10 | namespace web_app { |
| 11 | |
| 12 | WebAppRegistrar::WebAppRegistrar() {} |
| 13 | |
| 14 | WebAppRegistrar::~WebAppRegistrar() = default; |
| 15 | |
| 16 | void WebAppRegistrar::RegisterApp(std::unique_ptr<WebApp> web_app) { |
| 17 | const auto app_id = web_app->app_id(); |
| 18 | DCHECK(!app_id.empty()); |
| 19 | DCHECK(!GetAppById(app_id)); |
| 20 | |
| 21 | registry_.emplace(std::make_pair(app_id, std::move(web_app))); |
| 22 | } |
| 23 | |
| 24 | std::unique_ptr<WebApp> WebAppRegistrar::UnregisterApp(const AppId& app_id) { |
| 25 | DCHECK(!app_id.empty()); |
| 26 | |
| 27 | auto kv = registry_.find(app_id); |
| 28 | DCHECK(kv != registry_.end()); |
| 29 | |
| 30 | auto web_app = std::move(kv->second); |
| 31 | registry_.erase(kv); |
| 32 | return web_app; |
| 33 | } |
| 34 | |
| 35 | WebApp* WebAppRegistrar::GetAppById(const AppId& app_id) { |
| 36 | auto kv = registry_.find(app_id); |
| 37 | return kv == registry_.end() ? nullptr : kv->second.get(); |
| 38 | } |
| 39 | |
| 40 | } // namespace web_app |