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