Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: components/html_viewer/html_frame.cc

Issue 1371773003: mandoline: Add find in page. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: General patch cleanup. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 "components/html_viewer/html_frame.h" 5 #include "components/html_viewer/html_frame.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 #include "mojo/common/common_type_converters.h" 42 #include "mojo/common/common_type_converters.h"
43 #include "mojo/converters/geometry/geometry_type_converters.h" 43 #include "mojo/converters/geometry/geometry_type_converters.h"
44 #include "skia/ext/refptr.h" 44 #include "skia/ext/refptr.h"
45 #include "third_party/WebKit/public/platform/Platform.h" 45 #include "third_party/WebKit/public/platform/Platform.h"
46 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" 46 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
47 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" 47 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
48 #include "third_party/WebKit/public/platform/WebSize.h" 48 #include "third_party/WebKit/public/platform/WebSize.h"
49 #include "third_party/WebKit/public/web/WebConsoleMessage.h" 49 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
50 #include "third_party/WebKit/public/web/WebDocument.h" 50 #include "third_party/WebKit/public/web/WebDocument.h"
51 #include "third_party/WebKit/public/web/WebElement.h" 51 #include "third_party/WebKit/public/web/WebElement.h"
52 #include "third_party/WebKit/public/web/WebFindOptions.h"
52 #include "third_party/WebKit/public/web/WebInputEvent.h" 53 #include "third_party/WebKit/public/web/WebInputEvent.h"
53 #include "third_party/WebKit/public/web/WebKit.h" 54 #include "third_party/WebKit/public/web/WebKit.h"
54 #include "third_party/WebKit/public/web/WebLocalFrame.h" 55 #include "third_party/WebKit/public/web/WebLocalFrame.h"
55 #include "third_party/WebKit/public/web/WebNavigationPolicy.h" 56 #include "third_party/WebKit/public/web/WebNavigationPolicy.h"
56 #include "third_party/WebKit/public/web/WebRemoteFrame.h" 57 #include "third_party/WebKit/public/web/WebRemoteFrame.h"
57 #include "third_party/WebKit/public/web/WebRemoteFrameClient.h" 58 #include "third_party/WebKit/public/web/WebRemoteFrameClient.h"
58 #include "third_party/WebKit/public/web/WebScriptSource.h" 59 #include "third_party/WebKit/public/web/WebScriptSource.h"
59 #include "third_party/WebKit/public/web/WebView.h" 60 #include "third_party/WebKit/public/web/WebView.h"
60 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" 61 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
61 #include "third_party/skia/include/core/SkCanvas.h" 62 #include "third_party/skia/include/core/SkCanvas.h"
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 } 232 }
232 233
233 blink::WebView* HTMLFrame::web_view() { 234 blink::WebView* HTMLFrame::web_view() {
234 blink::WebWidget* web_widget = 235 blink::WebWidget* web_widget =
235 html_widget_ ? html_widget_->GetWidget() : nullptr; 236 html_widget_ ? html_widget_->GetWidget() : nullptr;
236 return web_widget && web_widget->isWebView() 237 return web_widget && web_widget->isWebView()
237 ? static_cast<blink::WebView*>(web_widget) 238 ? static_cast<blink::WebView*>(web_widget)
238 : nullptr; 239 : nullptr;
239 } 240 }
240 241
242 blink::WebView* HTMLFrame::web_view() const {
243 blink::WebWidget* web_widget =
244 html_widget_ ? html_widget_->GetWidget() : nullptr;
245 return web_widget && web_widget->isWebView()
246 ? static_cast<blink::WebView*>(web_widget)
247 : nullptr;
248 }
249
241 blink::WebWidget* HTMLFrame::GetWebWidget() { 250 blink::WebWidget* HTMLFrame::GetWebWidget() {
242 return html_widget_ ? html_widget_->GetWidget() : nullptr; 251 return html_widget_ ? html_widget_->GetWidget() : nullptr;
243 } 252 }
244 253
245 bool HTMLFrame::IsLocal() const { 254 bool HTMLFrame::IsLocal() const {
246 return web_frame_->isWebLocalFrame(); 255 return web_frame_->isWebLocalFrame();
247 } 256 }
248 257
249 bool HTMLFrame::HasLocalDescendant() const { 258 bool HTMLFrame::HasLocalDescendant() const {
250 if (IsLocal()) 259 if (IsLocal())
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 blink::WebTextDirection direction) { 468 blink::WebTextDirection direction) {
460 // TODO(beng): handle |direction|. 469 // TODO(beng): handle |direction|.
461 mojo::String formatted; 470 mojo::String formatted;
462 if (!title.isNull()) { 471 if (!title.isNull()) {
463 formatted = 472 formatted =
464 mojo::String::From(base::string16(title).substr(0, kMaxTitleChars)); 473 mojo::String::From(base::string16(title).substr(0, kMaxTitleChars));
465 } 474 }
466 server_->TitleChanged(formatted); 475 server_->TitleChanged(formatted);
467 } 476 }
468 477
478 void HTMLFrame::reportFindInPageMatchCount(int identifier,
479 int count,
480 bool finalUpdate) {
481 server_->ReportFindInPageMatchCount(identifier, count, finalUpdate);
482 }
483
484 void HTMLFrame::reportFindInPageSelection(int identifier,
485 int activeMatchOrdinal,
486 const blink::WebRect& selection) {
487 server_->ReportFindInPageSelection(identifier, activeMatchOrdinal);
488 }
489
469 void HTMLFrame::Bind( 490 void HTMLFrame::Bind(
470 web_view::mojom::FramePtr frame, 491 web_view::mojom::FramePtr frame,
471 mojo::InterfaceRequest<web_view::mojom::FrameClient> frame_client_request) { 492 mojo::InterfaceRequest<web_view::mojom::FrameClient> frame_client_request) {
472 DCHECK(IsLocal()); 493 DCHECK(IsLocal());
473 server_ = frame.Pass(); 494 server_ = frame.Pass();
474 server_.set_connection_error_handler( 495 server_.set_connection_error_handler(
475 base::Bind(&HTMLFrame::Close, base::Unretained(this))); 496 base::Bind(&HTMLFrame::Close, base::Unretained(this)));
476 frame_client_binding_.reset(new mojo::Binding<web_view::mojom::FrameClient>( 497 frame_client_binding_.reset(new mojo::Binding<web_view::mojom::FrameClient>(
477 this, frame_client_request.Pass())); 498 this, frame_client_request.Pass()));
478 } 499 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 web_layer_.reset(); 631 web_layer_.reset();
611 } 632 }
612 633
613 void HTMLFrame::SwapDelegate(HTMLFrameDelegate* delegate) { 634 void HTMLFrame::SwapDelegate(HTMLFrameDelegate* delegate) {
614 DCHECK(IsLocal()); 635 DCHECK(IsLocal());
615 HTMLFrameDelegate* old_delegate = delegate_; 636 HTMLFrameDelegate* old_delegate = delegate_;
616 delegate_ = delegate; 637 delegate_ = delegate;
617 delegate->OnSwap(this, old_delegate); 638 delegate->OnSwap(this, old_delegate);
618 } 639 }
619 640
641 blink::WebElement HTMLFrame::GetFocusedElement() const {
642 if (!web_view())
643 return blink::WebElement();
644 blink::WebFrame* focused_frame = web_view()->focusedFrame();
645 if (focused_frame) {
646 blink::WebDocument doc = focused_frame->document();
647 if (!doc.isNull())
648 return doc.focusedElement();
649 }
650
651 return blink::WebElement();
652 }
653
620 HTMLFrame* HTMLFrame::FindFrameWithWebFrame(blink::WebFrame* web_frame) { 654 HTMLFrame* HTMLFrame::FindFrameWithWebFrame(blink::WebFrame* web_frame) {
621 if (web_frame_ == web_frame) 655 if (web_frame_ == web_frame)
622 return this; 656 return this;
623 for (HTMLFrame* child_frame : children_) { 657 for (HTMLFrame* child_frame : children_) {
624 HTMLFrame* result = child_frame->FindFrameWithWebFrame(web_frame); 658 HTMLFrame* result = child_frame->FindFrameWithWebFrame(web_frame);
625 if (result) 659 if (result)
626 return result; 660 return result;
627 } 661 }
628 return nullptr; 662 return nullptr;
629 } 663 }
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
799 frame->web_frame_->toWebRemoteFrame()->didStopLoading(); 833 frame->web_frame_->toWebRemoteFrame()->didStopLoading();
800 } 834 }
801 } 835 }
802 836
803 void HTMLFrame::OnDispatchFrameLoadEvent(uint32_t frame_id) { 837 void HTMLFrame::OnDispatchFrameLoadEvent(uint32_t frame_id) {
804 HTMLFrame* frame = frame_tree_manager_->root_->FindFrame(frame_id); 838 HTMLFrame* frame = frame_tree_manager_->root_->FindFrame(frame_id);
805 if (frame && !frame->IsLocal()) 839 if (frame && !frame->IsLocal())
806 frame->web_frame_->toWebRemoteFrame()->DispatchLoadEventForFrameOwner(); 840 frame->web_frame_->toWebRemoteFrame()->DispatchLoadEventForFrameOwner();
807 } 841 }
808 842
843 void HTMLFrame::OnFind(int32 request_id, const mojo::String& search_text) {
Elliot Glaysher 2015/09/28 21:22:57 This and GetFocusedElement() were copied almost ve
844 blink::WebFrame* main_frame = web_view()->mainFrame();
845 blink::WebFrame* frame_after_main = main_frame->traverseNext(true);
846 blink::WebFrame* focused_frame = web_view()->focusedFrame();
847 blink::WebFrame* search_frame =
848 focused_frame; // start searching focused frame.
849
850 bool multi_frame = (frame_after_main != main_frame);
851
852 blink::WebFindOptions options;
853
854 // If we have multiple frames, we don't want to wrap the search within the
855 // frame, so we check here if we only have main_frame in the chain.
856 bool wrap_within_frame = !multi_frame;
857
858 blink::WebRect selection_rect;
859 bool result = false;
860
861 // If something is selected when we start searching it means we cannot just
862 // increment the current match ordinal; we need to re-generate it.
863 blink::WebRange current_selection = focused_frame->selectionRange();
864
865 do {
866 result = search_frame->find(request_id, search_text.To<blink::WebString>(),
867 options, wrap_within_frame, &selection_rect);
868
869 if (!result) {
870 // don't leave text selected as you move to the next frame.
871 search_frame->executeCommand(blink::WebString::fromUTF8("Unselect"),
872 GetFocusedElement());
873
874 // Find the next frame, but skip the invisible ones.
875 do {
876 // What is the next frame to search? (we might be going backwards). Note
877 // that we specify wrap=true so that search_frame never becomes NULL.
878 search_frame = options.forward ? search_frame->traverseNext(true)
879 : search_frame->traversePrevious(true);
880 } while (!search_frame->hasVisibleContent() &&
881 search_frame != focused_frame);
882
883 // Make sure selection doesn't affect the search operation in new frame.
884 search_frame->executeCommand(blink::WebString::fromUTF8("Unselect"),
885 GetFocusedElement());
886
887 // If we have multiple frames and we have wrapped back around to the
888 // focused frame, we need to search it once more allowing wrap within
889 // the frame, otherwise it will report 'no match' if the focused frame has
890 // reported matches, but no frames after the focused_frame contain a
891 // match for the search word(s).
892 if (multi_frame && search_frame == focused_frame) {
893 result =
894 search_frame->find(request_id, search_text.To<blink::WebString>(),
895 options, true, // Force wrapping.
896 &selection_rect);
897 }
898 }
899
900 web_view()->setFocusedFrame(search_frame);
901 } while (!result && search_frame != focused_frame);
902
903 if (options.findNext && current_selection.isNull()) {
904 // Force the main_frame to report the actual count.
905 main_frame->increaseMatchCount(0, request_id);
906 } else {
907 // If nothing is found, set result to "0 of 0", otherwise, set it to
908 // "-1 of 1" to indicate that we found at least one item, but we don't know
909 // yet what is active.
910 int ordinal = result ? -1 : 0; // -1 here means, we might know more later.
911 int match_count = result ? 1 : 0; // 1 here means possibly more coming.
912
913 // If we find no matches then this will be our last status update.
914 // Otherwise the scoping effort will send more results.
915 bool final_status_update = !result;
916
917 server_->ReportFindInPageSelection(request_id, ordinal);
918 server_->ReportFindInPageMatchCount(request_id, match_count,
919 final_status_update);
920
921 // Scoping effort begins, starting with the mainframe.
922 search_frame = main_frame;
923
924 main_frame->resetMatchCount();
925
926 do {
927 // Cancel all old scoping requests before starting a new one.
928 search_frame->cancelPendingScopingEffort();
929
930 // We don't start another scoping effort unless at least one match has
931 // been found.
932 if (result) {
933 // Start new scoping request. If the scoping function determines that it
934 // needs to scope, it will defer until later.
935 search_frame->scopeStringMatches(
936 request_id, search_text.To<blink::WebString>(), options,
937 true); // reset the tickmarks
938 }
939
940 // Iterate to the next frame. The frame will not necessarily scope, for
941 // example if it is not visible.
942 search_frame = search_frame->traverseNext(true);
943 } while (search_frame != main_frame);
944 }
945 }
946
947 void HTMLFrame::OnStopFinding() {
948 blink::WebView* view = web_view();
949 if (!view)
950 return;
951
952 // TODO(erg): content has a whole concept about different actions that can be
953 // done during a stop find. We aren't handling these at all.
954 bool clear_selection = true;
955
956 view->focusedFrame()->executeCommand(blink::WebString::fromUTF8("Unselect"),
957 GetFocusedElement());
958
959 blink::WebFrame* frame = view->mainFrame();
960 while (frame) {
961 frame->stopFinding(clear_selection);
962 frame = frame->traverseNext(false);
963 }
964 }
965
809 void HTMLFrame::frameDetached(blink::WebRemoteFrameClient::DetachType type) { 966 void HTMLFrame::frameDetached(blink::WebRemoteFrameClient::DetachType type) {
810 if (type == blink::WebRemoteFrameClient::DetachType::Swap) { 967 if (type == blink::WebRemoteFrameClient::DetachType::Swap) {
811 web_frame_->close(); 968 web_frame_->close();
812 return; 969 return;
813 } 970 }
814 971
815 DCHECK(type == blink::WebRemoteFrameClient::DetachType::Remove); 972 DCHECK(type == blink::WebRemoteFrameClient::DetachType::Remove);
816 FrameDetachedImpl(web_frame_); 973 FrameDetachedImpl(web_frame_);
817 } 974 }
818 975
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 1019
863 void HTMLFrame::reload(bool ignore_cache, bool is_client_redirect) { 1020 void HTMLFrame::reload(bool ignore_cache, bool is_client_redirect) {
864 NOTIMPLEMENTED(); 1021 NOTIMPLEMENTED();
865 } 1022 }
866 1023
867 void HTMLFrame::forwardInputEvent(const blink::WebInputEvent* event) { 1024 void HTMLFrame::forwardInputEvent(const blink::WebInputEvent* event) {
868 NOTIMPLEMENTED(); 1025 NOTIMPLEMENTED();
869 } 1026 }
870 1027
871 } // namespace mojo 1028 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698