summaryrefslogtreecommitdiffstats
path: root/chrome/views/view_storage.h
blob: a9651585d3a30b4ddfa1bf52ea5ae8c8910cc98c (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
// Copyright (c) 2006-2008 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 CHROME_VIEWS_VIEW_STORAGE_H_
#define CHROME_VIEWS_VIEW_STORAGE_H_

#include "chrome/common/notification_observer.h"
#include "chrome/views/view.h"

// This class is a simple storage place for storing/retrieving views.  It is
// used for example in the FocusManager to store/restore focused views when the
// main window becomes active/inactive.  It supports floating views, meaning
// that when you store a view, it can be retrieved even if it is a floating
// view or the child of a floating view that has been detached since the view
// was stored (in which case the floating view is recreated and reattached).
// It also automatically removes a view from the storage if the view is removed
// from the tree hierarchy (or in the case of a floating view, if the view
// containing the floating view is removed).
//
// To use it, you first need to create a view storage id that can then be used
// to store/retrieve views.

namespace views {

struct ViewLocationInfo;

class ViewStorage : public NotificationObserver {
 public:
  // Returns the global ViewStorage instance.
  // It is guaranted to be non NULL.
  static ViewStorage* GetSharedInstance();

  // Deletes the global instance of the ViewStorage.
  static void DeleteSharedInstance();

  // Returns a unique storage id that can be used to store/retrieve views.
  int CreateStorageID();

  // Associates |view| with the specified |storage_id|.
  void StoreView(int storage_id, View* view);

  // Returns the view associated with |storage_id| if any, NULL otherwise.
  View* RetrieveView(int storage_id);

  // Removes the view associated with |storage_id| if any.
  void RemoveView(int storage_id);

 private:
  ViewStorage();
  ~ViewStorage();

  // NotificationObserver method.
  void Observe(NotificationType type,
               const NotificationSource& source,
               const NotificationDetails& details);

  // Removes the view associated with |storage_id|. If |remove_all_ids| is true,
  // all other mapping pointing to the same view are removed as well.
  void EraseView(int storage_id, bool remove_all_ids);

  // Next id for the view storage.
  int view_storage_next_id_;

  // The association id to View used for the view storage.  The ViewStorage owns
  // the ViewLocationInfo.
  std::map<int, ViewLocationInfo*> id_to_view_location_;

  // Association View to id, used to speed up view notification removal.
  std::map<View*, std::vector<int>*> view_to_ids_;

  // The singleton instance.
  static ViewStorage* shared_instance_;

  DISALLOW_COPY_AND_ASSIGN(ViewStorage);
};

}  // namespace views

#endif  // #ifndef CHROME_VIEWS_VIEW_STORAGE_H_