Chromium Code Reviews| Index: chrome/browser/android/vr_shell/ui_scene.cc |
| diff --git a/chrome/browser/android/vr_shell/ui_scene.cc b/chrome/browser/android/vr_shell/ui_scene.cc |
| index b2ecb39eae100f0b8f2c5854bac99be484530fee..0841b6cf957442eb63ecb9c8998048d79ac92c40 100644 |
| --- a/chrome/browser/android/vr_shell/ui_scene.cc |
| +++ b/chrome/browser/android/vr_shell/ui_scene.cc |
| @@ -16,9 +16,19 @@ namespace vr_shell { |
| namespace { |
| +// Parse an integer to an int or enum value. |
| +template <typename T> |
| +bool ParseInt(const base::DictionaryValue& dict, |
|
tiborg
2017/02/23 15:21:18
+1 for the templates!
cjgrant
2017/02/23 16:13:37
Acknowledged.
|
| + const std::string& key, |
| + T* output) { |
| + return dict.GetInteger(key, reinterpret_cast<int*>(output)); |
|
tiborg
2017/02/23 15:21:18
Is this safe when you use a different type than in
cjgrant
2017/02/23 16:13:37
Done. It was not, but it is now.
|
| +} |
| + |
| +// Parse a floating point number to a float or double. |
| +template <typename T> |
| bool ParseFloat(const base::DictionaryValue& dict, |
| const std::string& key, |
| - float* output) { |
| + T* output) { |
| double value; |
| if (!dict.GetDouble(key, &value)) { |
| return false; |
| @@ -27,56 +37,6 @@ bool ParseFloat(const base::DictionaryValue& dict, |
| return true; |
| } |
| -bool ParseRecti(const base::DictionaryValue& dict, |
| - const std::string& key, |
| - Recti* output) { |
| - const base::DictionaryValue* item_dict; |
| - if (dict.GetDictionary(key, &item_dict)) { |
| - CHECK(item_dict->GetInteger("x", &output->x)); |
| - CHECK(item_dict->GetInteger("y", &output->y)); |
| - CHECK(item_dict->GetInteger("width", &output->width)); |
| - CHECK(item_dict->GetInteger("height", &output->height)); |
| - return true; |
| - } else { |
| - return false; |
| - } |
| -} |
| - |
| -bool Parse2DVec3f(const base::DictionaryValue& dict, |
| - const std::string& key, |
| - gvr::Vec3f* output) { |
| - const base::DictionaryValue* item_dict; |
| - if (dict.GetDictionary(key, &item_dict)) { |
| - double value; |
| - CHECK(item_dict->GetDouble("x", &value)); |
| - output->x = value; |
| - CHECK(item_dict->GetDouble("y", &value)); |
| - output->y = value; |
| - output->z = 1.0f; |
| - return true; |
| - } else { |
| - return false; |
| - } |
| -} |
| - |
| -bool ParseVec3f(const base::DictionaryValue& dict, |
| - const std::string& key, |
| - gvr::Vec3f* output) { |
| - const base::DictionaryValue* item_dict; |
| - if (dict.GetDictionary(key, &item_dict)) { |
| - double value; |
| - CHECK(item_dict->GetDouble("x", &value)); |
| - output->x = value; |
| - CHECK(item_dict->GetDouble("y", &value)); |
| - output->y = value; |
| - CHECK(item_dict->GetDouble("z", &value)); |
| - output->z = value; |
| - return true; |
| - } else { |
| - return false; |
| - } |
| -} |
| - |
| bool ParseColorf(const base::DictionaryValue& dict, |
| const std::string& key, |
| Colorf* output) { |
| @@ -97,26 +57,6 @@ bool ParseColorf(const base::DictionaryValue& dict, |
| } |
| } |
| -bool ParseRotationAxisAngle(const base::DictionaryValue& dict, |
| - const std::string& key, |
| - RotationAxisAngle* output) { |
| - const base::DictionaryValue* item_dict; |
| - if (dict.GetDictionary(key, &item_dict)) { |
| - double value; |
| - CHECK(item_dict->GetDouble("x", &value)); |
| - output->x = value; |
| - CHECK(item_dict->GetDouble("y", &value)); |
| - output->y = value; |
| - CHECK(item_dict->GetDouble("z", &value)); |
| - output->z = value; |
| - CHECK(item_dict->GetDouble("a", &value)); |
| - output->angle = value; |
| - return true; |
| - } else { |
| - return false; |
| - } |
| -} |
| - |
| void ParseFloats(const base::DictionaryValue& dict, |
| const std::vector<std::string>& keys, |
| std::vector<float>* vec) { |
| @@ -155,7 +95,7 @@ bool ParseEndpointToFloats(Animation::Property property, |
| std::unique_ptr<easing::Easing> ParseEasing(const base::DictionaryValue& dict) { |
| easing::EasingType easingType; |
| - CHECK(dict.GetInteger("type", reinterpret_cast<int*>(&easingType))); |
| + CHECK(ParseInt(dict, "type", &easingType)); |
| std::unique_ptr<easing::Easing> result; |
| switch (easingType) { |
| @@ -236,7 +176,7 @@ void UiScene::AddUiElement(std::unique_ptr<ContentRectangle>& element) { |
| void UiScene::AddUiElementFromDict(const base::DictionaryValue& dict) { |
| int id; |
| - CHECK(dict.GetInteger("id", &id)); |
| + CHECK(ParseInt(dict, "id", &id)); |
| CHECK_EQ(GetUiElementById(id), nullptr); |
| std::unique_ptr<ContentRectangle> element(new ContentRectangle); |
| @@ -248,7 +188,7 @@ void UiScene::AddUiElementFromDict(const base::DictionaryValue& dict) { |
| void UiScene::UpdateUiElementFromDict(const base::DictionaryValue& dict) { |
| int id; |
| - CHECK(dict.GetInteger("id", &id)); |
| + CHECK(ParseInt(dict, "id", &id)); |
| ContentRectangle* element = GetUiElementById(id); |
| CHECK_NE(element, nullptr); |
| ApplyDictToElement(dict, element); |
| @@ -290,11 +230,11 @@ void UiScene::AddAnimationFromDict(const base::DictionaryValue& dict, |
| std::vector<float> from; |
| std::vector<float> to; |
| - CHECK(dict.GetInteger("id", &animation_id)); |
| - CHECK(dict.GetInteger("meshId", &element_id)); |
| - CHECK(dict.GetInteger("property", reinterpret_cast<int*>(&property))); |
| - CHECK(dict.GetDouble("startInMillis", &start_time_ms)); |
| - CHECK(dict.GetDouble("durationMillis", &duration_ms)); |
| + CHECK(ParseInt(dict, "id", &animation_id)); |
| + CHECK(ParseInt(dict, "meshId", &element_id)); |
| + CHECK(ParseInt(dict, "property", &property)); |
| + CHECK(ParseFloat(dict, "startInMillis", &start_time_ms)); |
| + CHECK(ParseFloat(dict, "durationMillis", &duration_ms)); |
| CHECK(dict.GetDictionary("easing", &easing_dict)); |
| auto easing = ParseEasing(*easing_dict); |
| @@ -345,7 +285,7 @@ void UiScene::HandleCommands(std::unique_ptr<base::ListValue> commands, |
| Command type; |
| base::DictionaryValue* data; |
| - CHECK(dict->GetInteger("type", reinterpret_cast<int*>(&type))); |
| + CHECK(ParseInt(*dict, "type", &type)); |
| CHECK(dict->GetDictionary("data", &data)); |
| switch (type) { |
| @@ -357,7 +297,7 @@ void UiScene::HandleCommands(std::unique_ptr<base::ListValue> commands, |
| break; |
| case Command::REMOVE_ELEMENT: { |
| int element_id; |
| - CHECK(data->GetInteger("id", &element_id)); |
| + CHECK(ParseInt(*data, "id", &element_id)); |
| RemoveUiElement(element_id); |
| break; |
| } |
| @@ -366,8 +306,8 @@ void UiScene::HandleCommands(std::unique_ptr<base::ListValue> commands, |
| break; |
| case Command::REMOVE_ANIMATION: { |
| int element_id, animation_id; |
| - CHECK(data->GetInteger("id", &animation_id)); |
| - CHECK(data->GetInteger("meshId", &element_id)); |
| + CHECK(ParseInt(*data, "id", &animation_id)); |
| + CHECK(ParseInt(*data, "meshId", &element_id)); |
| RemoveAnimation(element_id, animation_id); |
| break; |
| } |
| @@ -441,7 +381,8 @@ void UiScene::ApplyRecursiveTransforms(const ContentRectangle& element, |
| void UiScene::ApplyDictToElement(const base::DictionaryValue& dict, |
| ContentRectangle* element) { |
| int parent_id; |
| - if (dict.GetInteger("parentId", &parent_id)) { |
| + |
| + if (ParseInt(dict, "parentId", &parent_id)) { |
| CHECK_GE(parent_id, 0); |
| CHECK_NE(GetUiElementById(parent_id), nullptr); |
| element->parent_id = parent_id; |
| @@ -450,27 +391,32 @@ void UiScene::ApplyDictToElement(const base::DictionaryValue& dict, |
| dict.GetBoolean("visible", &element->visible); |
| dict.GetBoolean("hitTestable", &element->hit_testable); |
| dict.GetBoolean("lockToFov", &element->lock_to_fov); |
| - Parse2DVec3f(dict, "size", &element->size); |
| - ParseVec3f(dict, "scale", &element->scale); |
| - ParseRotationAxisAngle(dict, "rotation", &element->rotation); |
| - ParseVec3f(dict, "translation", &element->translation); |
| - double opacity; |
| - if (dict.GetDouble("opacity", &opacity)) { |
| - element->opacity = opacity; |
| - } |
| - dict.GetInteger("drawPhase", &element->draw_phase); |
| - if (dict.GetInteger("xAnchoring", |
| - reinterpret_cast<int*>(&element->x_anchoring))) { |
| + ParseFloat(dict, "sizeX", &element->size.x); |
| + ParseFloat(dict, "sizeY", &element->size.y); |
| + ParseFloat(dict, "scaleX", &element->scale.x); |
| + ParseFloat(dict, "scaleY", &element->scale.y); |
| + ParseFloat(dict, "scaleZ", &element->scale.z); |
| + ParseFloat(dict, "rotationX", &element->rotation.x); |
| + ParseFloat(dict, "rotationY", &element->rotation.y); |
| + ParseFloat(dict, "rotationZ", &element->rotation.z); |
| + ParseFloat(dict, "rotationAngle", &element->rotation.angle); |
| + ParseFloat(dict, "translationX", &element->translation.x); |
| + ParseFloat(dict, "translationY", &element->translation.y); |
| + ParseFloat(dict, "translationZ", &element->translation.z); |
| + |
| + ParseInt(dict, "drawPhase", &element->draw_phase); |
| + |
| + ParseFloat(dict, "opacity", &element->opacity); |
|
tiborg
2017/02/23 15:21:18
Remove new lines or move the ParseFloat to the oth
cjgrant
2017/02/23 16:13:37
Done.
|
| + if (ParseInt(dict, "xAnchoring", &element->x_anchoring)) { |
| CHECK_GE(element->parent_id, 0); |
| } |
| - if (dict.GetInteger("yAnchoring", |
| - reinterpret_cast<int*>(&element->y_anchoring))) { |
| + if (ParseInt(dict, "yAnchoring", &element->y_anchoring)) { |
| CHECK_GE(element->parent_id, 0); |
| } |
| // Parse the element fill. |
| - if (dict.GetInteger("fillType", reinterpret_cast<int*>(&element->fill))) { |
| + if (ParseInt(dict, "fillType", &element->fill)) { |
| // If the previous content element has a new filling now make sure this is |
| // tracked correctly. |
| if (content_element_ == element && element->fill != Fill::CONTENT) { |
| @@ -479,7 +425,10 @@ void UiScene::ApplyDictToElement(const base::DictionaryValue& dict, |
| switch (element->fill) { |
| case Fill::SPRITE: |
| - CHECK(ParseRecti(dict, "copyRect", &element->copy_rect)); |
| + CHECK(ParseInt(dict, "copyRectX", &element->copy_rect.x)); |
| + CHECK(ParseInt(dict, "copyRectY", &element->copy_rect.y)); |
| + CHECK(ParseInt(dict, "copyRectWidth", &element->copy_rect.width)); |
| + CHECK(ParseInt(dict, "copyRectHeight", &element->copy_rect.height)); |
| break; |
| case Fill::OPAQUE_GRADIENT: |
| CHECK(ParseColorf(dict, "edgeColor", &element->edge_color)); |
| @@ -488,7 +437,7 @@ void UiScene::ApplyDictToElement(const base::DictionaryValue& dict, |
| case Fill::GRID_GRADIENT: |
| CHECK(ParseColorf(dict, "edgeColor", &element->edge_color)); |
| CHECK(ParseColorf(dict, "centerColor", &element->center_color)); |
| - CHECK(dict.GetInteger("gridlineCount", &element->gridline_count)); |
| + CHECK(ParseInt(dict, "gridlineCount", &element->gridline_count)); |
| CHECK_GE(element->gridline_count, 0); |
| break; |
| case Fill::CONTENT: |