Home | History | Annotate | Download | only in sessions
      1 // Copyright 2013 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 COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_
      6 #define COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/strings/string16.h"
     15 #include "base/time/time.h"
     16 #include "components/sessions/sessions_export.h"
     17 #include "content/public/common/page_state.h"
     18 #include "content/public/common/page_transition_types.h"
     19 #include "content/public/common/referrer.h"
     20 #include "url/gurl.h"
     21 
     22 class Pickle;
     23 class PickleIterator;
     24 
     25 namespace content {
     26 class BrowserContext;
     27 class NavigationEntry;
     28 }
     29 
     30 namespace sync_pb {
     31 class TabNavigation;
     32 }
     33 
     34 namespace sessions {
     35 
     36 class SerializedNavigationEntryTestHelper;
     37 
     38 // The key used to store search terms data in the NavigationEntry.
     39 SESSIONS_EXPORT extern const char kSearchTermsKey[];
     40 
     41 // SerializedNavigationEntry is a "freeze-dried" version of NavigationEntry.  It
     42 // contains the data needed to restore a NavigationEntry during session restore
     43 // and tab restore, and it can also be pickled and unpickled.  It is also
     44 // convertible to a sync protocol buffer for session syncing.
     45 //
     46 // Default copy constructor and assignment operator welcome.
     47 class SESSIONS_EXPORT SerializedNavigationEntry {
     48  public:
     49   enum BlockedState {
     50     STATE_INVALID = 0,
     51     STATE_ALLOWED = 1,
     52     STATE_BLOCKED = 2,
     53   };
     54 
     55   // Creates an invalid (index < 0) SerializedNavigationEntry.
     56   SerializedNavigationEntry();
     57   ~SerializedNavigationEntry();
     58 
     59   // Construct a SerializedNavigationEntry for a particular index from the given
     60   // NavigationEntry.
     61   static SerializedNavigationEntry FromNavigationEntry(
     62       int index,
     63       const content::NavigationEntry& entry);
     64 
     65   // Construct a SerializedNavigationEntry for a particular index from a sync
     66   // protocol buffer.  Note that the sync protocol buffer doesn't contain all
     67   // SerializedNavigationEntry fields.  Also, the timestamp of the returned
     68   // SerializedNavigationEntry is nulled out, as we assume that the protocol
     69   // buffer is from a foreign session.
     70   static SerializedNavigationEntry FromSyncData(
     71       int index,
     72       const sync_pb::TabNavigation& sync_data);
     73 
     74   // Note that not all SerializedNavigationEntry fields are preserved.
     75   // |max_size| is the max number of bytes to write.
     76   void WriteToPickle(int max_size, Pickle* pickle) const;
     77   bool ReadFromPickle(PickleIterator* iterator);
     78 
     79   // Convert this SerializedNavigationEntry into a NavigationEntry with the
     80   // given page ID and context.  The NavigationEntry will have a transition type
     81   // of PAGE_TRANSITION_RELOAD and a new unique ID.
     82   scoped_ptr<content::NavigationEntry> ToNavigationEntry(
     83       int page_id,
     84       content::BrowserContext* browser_context) const;
     85 
     86   // Convert this navigation into its sync protocol buffer equivalent.  Note
     87   // that the protocol buffer doesn't contain all SerializedNavigationEntry
     88   // fields.
     89   sync_pb::TabNavigation ToSyncData() const;
     90 
     91   // The index in the NavigationController. This SerializedNavigationEntry is
     92   // valid only when the index is non-negative.
     93   int index() const { return index_; }
     94   void set_index(int index) { index_ = index; }
     95 
     96   // Accessors for some fields taken from NavigationEntry.
     97   int unique_id() const { return unique_id_; }
     98   const GURL& virtual_url() const { return virtual_url_; }
     99   const string16& title() const { return title_; }
    100   const content::PageState& page_state() const { return page_state_; }
    101   const string16& search_terms() const { return search_terms_; }
    102   const GURL& favicon_url() const { return favicon_url_; }
    103   const content::Referrer& referrer() const { return referrer_; }
    104   content::PageTransition transition_type() const {
    105     return transition_type_;
    106   }
    107   bool has_post_data() const { return has_post_data_; }
    108   int64 post_id() const { return post_id_; }
    109   const GURL& original_request_url() const { return original_request_url_; }
    110   bool is_overriding_user_agent() const { return is_overriding_user_agent_; }
    111   base::Time timestamp() const { return timestamp_; }
    112 
    113   BlockedState blocked_state() { return blocked_state_; }
    114   void set_blocked_state(BlockedState blocked_state) {
    115     blocked_state_ = blocked_state;
    116   }
    117   std::set<std::string> content_pack_categories() {
    118     return content_pack_categories_;
    119   }
    120   void set_content_pack_categories(
    121       const std::set<std::string>& content_pack_categories) {
    122     content_pack_categories_ = content_pack_categories;
    123   }
    124 
    125   // Converts a set of SerializedNavigationEntrys into a list of
    126   // NavigationEntrys with sequential page IDs and the given context. The caller
    127   // owns the returned NavigationEntrys.
    128   static std::vector<content::NavigationEntry*> ToNavigationEntries(
    129       const std::vector<SerializedNavigationEntry>& navigations,
    130       content::BrowserContext* browser_context);
    131 
    132  private:
    133   friend class SerializedNavigationEntryTestHelper;
    134 
    135   // Index in the NavigationController.
    136   int index_;
    137 
    138   // Member variables corresponding to NavigationEntry fields.
    139   int unique_id_;
    140   content::Referrer referrer_;
    141   GURL virtual_url_;
    142   string16 title_;
    143   content::PageState page_state_;
    144   content::PageTransition transition_type_;
    145   bool has_post_data_;
    146   int64 post_id_;
    147   GURL original_request_url_;
    148   bool is_overriding_user_agent_;
    149   base::Time timestamp_;
    150   string16 search_terms_;
    151   GURL favicon_url_;
    152 
    153   // Additional information.
    154   BlockedState blocked_state_;
    155   std::set<std::string> content_pack_categories_;
    156 };
    157 
    158 }  // namespace sessions
    159 
    160 #endif  // COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_
    161