app_registry_impl.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2014 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 "athena/content/public/app_registry.h"
6
7#include "athena/content/app_activity_registry.h"
8#include "base/logging.h"
9
10namespace athena {
11
12class AppRegistryImpl : public AppRegistry {
13 public:
14  AppRegistryImpl();
15  virtual ~AppRegistryImpl();
16
17  // AppRegistry:
18  virtual AppActivityRegistry* GetAppActivityRegistry(
19      const std::string& app_id,
20      content::BrowserContext* browser_context) OVERRIDE;
21  virtual int NumberOfApplications() const OVERRIDE { return app_list_.size(); }
22
23 private:
24  virtual void RemoveAppActivityRegistry(
25      AppActivityRegistry* registry) OVERRIDE;
26
27  std::vector<AppActivityRegistry*> app_list_;
28
29  DISALLOW_COPY_AND_ASSIGN(AppRegistryImpl);
30};
31
32namespace {
33
34AppRegistryImpl* instance = NULL;
35
36}  // namespace
37
38AppRegistryImpl::AppRegistryImpl() {
39}
40AppRegistryImpl::~AppRegistryImpl() {
41  DCHECK(app_list_.empty());
42}
43
44AppActivityRegistry* AppRegistryImpl::GetAppActivityRegistry(
45    const std::string& app_id,
46    content::BrowserContext* browser_context) {
47  // Search for an existing proxy.
48  for (std::vector<AppActivityRegistry*>::iterator it = app_list_.begin();
49       it != app_list_.end(); ++it) {
50    if ((*it)->app_id() == app_id &&
51        (*it)->browser_context() == browser_context)
52      return *it;
53  }
54
55  // Create and return a new application object.
56  AppActivityRegistry* app_activity_registry =
57      new AppActivityRegistry(app_id, browser_context);
58  app_list_.push_back(app_activity_registry);
59  return app_activity_registry;
60}
61
62void AppRegistryImpl::RemoveAppActivityRegistry(AppActivityRegistry* registry) {
63  std::vector<AppActivityRegistry*>::iterator item =
64      std::find(app_list_.begin(), app_list_.end(), registry);
65  CHECK(item != app_list_.end());
66  app_list_.erase(item);
67  delete registry;
68}
69
70// static
71void AppRegistry::Create() {
72  DCHECK(!instance);
73  instance = new AppRegistryImpl();
74}
75
76// static
77AppRegistry* AppRegistry::Get() {
78  DCHECK(instance);
79  return instance;
80}
81
82// static
83void AppRegistry::ShutDown() {
84  DCHECK(instance);
85  delete instance;
86}
87
88AppRegistry::AppRegistry() {}
89
90AppRegistry::~AppRegistry() {
91  instance = NULL;
92}
93
94}  // namespace athena
95