summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profile_resetter/jtl_interpreter.h
blob: cafb7fdb01abbf91c32d54bcb9474ca75d295a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PROFILE_RESETTER_JTL_INTERPRETER_H_
#define CHROME_BROWSER_PROFILE_RESETTER_JTL_INTERPRETER_H_

#include <string>

#include "base/basictypes.h"
#include "base/values.h"

// Executes a JTL program on a given dictionary.
//
// JTL (Json Traversal Language) programs are defined in jtl_foundation.h
class JtlInterpreter {
 public:
  enum Result {
    OK,
    PARSE_ERROR,
    RUNTIME_ERROR,
    RESULT_MAX,
  };

  // |hasher_seed| is a value used in jtl_foundation::Hasher. All node names,
  // strings, integers and doubles are hashed before being compared to hash
  // values listed in |program|.
  // |program| is a byte array containing a JTL program.
  // |input| is a dictionary on which the program is evaluated.
  JtlInterpreter(const std::string& hasher_seed,
                 const std::string& program,
                 const base::DictionaryValue* input);
  ~JtlInterpreter();

  void Execute();

  Result result() const { return result_; }
  const base::DictionaryValue* working_memory() const {
    return working_memory_.get();
  }
  bool GetOutputBoolean(const std::string& unhashed_key, bool* output) const;
  bool GetOutputString(const std::string& unhashed_key,
                       std::string* output) const;

  // Generates a checksum of the loaded program, defined as the first 3 bytes of
  // the program's SHA-256 hash interpreted as a big-endian integer.
  int CalculateProgramChecksum() const;

 private:
  // Input.
  std::string hasher_seed_;
  std::string program_;
  const base::DictionaryValue* input_;
  // Output.
  scoped_ptr<base::DictionaryValue> working_memory_;
  Result result_;

  DISALLOW_COPY_AND_ASSIGN(JtlInterpreter);
};

#endif  // CHROME_BROWSER_PROFILE_RESETTER_JTL_INTERPRETER_H_