Home | History | Annotate | Download | only in focus
      1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_FOCUS_VIEW_STORAGE_H_
      6 #define UI_VIEWS_FOCUS_VIEW_STORAGE_H_
      7 
      8 #include <map>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "ui/views/views_export.h"
     13 
     14 template <typename T> struct DefaultSingletonTraits;
     15 
     16 // This class is a simple storage place for storing/retrieving views.  It is
     17 // used for example in the FocusManager to store/restore focused views when the
     18 // main window becomes active/inactive.
     19 // It automatically removes a view from the storage if the view is removed from
     20 // the tree hierarchy.
     21 //
     22 // To use it, you first need to create a view storage id that can then be used
     23 // to store/retrieve views.
     24 
     25 namespace views {
     26 class View;
     27 
     28 class VIEWS_EXPORT ViewStorage {
     29  public:
     30   // Returns the global ViewStorage instance.
     31   // It is guaranted to be non NULL.
     32   static ViewStorage* GetInstance();
     33 
     34   // Returns a unique storage id that can be used to store/retrieve views.
     35   int CreateStorageID();
     36 
     37   // Associates |view| with the specified |storage_id|.
     38   void StoreView(int storage_id, View* view);
     39 
     40   // Returns the view associated with |storage_id| if any, NULL otherwise.
     41   View* RetrieveView(int storage_id);
     42 
     43   // Removes the view associated with |storage_id| if any.
     44   void RemoveView(int storage_id);
     45 
     46   // Notifies the ViewStorage that a view was removed from its parent somewhere.
     47   void ViewRemoved(View* removed);
     48 
     49   size_t view_count() const { return view_to_ids_.size(); }
     50 
     51  private:
     52   friend struct DefaultSingletonTraits<ViewStorage>;
     53 
     54   ViewStorage();
     55   ~ViewStorage();
     56 
     57   // Removes the view associated with |storage_id|. If |remove_all_ids| is true,
     58   // all other mapping pointing to the same view are removed as well.
     59   void EraseView(int storage_id, bool remove_all_ids);
     60 
     61   // Next id for the view storage.
     62   int view_storage_next_id_;
     63 
     64   // The association id to View used for the view storage.
     65   std::map<int, View*> id_to_view_;
     66 
     67   // Association View to id, used to speed up view notification removal.
     68   std::map<View*, std::vector<int>*> view_to_ids_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(ViewStorage);
     71 };
     72 
     73 }  // namespace views
     74 
     75 #endif  // UI_VIEWS_FOCUS_VIEW_STORAGE_H_
     76