Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / sessions / serialized_navigation_entry.h
blob0582c7e2d862d564807493a07ecc8a97ef1d71b0
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.
5 #ifndef COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_
6 #define COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_
8 #include <set>
9 #include <string>
10 #include <vector>
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"
22 class Pickle;
23 class PickleIterator;
25 namespace content {
26 class BrowserContext;
27 class NavigationEntry;
30 namespace sync_pb {
31 class TabNavigation;
34 namespace sessions {
36 class SerializedNavigationEntryTestHelper;
38 // The key used to store search terms data in the NavigationEntry.
39 SESSIONS_EXPORT extern const char kSearchTermsKey[];
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.
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,
55 // Creates an invalid (index < 0) SerializedNavigationEntry.
56 SerializedNavigationEntry();
57 ~SerializedNavigationEntry();
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);
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);
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);
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;
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;
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; }
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 base::string16& title() const { return title_; }
100 const content::PageState& page_state() const { return page_state_; }
101 const base::string16& search_terms() const { return search_terms_; }
102 const GURL& favicon_url() const { return favicon_url_; }
103 int http_status_code() const { return http_status_code_; }
104 const content::Referrer& referrer() const { return referrer_; }
105 content::PageTransition transition_type() const {
106 return transition_type_;
108 bool has_post_data() const { return has_post_data_; }
109 int64 post_id() const { return post_id_; }
110 const GURL& original_request_url() const { return original_request_url_; }
111 bool is_overriding_user_agent() const { return is_overriding_user_agent_; }
112 base::Time timestamp() const { return timestamp_; }
114 BlockedState blocked_state() { return blocked_state_; }
115 void set_blocked_state(BlockedState blocked_state) {
116 blocked_state_ = blocked_state;
118 std::set<std::string> content_pack_categories() {
119 return 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;
125 const std::vector<GURL>& redirect_chain() const { return redirect_chain_; }
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);
134 private:
135 friend class SerializedNavigationEntryTestHelper;
137 // Sanitizes the data in this class to be more robust against faulty data
138 // written by older versions.
139 void Sanitize();
141 // Index in the NavigationController.
142 int index_;
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 content::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.
162 // Additional information.
163 BlockedState blocked_state_;
164 std::set<std::string> content_pack_categories_;
167 } // namespace sessions
169 #endif // COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_