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
22class Pickle;
23class PickleIterator;
24
25namespace content {
26class BrowserContext;
27class NavigationEntry;
28}
29
30namespace sync_pb {
31class TabNavigation;
32}
33
34namespace sessions {
35
36class SerializedNavigationEntryTestHelper;
37
38// The key used to store search terms data in the NavigationEntry.
39SESSIONS_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.
47class 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 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_;
107  }
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_; }
113
114  BlockedState blocked_state() { return blocked_state_; }
115  void set_blocked_state(BlockedState blocked_state) {
116    blocked_state_ = blocked_state;
117  }
118  std::set<std::string> content_pack_categories() {
119    return content_pack_categories_;
120  }
121  void set_content_pack_categories(
122      const std::set<std::string>& content_pack_categories) {
123    content_pack_categories_ = content_pack_categories;
124  }
125
126  // Converts a set of SerializedNavigationEntrys into a list of
127  // NavigationEntrys with sequential page IDs and the given context. The caller
128  // owns the returned NavigationEntrys.
129  static std::vector<content::NavigationEntry*> ToNavigationEntries(
130      const std::vector<SerializedNavigationEntry>& navigations,
131      content::BrowserContext* browser_context);
132
133 private:
134  friend class SerializedNavigationEntryTestHelper;
135
136  // Index in the NavigationController.
137  int index_;
138
139  // Member variables corresponding to NavigationEntry fields.
140  int unique_id_;
141  content::Referrer referrer_;
142  GURL virtual_url_;
143  base::string16 title_;
144  content::PageState page_state_;
145  content::PageTransition transition_type_;
146  bool has_post_data_;
147  int64 post_id_;
148  GURL original_request_url_;
149  bool is_overriding_user_agent_;
150  base::Time timestamp_;
151  base::string16 search_terms_;
152  GURL favicon_url_;
153  int http_status_code_;
154
155  // Additional information.
156  BlockedState blocked_state_;
157  std::set<std::string> content_pack_categories_;
158};
159
160}  // namespace sessions
161
162#endif  // COMPONENTS_SESSIONS_SERIALIZED_NAVIGATION_ENTRY_H_
163