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_POLICY_CORE_BROWSER_POLICY_ERROR_MAP_H_
6#define COMPONENTS_POLICY_CORE_BROWSER_POLICY_ERROR_MAP_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/memory/scoped_vector.h"
14#include "base/strings/string16.h"
15#include "components/policy/policy_export.h"
16
17namespace policy {
18
19// Collects error messages and their associated policies.
20class POLICY_EXPORT PolicyErrorMap {
21 public:
22  typedef std::multimap<std::string, base::string16> PolicyMapType;
23  typedef PolicyMapType::const_iterator const_iterator;
24
25  class PendingError;
26
27  PolicyErrorMap();
28  virtual ~PolicyErrorMap();
29
30  // Returns true when the errors logged are ready to be retrieved. It is always
31  // safe to call AddError, but the other methods are only allowed once
32  // IsReady is true. IsReady will be true once the UI message loop has started.
33  bool IsReady() const;
34
35  // Adds an entry with key |policy| and the error message corresponding to
36  // |message_id| in grit/generated_resources.h to the map.
37  void AddError(const std::string& policy, int message_id);
38
39  // Adds an entry with key |policy|, subkey |subkey|, and the error message
40  // corresponding to |message_id| in grit/generated_resources.h to the map.
41  void AddError(const std::string& policy,
42                const std::string& subkey,
43                int message_id);
44
45  // Adds an entry with key |policy|, list index |index|, and the error message
46  // corresponding to |message_id| in grit/generated_resources.h to the map.
47  void AddError(const std::string& policy,
48                int index,
49                int message_id);
50
51  // Adds an entry with key |policy| and the error message corresponding to
52  // |message_id| in grit/generated_resources.h to the map and replaces the
53  // placeholder within the error message with |replacement_string|.
54  void AddError(const std::string& policy,
55                int message_id,
56                const std::string& replacement_string);
57
58  // Adds an entry with key |policy|, subkey |subkey| and the error message
59  // corresponding to |message_id| in grit/generated_resources.h to the map.
60  // Replaces the placeholder in the error message with |replacement_string|.
61  void AddError(const std::string& policy,
62                const std::string& subkey,
63                int message_id,
64                const std::string& replacement_string);
65
66  // Adds an entry with key |policy|, list index |index| and the error message
67  // corresponding to |message_id| in grit/generated_resources.h to the map.
68  // Replaces the placeholder in the error message with |replacement_string|.
69  void AddError(const std::string& policy,
70                int index,
71                int message_id,
72                const std::string& replacement_string);
73
74  // Adds an entry with key |policy|, the schema validation error location
75  // |error_path|, and detailed error |message|.
76  void AddError(const std::string& policy,
77                const std::string& error_path,
78                const std::string& message);
79
80  // Returns all the error messages stored for |policy|, separated by a white
81  // space. Returns an empty string if there are no errors for |policy|.
82  base::string16 GetErrors(const std::string& policy);
83
84  bool empty();
85  size_t size();
86
87  const_iterator begin();
88  const_iterator end();
89
90  void Clear();
91
92 private:
93  // Maps the error when ready, otherwise adds it to the pending errors list.
94  void AddError(PendingError* error);
95
96  // Converts a PendingError into a |map_| entry.
97  void Convert(PendingError* error);
98
99  // Converts all pending errors to |map_| entries.
100  void CheckReadyAndConvert();
101
102  ScopedVector<PendingError> pending_;
103  PolicyMapType map_;
104
105  DISALLOW_COPY_AND_ASSIGN(PolicyErrorMap);
106};
107
108}  // namespace policy
109
110#endif  // COMPONENTS_POLICY_CORE_BROWSER_POLICY_ERROR_MAP_H_
111