OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "mojo/ui/associates/view_inspector_client.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 |
| 12 namespace mojo { |
| 13 namespace ui { |
| 14 |
| 15 ViewInspectorClient::ViewInspectorClient( |
| 16 mojo::ui::ViewInspectorPtr view_inspector) |
| 17 : view_inspector_(view_inspector.Pass()) { |
| 18 DCHECK(view_inspector_); |
| 19 } |
| 20 |
| 21 ViewInspectorClient::~ViewInspectorClient() {} |
| 22 |
| 23 void ViewInspectorClient::ResolveHits( |
| 24 mojo::gfx::composition::HitTestResultPtr hit_test_result, |
| 25 const ResolvedHitsCallback& callback) { |
| 26 DCHECK(hit_test_result); |
| 27 |
| 28 scoped_ptr<ResolvedHits> resolved_hits( |
| 29 new ResolvedHits(hit_test_result.Pass())); |
| 30 |
| 31 if (resolved_hits->result()->root) { |
| 32 mojo::Array<mojo::gfx::composition::SceneTokenPtr> missing_scene_tokens; |
| 33 ResolveSceneHit(resolved_hits->result()->root.get(), resolved_hits.get(), |
| 34 &missing_scene_tokens); |
| 35 if (missing_scene_tokens.size()) { |
| 36 // TODO(jeffbrown): Ideally we would set the capacity of the array |
| 37 // here since we know it upfront but mojo::Array doesn't support this. |
| 38 mojo::Array<uint32_t> missing_scene_token_values; |
| 39 for (const auto& token : missing_scene_tokens.storage()) |
| 40 missing_scene_token_values.push_back(token->value); |
| 41 view_inspector_->ResolveScenes( |
| 42 missing_scene_tokens.Pass(), |
| 43 base::Bind(&ViewInspectorClient::OnScenesResolved, |
| 44 base::Unretained(this), base::Passed(resolved_hits.Pass()), |
| 45 base::Passed(missing_scene_token_values.Pass()), |
| 46 callback)); |
| 47 return; |
| 48 } |
| 49 } |
| 50 |
| 51 callback.Run(resolved_hits.Pass()); |
| 52 } |
| 53 |
| 54 void ViewInspectorClient::ResolveSceneHit( |
| 55 const mojo::gfx::composition::SceneHit* scene_hit, |
| 56 ResolvedHits* resolved_hits, |
| 57 mojo::Array<mojo::gfx::composition::SceneTokenPtr>* missing_scene_tokens) { |
| 58 DCHECK(scene_hit); |
| 59 DCHECK(scene_hit->scene_token); |
| 60 DCHECK(resolved_hits); |
| 61 DCHECK(missing_scene_tokens); |
| 62 |
| 63 const uint32_t scene_token_value = scene_hit->scene_token->value; |
| 64 if (resolved_hits->map().find(scene_token_value) == |
| 65 resolved_hits->map().end()) { |
| 66 auto it = resolved_scene_cache_.find(scene_hit->scene_token->value); |
| 67 if (it != resolved_scene_cache_.end()) { |
| 68 if (it->second) |
| 69 resolved_hits->AddMapping(scene_token_value, it->second->Clone()); |
| 70 } else { |
| 71 if (std::none_of( |
| 72 missing_scene_tokens->storage().begin(), |
| 73 missing_scene_tokens->storage().end(), |
| 74 [scene_token_value]( |
| 75 const mojo::gfx::composition::SceneTokenPtr& needle) { |
| 76 return needle->value == scene_token_value; |
| 77 })) |
| 78 missing_scene_tokens->push_back(scene_hit->scene_token.Clone()); |
| 79 } |
| 80 } |
| 81 |
| 82 for (const auto& hit : scene_hit->hits.storage()) { |
| 83 if (hit->is_scene()) { |
| 84 ResolveSceneHit(hit->get_scene().get(), resolved_hits, |
| 85 missing_scene_tokens); |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 90 void ViewInspectorClient::OnScenesResolved( |
| 91 scoped_ptr<ResolvedHits> resolved_hits, |
| 92 mojo::Array<uint32_t> missing_scene_token_values, |
| 93 const ResolvedHitsCallback& callback, |
| 94 mojo::Array<mojo::ui::ViewTokenPtr> view_tokens) { |
| 95 DCHECK(resolved_hits); |
| 96 DCHECK(missing_scene_token_values); |
| 97 DCHECK(view_tokens); |
| 98 DCHECK(missing_scene_token_values.size() == view_tokens.size()); |
| 99 |
| 100 for (size_t i = 0; i < view_tokens.size(); i++) { |
| 101 const uint32_t scene_token_value = missing_scene_token_values[i]; |
| 102 resolved_scene_cache_.emplace(scene_token_value, view_tokens[i].Clone()); |
| 103 if (view_tokens[i]) |
| 104 resolved_hits->AddMapping(scene_token_value, view_tokens[i].Pass()); |
| 105 } |
| 106 |
| 107 callback.Run(resolved_hits.Pass()); |
| 108 } |
| 109 |
| 110 } // namespace ui |
| 111 } // namespace mojo |
OLD | NEW |