blob: 6861e70b1bbf2073689aaf8af16614e30a1d0481 [file] [log] [blame]
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001// Copyright (c) 2012 The Chromium OS 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
7
8#include "base/file_path.h"
9
10#include "update_engine/action_processor.h"
11#include "update_engine/prefs_interface.h"
12
13namespace chromeos_update_engine {
14
15// Forward declaration here because we get a circular dependency if
16// we include omaha_request_action.h directly.
17struct OmahaResponse;
18
19// Encapsulates all the payload state required for download. This includes the
20// state necessary for handling multiple URLs in Omaha response, the back-off
21// state, etc. All state is persisted so that we use the most recently saved
22// value when resuming the update_engine process. All state is also cached in
23// memory so that we ensure we always make progress based on last known good
24// state even when there's any issue in reading/writing from the file system.
25class PayloadState {
26 public:
27
28 PayloadState() : prefs_(NULL), num_urls_(0), url_index_(0) {}
29
30 // Initializes a payload state object using |prefs| for storing the
31 // persisted state. It also performs the initial loading of all persisted
32 // state into memory and dumps the initial state for debugging purposes.
33 // Note: the other methods should be called only after calling Initialize
34 // on this object.
35 bool Initialize(PrefsInterface* prefs);
36
37 // Logs the current payload state.
38 void LogPayloadState();
39
40 // Sets the internal payload state based on the given Omaha response. This
41 // response could be the same or different from the one for which we've stored
42 // the internal state. If it's different, then this method resets all the
43 // internal state corresponding to the old response. Since the Omaha response
44 // has a lot of fields that are not related to payload state, it uses only
45 // a subset of the fields in the Omaha response to compare equality.
46 void SetResponse(const OmahaResponse& response);
47
48 // Updates the payload state when the current update attempt has failed.
49 void UpdateFailed(ActionExitCode error);
50
51 // Returns the internally stored subset of the response state as a string.
52 // This is logically a private method, but exposed here for unit tests.
53 std::string GetResponse() {
54 return response_;
55 }
56
57 // Returns the current URL index.
58 uint32_t GetUrlIndex() {
59 return url_index_;
60 }
61
62 private:
63 // Sets the stored response_ value from the currently persisted value for
64 // the response. Returns the same value.
65 std::string LoadResponse();
66
67 // Sets the url_index_ value from the currently persisted value for
68 // URL index. Returns the same value.
69 uint32_t LoadUrlIndex();
70
71 // Sets the current URL index.
72 void SetUrlIndex(uint32_t url_index);
73
74 // Interface object with which we read/write persisted state. This must
75 // be set by calling the Initialize method before calling any other method.
76 PrefsInterface* prefs_;
77
78 // Cached value of the latest subset of the Omaha response with which we're
79 // working off currently. This value is persisted so we load it off the next
80 // time when update_engine restarts. The rest of the state in this class will
81 // be cleared when we set a new response.
82 std::string response_;
83
84 // The number of urls in the current response. Not persisted.
85 uint32_t num_urls_;
86
87 // Cached value of the index of the current URL, to be used in case we are
88 // unable to read from the persisted store for any reason. This type is
89 // different from the one in the accessor methods because PrefsInterface
90 // supports only int64_t but we want to provide a stronger abstraction of
91 // uint32_t.
92 int64_t url_index_;
93
94 DISALLOW_COPY_AND_ASSIGN(PayloadState);
95};
96
97} // namespace chromeos_update_engine
98
99#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__