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