summaryrefslogtreecommitdiffstats
path: root/athena/content/app_activity_registry.h
blob: 9bcf09f72efde0196d158026d6876a6f47b61de9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ATHENA_CONTENT_APP_ACTIVITY_REGISTRY_H_
#define ATHENA_CONTENT_APP_ACTIVITY_REGISTRY_H_

#include <vector>

#include "athena/activity/public/activity_view_model.h"
#include "athena/content/app_activity_proxy.h"
#include "ui/gfx/image/image_skia.h"

namespace aura {
class Window;
}

namespace content {
class BrowserContext;
}

namespace athena {

class AppActivity;

// This class keeps track of all existing |AppActivity|s and shuts down all of
// them when the application gets unloaded to save memory. It will then replace
// the |AppActivity| in the Activity list as proxy to allow restarting of the
// application.
class ATHENA_EXPORT AppActivityRegistry {
 public:
  AppActivityRegistry(const std::string& app_id,
                      content::BrowserContext* browser_context);
  ~AppActivityRegistry();

  // Register an |AppActivity| with this application.
  void RegisterAppActivity(AppActivity* app_activity);

  // Unregister a previously attached |AppActivity|.
  // Note that detaching the last |AppActivity| will delete this object - unless
  // the resource manager was trying to unload the application.
  // Note furthermore that Detach can be called without ever being registered.
  void UnregisterAppActivity(AppActivity* app_activity);

  // Returns the number of activities/windows with this application.
  int NumberOfActivities() const { return activity_list_.size(); }

  // Returns the |AppActivity| at |index|. It will return nullptr if an invalid
  // index was specified.
  AppActivity* GetAppActivityAt(size_t index);

  // Unload all application associated activities to save resources.
  void Unload();

  // Returns true if the application is in the unloaded state.
  bool IsUnloaded() { return unloaded_activity_proxy_ != nullptr; }

  content::BrowserContext* browser_context() const { return browser_context_; }
  const std::string& app_id() const { return app_id_; }

  // Returns the proxy - if there is one. This will get used by a newly created
  // activity to position itself in its place before it get destroyed.
  Activity* unloaded_activity_proxy() { return unloaded_activity_proxy_; }

 protected:
  friend AppActivityProxy;

  // When the |AppActivityProxy| gets destroyed it should call this function
  // to disconnect from this object. This call might destroy |this|.
  void ProxyDestroyed(AppActivityProxy* proxy);

  // When called by the |AppActivityProxy| to restart the application, it can
  // cause the application to restart. When that happens the proxy will get
  // destroyed. After this call |this| might be destroyed.
  void RestartApplication(AppActivityProxy* proxy);

 private:
  // Called if an unload of an application should take place asynchronously to
  // avoid object destruction within an observer handler.
  void DelayedUnload();

  // Gets most recently used AppAcitivty that belongs to the same application.
  AppActivity* GetMruActivity();

  // A list of all activities associated with this application.
  std::vector<AppActivity*> activity_list_;

  // The application id for this proxy.
  std::string app_id_;

  // The browser context of the user.
  content::BrowserContext* browser_context_;

  // When the activity is unloaded this is the AppActivityProxy. The object is
  // owned the the ActivityManager.
  AppActivityProxy* unloaded_activity_proxy_;

  // The presentation values.
  SkColor color_;
  base::string16 title_;
  gfx::ImageSkia image_;

  DISALLOW_COPY_AND_ASSIGN(AppActivityRegistry);
};

}  // namespace athena

#endif  // ATHENA_CONTENT_APP_ACTIVITY_REGISTRY_H_