| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/browser/find_request_manager.h" | 5 #include "content/browser/find_request_manager.h" |
| 6 | 6 |
| 7 #include "content/browser/frame_host/render_frame_host_impl.h" | 7 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" | 8 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/common/frame_messages.h" | 9 #include "content/common/frame_messages.h" |
| 10 #include "content/common/input_messages.h" | 10 #include "content/common/input_messages.h" |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 DCHECK(CheckFrame(rfh)); | 144 DCHECK(CheckFrame(rfh)); |
| 145 | 145 |
| 146 // Update the stored find results. | 146 // Update the stored find results. |
| 147 | 147 |
| 148 DCHECK_GE(number_of_matches, -1); | 148 DCHECK_GE(number_of_matches, -1); |
| 149 DCHECK_GE(active_match_ordinal, -1); | 149 DCHECK_GE(active_match_ordinal, -1); |
| 150 | 150 |
| 151 // Check for an update to the number of matches. | 151 // Check for an update to the number of matches. |
| 152 if (number_of_matches != -1) { | 152 if (number_of_matches != -1) { |
| 153 DCHECK_GE(number_of_matches, 0); | 153 DCHECK_GE(number_of_matches, 0); |
| 154 // Increment the global number of matches by the number of additional |
| 155 // matches found for this frame. |
| 154 auto matches_per_frame_it = matches_per_frame_.find(rfh); | 156 auto matches_per_frame_it = matches_per_frame_.find(rfh); |
| 155 if (int matches_delta = number_of_matches - matches_per_frame_it->second) { | 157 number_of_matches_ += number_of_matches - matches_per_frame_it->second; |
| 156 // Increment the global number of matches by the number of additional | 158 matches_per_frame_it->second = number_of_matches; |
| 157 // matches found for this frame. | |
| 158 number_of_matches_ += matches_delta; | |
| 159 matches_per_frame_it->second = number_of_matches; | |
| 160 | |
| 161 // The active match ordinal may need updating since the number of matches | |
| 162 // before the active match may have changed. | |
| 163 if (rfh != active_frame_) | |
| 164 UpdateActiveMatchOrdinal(); | |
| 165 } | |
| 166 } | 159 } |
| 167 | 160 |
| 168 // Check for an update to the selection rect. | 161 // Check for an update to the selection rect. |
| 169 if (!selection_rect.IsEmpty()) | 162 if (!selection_rect.IsEmpty()) |
| 170 selection_rect_ = selection_rect; | 163 selection_rect_ = selection_rect; |
| 171 | 164 |
| 172 // Check for an update to the active match ordinal. | 165 // Check for an update to the active match ordinal. |
| 173 if (active_match_ordinal > 0) { | 166 if (active_match_ordinal > 0) { |
| 174 if (rfh == active_frame_) { | 167 if (rfh == active_frame_) { |
| 175 active_match_ordinal_ += | 168 active_match_ordinal_ += |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 // New version of rects has been received, so update the data. | 339 // New version of rects has been received, so update the data. |
| 347 match_rects_.frame_rects[rfh] = FrameRects(rects, version); | 340 match_rects_.frame_rects[rfh] = FrameRects(rects, version); |
| 348 ++match_rects_.known_version; | 341 ++match_rects_.known_version; |
| 349 } | 342 } |
| 350 if (!active_rect.IsEmpty()) | 343 if (!active_rect.IsEmpty()) |
| 351 match_rects_.active_rect = active_rect; | 344 match_rects_.active_rect = active_rect; |
| 352 RemoveFindMatchRectsPendingReply(rfh); | 345 RemoveFindMatchRectsPendingReply(rfh); |
| 353 } | 346 } |
| 354 #endif | 347 #endif |
| 355 | 348 |
| 356 void FindRequestManager::DidFinishLoad(RenderFrameHost* rfh, | |
| 357 const GURL& validated_url) { | |
| 358 if (current_session_id_ == kInvalidId) | |
| 359 return; | |
| 360 | |
| 361 RemoveFrame(rfh); | |
| 362 AddFrame(rfh, true /* force */); | |
| 363 } | |
| 364 | |
| 365 void FindRequestManager::RenderFrameDeleted(RenderFrameHost* rfh) { | 349 void FindRequestManager::RenderFrameDeleted(RenderFrameHost* rfh) { |
| 366 RemoveFrame(rfh); | 350 RemoveFrame(rfh); |
| 367 } | 351 } |
| 368 | 352 |
| 369 void FindRequestManager::RenderFrameHostChanged(RenderFrameHost* old_host, | 353 void FindRequestManager::RenderFrameHostChanged(RenderFrameHost* old_host, |
| 370 RenderFrameHost* new_host) { | 354 RenderFrameHost* new_host) { |
| 371 RemoveFrame(old_host); | 355 RemoveFrame(old_host); |
| 372 } | 356 } |
| 373 | 357 |
| 374 void FindRequestManager::FrameDeleted(RenderFrameHost* rfh) { | 358 void FindRequestManager::FrameDeleted(RenderFrameHost* rfh) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 | 398 |
| 415 SendFindIPC(request, target_rfh); | 399 SendFindIPC(request, target_rfh); |
| 416 current_request_ = request; | 400 current_request_ = request; |
| 417 pending_active_match_ordinal_ = true; | 401 pending_active_match_ordinal_ = true; |
| 418 return; | 402 return; |
| 419 } | 403 } |
| 420 | 404 |
| 421 // This is an initial find operation. | 405 // This is an initial find operation. |
| 422 Reset(request); | 406 Reset(request); |
| 423 for (FrameTreeNode* node : contents_->GetFrameTree()->Nodes()) | 407 for (FrameTreeNode* node : contents_->GetFrameTree()->Nodes()) |
| 424 AddFrame(node->current_frame_host(), false /* force */); | 408 AddFrame(node->current_frame_host()); |
| 425 } | 409 } |
| 426 | 410 |
| 427 void FindRequestManager::AdvanceQueue(int request_id) { | 411 void FindRequestManager::AdvanceQueue(int request_id) { |
| 428 if (find_request_queue_.empty() || | 412 if (find_request_queue_.empty() || |
| 429 request_id != find_request_queue_.front().id) { | 413 request_id != find_request_queue_.front().id) { |
| 430 return; | 414 return; |
| 431 } | 415 } |
| 432 | 416 |
| 433 find_request_queue_.pop(); | 417 find_request_queue_.pop(); |
| 434 if (!find_request_queue_.empty()) | 418 if (!find_request_queue_.empty()) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 // |matches_only|. | 477 // |matches_only|. |
| 494 return node->current_frame_host(); | 478 return node->current_frame_host(); |
| 495 } | 479 } |
| 496 if (wrap && node->current_frame_host() == from_rfh) | 480 if (wrap && node->current_frame_host() == from_rfh) |
| 497 return nullptr; | 481 return nullptr; |
| 498 } | 482 } |
| 499 | 483 |
| 500 return nullptr; | 484 return nullptr; |
| 501 } | 485 } |
| 502 | 486 |
| 503 void FindRequestManager::AddFrame(RenderFrameHost* rfh, bool force) { | 487 void FindRequestManager::AddFrame(RenderFrameHost* rfh) { |
| 504 if (!rfh || !rfh->IsRenderFrameLive()) | 488 if (!rfh || !rfh->IsRenderFrameLive()) |
| 505 return; | 489 return; |
| 506 | 490 |
| 507 // A frame that is already being searched should not normally be added again. | 491 // A frame that is already being searched should not be added again. |
| 508 DCHECK(force || !CheckFrame(rfh)); | 492 DCHECK(!CheckFrame(rfh)); |
| 509 | 493 |
| 510 matches_per_frame_[rfh] = 0; | 494 matches_per_frame_[rfh] = 0; |
| 511 | 495 |
| 512 FindRequest request = current_request_; | 496 FindRequest request = current_request_; |
| 513 request.id = current_session_id_; | 497 request.id = current_session_id_; |
| 514 request.options.findNext = false; | 498 request.options.findNext = false; |
| 515 request.options.force = force; | |
| 516 SendFindIPC(request, rfh); | 499 SendFindIPC(request, rfh); |
| 517 } | 500 } |
| 518 | 501 |
| 519 bool FindRequestManager::CheckFrame(RenderFrameHost* rfh) const { | 502 bool FindRequestManager::CheckFrame(RenderFrameHost* rfh) const { |
| 520 return rfh && matches_per_frame_.count(rfh); | 503 return rfh && matches_per_frame_.count(rfh); |
| 521 } | 504 } |
| 522 | 505 |
| 523 void FindRequestManager::UpdateActiveMatchOrdinal() { | 506 void FindRequestManager::UpdateActiveMatchOrdinal() { |
| 524 active_match_ordinal_ = 0; | 507 active_match_ordinal_ = 0; |
| 525 | 508 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 538 false /* wrap */)) != nullptr) { | 521 false /* wrap */)) != nullptr) { |
| 539 active_match_ordinal_ += matches_per_frame_[frame]; | 522 active_match_ordinal_ += matches_per_frame_[frame]; |
| 540 } | 523 } |
| 541 active_match_ordinal_ += relative_active_match_ordinal_; | 524 active_match_ordinal_ += relative_active_match_ordinal_; |
| 542 } | 525 } |
| 543 | 526 |
| 544 void FindRequestManager::FinalUpdateReceived(int request_id, | 527 void FindRequestManager::FinalUpdateReceived(int request_id, |
| 545 RenderFrameHost* rfh) { | 528 RenderFrameHost* rfh) { |
| 546 if (!number_of_matches_ || | 529 if (!number_of_matches_ || |
| 547 (active_match_ordinal_ && !pending_active_match_ordinal_) || | 530 (active_match_ordinal_ && !pending_active_match_ordinal_) || |
| 548 pending_find_next_reply_) { | 531 request_id != current_request_.id) { |
| 549 // All the find results for |request_id| are in and ready to report. Note | 532 // All the find results for |request_id| are in and ready to report. Note |
| 550 // that |final_update| will be set to false if there are still pending | 533 // that |final_update| will be set to false if there are still pending |
| 551 // replies expected from the initial find request. | 534 // replies expected from the initial find request. |
| 552 NotifyFindReply(request_id, | 535 NotifyFindReply(request_id, |
| 553 pending_initial_replies_.empty() /* final_update */); | 536 pending_initial_replies_.empty() /* final_update */); |
| 554 AdvanceQueue(request_id); | 537 AdvanceQueue(request_id); |
| 555 return; | 538 return; |
| 556 } | 539 } |
| 557 | 540 |
| 558 // There are matches, but no active match was returned, so another find next | 541 // There are matches, but no active match was returned, so another find next |
| 559 // request must be sent. | 542 // request must be sent. |
| 560 | 543 |
| 561 RenderFrameHost* target_rfh; | 544 RenderFrameHost* target_rfh; |
| 562 if (request_id == current_request_.id && current_request_.options.findNext) { | 545 if (current_request_.options.findNext) { |
| 563 // If this was a find next operation, then the active match will be in the | 546 // If this was a find next operation, then the active match will be in the |
| 564 // next frame with matches after this one. | 547 // next frame with matches after this one. |
| 565 target_rfh = Traverse(rfh, | 548 target_rfh = Traverse(rfh, |
| 566 current_request_.options.forward, | 549 current_request_.options.forward, |
| 567 true /* matches_only */, | 550 true /* matches_only */, |
| 568 true /* wrap */); | 551 true /* wrap */); |
| 569 } else if ((target_rfh = contents_->GetFocusedFrame()) != nullptr) { | 552 } else if ((target_rfh = contents_->GetFocusedFrame()) != nullptr) { |
| 570 // Otherwise, if there is a focused frame, then the active match will be in | 553 // Otherwise, if there is a focused frame, then the active match will be in |
| 571 // the next frame with matches after that one. | 554 // the next frame with matches after that one. |
| 572 target_rfh = Traverse(target_rfh, | 555 target_rfh = Traverse(target_rfh, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 aggregate_rects.end(), frame_rects.begin(), frame_rects.end()); | 619 aggregate_rects.end(), frame_rects.begin(), frame_rects.end()); |
| 637 } | 620 } |
| 638 } | 621 } |
| 639 contents_->NotifyFindMatchRectsReply( | 622 contents_->NotifyFindMatchRectsReply( |
| 640 match_rects_.known_version, aggregate_rects, match_rects_.active_rect); | 623 match_rects_.known_version, aggregate_rects, match_rects_.active_rect); |
| 641 } | 624 } |
| 642 } | 625 } |
| 643 #endif | 626 #endif |
| 644 | 627 |
| 645 } // namespace content | 628 } // namespace content |
| OLD | NEW |