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/referrer.h" 19 #include "ui/base/page_transition_types.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. index()93 int index() const { return index_; } set_index(int index)94 void set_index(int index) { index_ = index; } 95 96 // Accessors for some fields taken from NavigationEntry. unique_id()97 int unique_id() const { return unique_id_; } virtual_url()98 const GURL& virtual_url() const { return virtual_url_; } title()99 const base::string16& title() const { return title_; } page_state()100 const content::PageState& page_state() const { return page_state_; } search_terms()101 const base::string16& search_terms() const { return search_terms_; } favicon_url()102 const GURL& favicon_url() const { return favicon_url_; } http_status_code()103 int http_status_code() const { return http_status_code_; } referrer()104 const content::Referrer& referrer() const { return referrer_; } transition_type()105 ui::PageTransition transition_type() const { 106 return transition_type_; 107 } has_post_data()108 bool has_post_data() const { return has_post_data_; } post_id()109 int64 post_id() const { return post_id_; } original_request_url()110 const GURL& original_request_url() const { return original_request_url_; } is_overriding_user_agent()111 bool is_overriding_user_agent() const { return is_overriding_user_agent_; } timestamp()112 base::Time timestamp() const { return timestamp_; } 113 blocked_state()114 BlockedState blocked_state() { return blocked_state_; } set_blocked_state(BlockedState blocked_state)115 void set_blocked_state(BlockedState blocked_state) { 116 blocked_state_ = blocked_state; 117 } content_pack_categories()118 std::set<std::string> content_pack_categories() { 119 return content_pack_categories_; 120 } set_content_pack_categories(const std::set<std::string> & content_pack_categories)121 void set_content_pack_categories( 122 const std::set<std::string>& content_pack_categories) { 123 content_pack_categories_ = content_pack_categories; 124 } redirect_chain()125 const std::vector<GURL>& redirect_chain() const { return redirect_chain_; } 126 127 // Converts a set of SerializedNavigationEntrys into a list of 128 // NavigationEntrys with sequential page IDs and the given context. The caller 129 // owns the returned NavigationEntrys. 130 static std::vector<content::NavigationEntry*> ToNavigationEntries( 131 const std::vector<SerializedNavigationEntry>& navigations, 132 content::BrowserContext* browser_context); 133 134 private: 135 friend class SerializedNavigationEntryTestHelper; 136 137 // Sanitizes the data in this class to be more robust against faulty data 138 // written by older versions. 139 void Sanitize(); 140 141 // Index in the NavigationController. 142 int index_; 143 144 // Member variables corresponding to NavigationEntry fields. 145 int unique_id_; 146 content::Referrer referrer_; 147 GURL virtual_url_; 148 base::string16 title_; 149 content::PageState page_state_; 150 ui::PageTransition transition_type_; 151 bool has_post_data_; 152 int64 post_id_; 153 GURL original_request_url_; 154 bool is_overriding_user_agent_; 155 base::Time timestamp_; 156 base::string16 search_terms_; 157 GURL favicon_url_; 158 int http_status_code_; 159 bool is_restored_; // Not persisted. 160 std::vector<GURL> redirect_chain_; // Not persisted. 161 162 // Additional information. 163 BlockedState blocked_state_; 164 std::set<std::string> content_pack_categories_; 165 }; 166 167 } // namespace sessions 168 169 #endif // COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_ 170