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