OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/web_view/find_controller.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "components/web_view/find_controller_delegate.h" |
| 9 #include "components/web_view/frame.h" |
| 10 |
| 11 namespace web_view { |
| 12 |
| 13 FindController::FindController(FindControllerDelegate* delegate) |
| 14 : delegate_(delegate), current_find_request_(-1), weak_ptr_factory_(this) {} |
| 15 |
| 16 FindController::~FindController() {} |
| 17 |
| 18 void FindController::Find(int32_t request_id, const mojo::String& search_text) { |
| 19 // TODO(erg): While this deals with multiple frames, it does not deal with |
| 20 // going forward or backwards. To do that, we'll have to port all frame |
| 21 // traversal and focusing concepts from blink::WebFrame to mojo::Frame. |
| 22 |
| 23 // TODO(erg): This isn't great and causes flashes on character |
| 24 // entry. However, it's needed for now because the internals of TextFinder |
| 25 // still track the entire state of the blink frame tree, and if there are any |
| 26 // frames that have marked text, doing a find clears the results of all |
| 27 // frames _except_ for the first frame that it finds a result on. |
| 28 StopFinding(); |
| 29 |
| 30 // TODO(erg): This cheap method does not traverse in the order that blink |
| 31 // does. |
| 32 pending_find_frames_ = delegate_->GetAllFrames(); |
| 33 |
| 34 current_find_request_ = request_id; |
| 35 returned_find_data_.clear(); |
| 36 |
| 37 // Prime the continue loop. |
| 38 OnContinueFinding(request_id, search_text, false); |
| 39 } |
| 40 |
| 41 void FindController::StopFinding() { |
| 42 // Don't report any callbacks that we get after this. |
| 43 current_find_request_ = -1; |
| 44 |
| 45 for (Frame* f : delegate_->GetAllFrames()) |
| 46 f->StopFinding(true); |
| 47 } |
| 48 |
| 49 void FindController::OnFindInFrameCountUpdated(int32_t request_id, |
| 50 Frame* frame, |
| 51 int32_t count, |
| 52 bool final_update) { |
| 53 if (request_id != current_find_request_) |
| 54 return; |
| 55 |
| 56 auto it = returned_find_data_.find(frame); |
| 57 if (it == returned_find_data_.end()) { |
| 58 NOTREACHED(); |
| 59 return; |
| 60 } |
| 61 |
| 62 it->second.count = count; |
| 63 it->second.final_update = final_update; |
| 64 |
| 65 int merged_count = 0; |
| 66 bool merged_final_update = true; |
| 67 for (auto const& data : returned_find_data_) { |
| 68 merged_count += data.second.count; |
| 69 merged_final_update = merged_final_update && data.second.final_update; |
| 70 } |
| 71 |
| 72 // We can now take the individual FindInFrame messages and construct a |
| 73 // FindInPage message. |
| 74 delegate_->GetWebViewClient()->FindInPageMatchCountUpdated( |
| 75 request_id, merged_count, merged_final_update); |
| 76 } |
| 77 |
| 78 void FindController::OnFindInPageSelectionUpdated( |
| 79 int32_t request_id, |
| 80 Frame* frame, |
| 81 int32_t active_match_ordinal) { |
| 82 if (request_id != current_find_request_) |
| 83 return; |
| 84 |
| 85 // TODO(erg): This is the one that's really hard. To give an accurate count |
| 86 // here, we need to have all the results for frames that are before the Frame |
| 87 // that contains the selected match so we can add their sums together. |
| 88 // |
| 89 // Thankfully, we don't have to worry about this now. Since there aren't |
| 90 // back/forward controls yet, active_match_ordinal will always be 1. |
| 91 delegate_->GetWebViewClient()->FindInPageSelectionUpdated( |
| 92 request_id, active_match_ordinal); |
| 93 } |
| 94 |
| 95 void FindController::DidDestroyFrame(Frame* frame) { |
| 96 auto it = |
| 97 find(pending_find_frames_.begin(), pending_find_frames_.end(), frame); |
| 98 if (it != pending_find_frames_.end()) |
| 99 pending_find_frames_.erase(it); |
| 100 } |
| 101 |
| 102 void FindController::OnContinueFinding(int32_t request_id, |
| 103 const mojo::String& search_text, |
| 104 bool found) { |
| 105 if (!found && !pending_find_frames_.empty()) { |
| 106 // No match found, search on the next frame. |
| 107 Frame* next_frame = pending_find_frames_.front(); |
| 108 pending_find_frames_.pop_front(); |
| 109 next_frame->Find( |
| 110 request_id, search_text, |
| 111 base::Bind(&FindController::OnContinueFinding, |
| 112 weak_ptr_factory_.GetWeakPtr(), request_id, search_text)); |
| 113 |
| 114 // TODO(erg): This doesn't deal with wrapping around the document at the |
| 115 // end when there are multiple frames. |
| 116 return; |
| 117 } |
| 118 |
| 119 pending_find_frames_.clear(); |
| 120 |
| 121 // We either found a match or we got the final rejection. Either way, we |
| 122 // alert our caller. |
| 123 |
| 124 // If nothing is found, set result to "0 of 0", otherwise, set it to |
| 125 // "-1 of 1" to indicate that we found at least one item, but we don't know |
| 126 // yet what is active. |
| 127 int ordinal = found ? -1 : 0; // -1 here means, we might know more later. |
| 128 int match_count = found ? 1 : 0; // 1 here means possibly more coming. |
| 129 |
| 130 // If we find no matches then this will be our last status update. |
| 131 // Otherwise the scoping effort will send more results. |
| 132 bool final_status_update = !found; |
| 133 |
| 134 // Send priming messages. |
| 135 delegate_->GetWebViewClient()->FindInPageSelectionUpdated(request_id, |
| 136 ordinal); |
| 137 delegate_->GetWebViewClient()->FindInPageMatchCountUpdated( |
| 138 request_id, match_count, final_status_update); |
| 139 |
| 140 // TODO(erg): This doesn't iterate in the same order as the current code |
| 141 // because we don't have the correct iteration primitives. |
| 142 std::deque<Frame*> frames = delegate_->GetAllFrames(); |
| 143 for (Frame* f : frames) { |
| 144 f->StopHighlightingFindResults(); |
| 145 |
| 146 if (found) { |
| 147 MatchData& match_data = returned_find_data_[f]; |
| 148 match_data.count = 0; |
| 149 match_data.final_update = false; |
| 150 f->HighlightFindResults(request_id, search_text, true); |
| 151 } |
| 152 } |
| 153 } |
| 154 |
| 155 } // namespace web_view |
OLD | NEW |