| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_ | 5 #ifndef COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_ |
| 6 #define COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_ | 6 #define COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/sequence_checker.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "base/threading/non_thread_safe.h" | |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "components/login/base_screen_handler_utils.h" | 16 #include "components/login/base_screen_handler_utils.h" |
| 17 #include "components/login/login_export.h" | 17 #include "components/login/login_export.h" |
| 18 | 18 |
| 19 namespace login { | 19 namespace login { |
| 20 | 20 |
| 21 // ScreenContext is a key-value storage for values that are shared | 21 // ScreenContext is a key-value storage for values that are shared |
| 22 // between C++ and JS sides. Objects of this class should be used in | 22 // between C++ and JS sides. Objects of this class should be used in |
| 23 // the following way: | 23 // the following way: |
| 24 // | 24 // |
| 25 // context.SetString("user", "john"); | 25 // context.SetString("user", "john"); |
| 26 // context.SetInteger("image-index", 0); | 26 // context.SetInteger("image-index", 0); |
| 27 // context.SetDouble("zoom", 1.25); | 27 // context.SetDouble("zoom", 1.25); |
| 28 // context.GetChangesAndReset(&dictionary); | 28 // context.GetChangesAndReset(&dictionary); |
| 29 // CallJS("onContextChanged", dictionary); | 29 // CallJS("onContextChanged", dictionary); |
| 30 // | 30 // |
| 31 // ScreenContext memorizes changed key-value pairs and returns them | 31 // ScreenContext memorizes changed key-value pairs and returns them |
| 32 // via GetChangesAndReset() method. After call to this method an | 32 // via GetChangesAndReset() method. After call to this method an |
| 33 // internal buffer of changes will be cleared. | 33 // internal buffer of changes will be cleared. |
| 34 class LOGIN_EXPORT ScreenContext : public base::NonThreadSafe { | 34 class LOGIN_EXPORT ScreenContext { |
| 35 public: | 35 public: |
| 36 typedef std::string KeyType; | 36 typedef std::string KeyType; |
| 37 typedef base::Value ValueType; | 37 typedef base::Value ValueType; |
| 38 | 38 |
| 39 ScreenContext(); | 39 ScreenContext(); |
| 40 ~ScreenContext(); | 40 ~ScreenContext(); |
| 41 | 41 |
| 42 bool SetBoolean(const KeyType& key, bool value); | 42 bool SetBoolean(const KeyType& key, bool value); |
| 43 bool SetInteger(const KeyType& key, int value); | 43 bool SetInteger(const KeyType& key, int value); |
| 44 bool SetDouble(const KeyType& key, double value); | 44 bool SetDouble(const KeyType& key, double value); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 std::vector<std::string>* keys); | 87 std::vector<std::string>* keys); |
| 88 | 88 |
| 89 // Returns underlying dictionary containing all the stored data. | 89 // Returns underlying dictionary containing all the stored data. |
| 90 const base::DictionaryValue& storage() const { return storage_; } | 90 const base::DictionaryValue& storage() const { return storage_; } |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 bool Set(const KeyType& key, base::Value* value); | 93 bool Set(const KeyType& key, base::Value* value); |
| 94 | 94 |
| 95 template <typename T> | 95 template <typename T> |
| 96 T Get(const KeyType& key) const { | 96 T Get(const KeyType& key) const { |
| 97 DCHECK(CalledOnValidThread()); | 97 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 98 const base::Value* value; | 98 const base::Value* value; |
| 99 bool has_key = storage_.Get(key, &value); | 99 bool has_key = storage_.Get(key, &value); |
| 100 DCHECK(has_key); | 100 DCHECK(has_key); |
| 101 T result; | 101 T result; |
| 102 if (!ParseValue(value, &result)) { | 102 if (!ParseValue(value, &result)) { |
| 103 NOTREACHED(); | 103 NOTREACHED(); |
| 104 return T(); | 104 return T(); |
| 105 } | 105 } |
| 106 return result; | 106 return result; |
| 107 } | 107 } |
| 108 | 108 |
| 109 template <typename T> | 109 template <typename T> |
| 110 T Get(const KeyType& key, const T& default_value) const { | 110 T Get(const KeyType& key, const T& default_value) const { |
| 111 DCHECK(CalledOnValidThread()); | 111 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 112 if (!HasKey(key)) | 112 if (!HasKey(key)) |
| 113 return default_value; | 113 return default_value; |
| 114 return Get<T>(key); | 114 return Get<T>(key); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Contains current state of <key, value> map. | 117 // Contains current state of <key, value> map. |
| 118 base::DictionaryValue storage_; | 118 base::DictionaryValue storage_; |
| 119 | 119 |
| 120 // Contains all pending changes. | 120 // Contains all pending changes. |
| 121 base::DictionaryValue changes_; | 121 base::DictionaryValue changes_; |
| 122 | 122 |
| 123 SEQUENCE_CHECKER(sequence_checker_); |
| 124 |
| 123 DISALLOW_COPY_AND_ASSIGN(ScreenContext); | 125 DISALLOW_COPY_AND_ASSIGN(ScreenContext); |
| 124 }; | 126 }; |
| 125 | 127 |
| 126 } // namespace login | 128 } // namespace login |
| 127 | 129 |
| 128 #endif // COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_ | 130 #endif // COMPONENTS_LOGIN_SCREENS_SCREEN_CONTEXT_H_ |
| OLD | NEW |