session_command.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2006-2008 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_SESSIONS_SESSION_COMMAND_H_
6#define CHROME_BROWSER_SESSIONS_SESSION_COMMAND_H_
7#pragma once
8
9#include <string>
10
11#include "base/basictypes.h"
12
13class Pickle;
14
15// SessionCommand contains a command id and arbitrary chunk of data. The id
16// and chunk of data are specific to the service creating them.
17//
18// Both TabRestoreService and SessionService use SessionCommands to represent
19// state on disk.
20//
21// There are two ways to create a SessionCommand:
22// . Specificy the size of the data block to create. This is useful for
23//   commands that have a fixed size.
24// . From a pickle, this is useful for commands whose length varies.
25class SessionCommand {
26 public:
27  // These get written to disk, so we define types for them.
28  // Type for the identifier.
29  typedef uint8 id_type;
30  // Type for writing the size.
31  typedef uint16 size_type;
32
33  // Creates a session command with the specified id. This allocates a buffer
34  // of size |size| that must be filled via contents().
35  SessionCommand(id_type id, size_type size);
36
37  // Convenience constructor that creates a session command with the specified
38  // id whose contents is populated from the contents of pickle.
39  SessionCommand(id_type id, const Pickle& pickle);
40
41  // The contents of the command.
42  char* contents() { return &(contents_[0]); }
43  const char* contents() const { return &(contents_[0]); }
44
45  // Identifier for the command.
46  id_type id() const { return id_; }
47
48  // Size of data.
49  size_type size() const { return static_cast<size_type>(contents_.size()); }
50
51  // Convenience for extracting the data to a target. Returns false if
52  // count is not equal to the size of data this command contains.
53  bool GetPayload(void* dest, size_t count) const;
54
55  // Returns the contents as a pickle. It is up to the caller to delete the
56  // returned Pickle. The returned Pickle references the underlying data of
57  // this SessionCommand. If you need it to outlive the command, copy the
58  // pickle.
59  Pickle* PayloadAsPickle() const;
60
61 private:
62  const id_type id_;
63  std::string contents_;
64
65  DISALLOW_COPY_AND_ASSIGN(SessionCommand);
66};
67
68#endif  // CHROME_BROWSER_SESSIONS_SESSION_COMMAND_H_
69