Home | History | Annotate | Download | only in glue
      1 // Copyright (c) 2011 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 CHROME_BROWSER_SYNC_GLUE_FOREIGN_SESSION_TRACKER_H_
      6 #define CHROME_BROWSER_SYNC_GLUE_FOREIGN_SESSION_TRACKER_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <set>
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/basictypes.h"
     15 #include "base/memory/scoped_vector.h"
     16 #include "chrome/browser/sessions/session_id.h"
     17 #include "chrome/browser/sessions/session_types.h"
     18 
     19 namespace browser_sync {
     20 
     21 // Class to manage foreign sessions. The tracker will own all ForeignSession
     22 // and SessionTab objects it creates, and deletes them appropriately on
     23 // destruction.
     24 class ForeignSessionTracker {
     25  public:
     26   ForeignSessionTracker();
     27   ~ForeignSessionTracker();
     28 
     29   // Fill a preallocated vector with all foreign sessions we're tracking.
     30   // Returns true if we had foreign sessions to fill it with, false otherwise.
     31   bool LookupAllForeignSessions(std::vector<const ForeignSession*>* sessions);
     32 
     33   // Attempts to look up the session windows associatd with the foreign session
     34   // given by |foreign_session_tag|.
     35   // If lookup succeeds:
     36   // - Fills windows with the SessionWindow pointers, returns true.
     37   // Else
     38   // - Returns false.
     39   bool LookupSessionWindows(const std::string& foreign_session_tag,
     40                             std::vector<SessionWindow*>* windows);
     41 
     42   // Attempts to look up the foreign tab associated with the given tag and tab
     43   // id.
     44   // If lookup succeeds:
     45   // - Sets tab to point to the SessionTab, and returns true.
     46   // Else
     47   // - Returns false, tab is set to NULL.
     48   bool LookupSessionTab(const std::string& foreign_session_tag,
     49                         SessionID::id_type tab_id,
     50                         const SessionTab** tab);
     51 
     52   // Returns a pointer to the ForeignSession object associated with
     53   // foreign_session_tag. If none exists, creates one and returns its pointer.
     54   ForeignSession* GetForeignSession(const std::string& foreign_session_tag);
     55 
     56   // Deletes the foreign session associated with |foreign_session_tag| if it
     57   // exists.
     58   // Returns true if the session existed and was deleted, false otherwise.
     59   bool DeleteForeignSession(const std::string& foreign_session_tag);
     60 
     61   // Returns a pointer to the SessionTab object associated with |tab_id| for
     62   // the session specified with |foreign_session_tag|. If none exists, creates
     63   // one and returns its pointer.
     64   // |has_window| determines if newly created tabs are added to the pool of
     65   // orphaned tabs (thos which can't be reached by traversing foreign sessions).
     66   SessionTab* GetSessionTab(const std::string& foreign_session_tag,
     67                             SessionID::id_type tab_id,
     68                             bool has_window);
     69 
     70   // Free the memory for all dynamically allocated objects and clear the
     71   // tracking structures.
     72   void clear();
     73 
     74   inline bool empty() {
     75     return foreign_tab_map_.empty() && foreign_session_map_.empty();
     76   }
     77 
     78   inline size_t num_foreign_sessions() {
     79     return foreign_session_map_.size();
     80   }
     81 
     82   inline size_t num_foreign_tabs(const std::string& foreign_session_tag) {
     83     if (foreign_tab_map_.find(foreign_session_tag) != foreign_tab_map_.end()) {
     84       return foreign_tab_map_[foreign_session_tag]->size();
     85     } else {
     86       return 0;
     87     }
     88   }
     89  private:
     90   // Datatypes for accessing foreign tab data.
     91   typedef std::map<SessionID::id_type, SessionTab*> IDToSessionTabMap;
     92   typedef std::map<std::string, IDToSessionTabMap*> ForeignTabMap;
     93   typedef std::map<std::string, ForeignSession*> ForeignSessionMap;
     94 
     95   // Per foreign client mapping of their tab id's to their SessionTab objects.
     96   ForeignTabMap foreign_tab_map_;
     97 
     98   // Map of foreign sessions, accessed by the foreign client id.
     99   ForeignSessionMap foreign_session_map_;
    100 
    101   // The set of foreign tabs that we have seen, and created SessionTab objects
    102   // for, but have not yet mapped to ForeignSessions. These are temporarily
    103   // orphaned tabs, and won't be deleted if we delete foreign_session_map_.
    104   std::set<SessionTab*> unmapped_tabs_;
    105 
    106   DISALLOW_COPY_AND_ASSIGN(ForeignSessionTracker);
    107 };
    108 
    109 }  // namespace browser_sync
    110 
    111 #endif  // CHROME_BROWSER_SYNC_GLUE_FOREIGN_SESSION_TRACKER_H_
    112