Commit 5cf58c68 authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

Remove unused find in page implementation

The Find in Page feature implementation has moved to
ios/web/public/find_in_page. This code is unused and can be removed.

Bug: 487804 , 949651, 996324
Change-Id: I55bfe528d428085f4f59de80d5bfb56130cabfa4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2480887
Auto-Submit: Mike Dougherty <michaeldo@chromium.org>
Commit-Queue: Eugene But <eugenebut@chromium.org>
Reviewed-by: default avatarChris Lu <thegreenfrog@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819027}
parent 8736e9de
......@@ -14,36 +14,22 @@ source_set("find_in_page") {
"find_in_page_response_delegate.h",
"find_tab_helper.h",
"find_tab_helper.mm",
"js_findinpage_manager.h",
"js_findinpage_manager.mm",
]
deps = [
":injected_js",
"//base",
"//components/ukm/ios:ukm_url_recorder",
"//ios/chrome/browser/web",
"//ios/web/public",
"//ios/web/public/deprecated",
"//ios/web/public/find_in_page",
"//services/metrics/public/cpp:ukm_builders",
]
frameworks = [ "CoreGraphics.framework" ]
}
# TODO(crbug.com/487804): use js_compile_checked instead once the errors have
# been fixed.
js_compile_unchecked("injected_js") {
sources = [ "resources/find_in_page.js" ]
}
source_set("unit_tests") {
testonly = true
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"find_in_page_controller_unittest.mm",
"find_in_page_js_unittest.mm",
"js_findinpage_manager_unittest.mm",
]
sources = [ "find_in_page_controller_unittest.mm" ]
deps = [
":find_in_page",
"//base",
......@@ -53,9 +39,6 @@ source_set("unit_tests") {
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/web:test_support",
"//ios/chrome/browser/web:web_internal",
"//ios/web/public/deprecated",
"//ios/web/public/test",
"//ios/web/public/test/fakes",
"//testing/gtest",
]
}
......@@ -15,9 +15,7 @@
#include "components/ukm/ios/ukm_url_recorder.h"
#import "ios/chrome/browser/find_in_page/find_in_page_model.h"
#import "ios/chrome/browser/find_in_page/find_in_page_response_delegate.h"
#import "ios/chrome/browser/find_in_page/js_findinpage_manager.h"
#import "ios/chrome/browser/web/dom_altering_lock.h"
#import "ios/web/public/deprecated/crw_js_injection_receiver.h"
#import "ios/web/public/find_in_page/find_in_page_manager.h"
#import "ios/web/public/find_in_page/find_in_page_manager_delegate_bridge.h"
#import "ios/web/public/ui/crw_web_view_proxy.h"
......
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_FIND_IN_PAGE_JS_FINDINPAGE_MANAGER_H_
#define IOS_CHROME_BROWSER_FIND_IN_PAGE_JS_FINDINPAGE_MANAGER_H_
#include <CoreGraphics/CGBase.h>
#include <CoreGraphics/CGGeometry.h>
#include "base/ios/block_types.h"
#import "ios/web/public/deprecated/crw_js_injection_manager.h"
// Data from find in page.
typedef struct FindInPageEntry {
CGPoint point; // Scroll offset required to center the highlighted item.
NSInteger index; // Currently higlighted search term.
} FindInPageEntry;
// Constant for "not found".
extern FindInPageEntry FindInPageEntryZero;
@class CRWJSInjectionReceiver;
@class FindInPageModel;
// Manager for the injection of the Find In Page JavaScript.
@interface JsFindinpageManager : CRWJSInjectionManager
// Find In Page model.
@property(nonatomic, readwrite, strong) FindInPageModel* findInPageModel;
// Sets the width and height of the window.
- (void)setWidth:(CGFloat)width height:(CGFloat)height;
// Runs injected JavaScript to find |query| string. Calls |completionHandler|
// with YES if the find operation completed, it is called with NO otherwise.
// If the find operation was successfiul the first match to scroll to is
// also called with. If the |completionHandler| is called with NO, another
// call to |pumpWithCompletionHandler:| is required. |completionHandler| cannot
// be nil.
- (void)findString:(NSString*)query
completionHandler:(void (^)(BOOL, CGPoint))completionHandler;
// Searches for more matches. Calls |completionHandler| with a success BOOL and
// scroll position if pumping was successful. If the pumping was unsuccessful
// another pumping call maybe required. |completionHandler| cannot be nil.
- (void)pumpWithCompletionHandler:(void (^)(BOOL, CGPoint))completionHandler;
// Moves to the next matched location and executes the completion handler with
// the new scroll position passed in. The |completionHandler| can be nil.
- (void)nextMatchWithCompletionHandler:(void (^)(CGPoint))completionHandler;
// Moves to the previous matched location and executes the completion handle
// with the new scroll position passed in. The |completionHandler| can be nil.
- (void)previousMatchWithCompletionHandler:(void (^)(CGPoint))completionHandler;
// Stops find in page and calls |completionHandler| once find in page is
// stopped. |completionHandler| cannot be nil.
- (void)disableWithCompletionHandler:(ProceduralBlock)completionHandler;
@end
#endif // IOS_CHROME_BROWSER_FIND_IN_PAGE_JS_FINDINPAGE_MANAGER_H_
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/find_in_page/js_findinpage_manager.h"
#include <memory>
#include <string>
#include "base/check.h"
#include "base/json/json_reader.h"
#include "base/json/string_escape.h"
#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
#import "ios/chrome/browser/find_in_page/find_in_page_model.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Initializes Find In Page JavaScript with the width and height of the window.
NSString* const kFindInPageInit = @"window.__gCrWeb.findInPage && "
"window.__gCrWeb.findInPage.init(%.f, %.f);";
// This will only do verbatim matches.
// The timeout of 100ms is hardcoded into this string so we don't have
// to spend any time at runtime to format this constant into another constant.
NSString* const kFindInPageVerbatim =
@"window.__gCrWeb.findInPage && "
"window.__gCrWeb.findInPage.highlightWord(%@, 100.0);";
// The timeout of 100ms is hardcoded into this string so we don't have
// to spend any time at runtime to format this constant into another constant.
NSString* const kFindInPagePump =
@"window.__gCrWeb.findInPage && "
"window.__gCrWeb.findInPage.pumpSearch(100.0);";
NSString* const kFindInPagePrev = @"window.__gCrWeb.findInPage && "
"window.__gCrWeb.findInPage.goPrev();";
NSString* const kFindInPageNext = @"window.__gCrWeb.findInPage && "
"window.__gCrWeb.findInPage.goNext();";
NSString* const kFindInPageDisable = @"window.__gCrWeb.findInPage && "
"window.__gCrWeb.findInPage.disable();";
NSString* const kFindInPagePending = @"[false]";
const FindInPageEntry kFindInPageEntryZero = {{0.0, 0.0}, 0};
} // namespace
@interface JsFindinpageManager ()
// Update find in page model with results, return true if fip completes or
// false if still pending and requires pumping. If |point| is not nil, it will
// contain the scroll position upon return.
- (BOOL)processFindInPageResult:(id)result scrollPosition:(CGPoint*)point;
// Updates find in page model with results. Calls |completionHandler| with the
// the result of the processing and the new scroll position if successful. If
// |completionHandler| is called with NO, further pumping is required.
// |completionHandler| cannot be nil.
- (void)processFindInPagePumpResult:(NSString*)result
completionHandler:(void (^)(BOOL, CGPoint))completionHandler;
// Helper functions to extract FindInPageEntry from JSON.
- (FindInPageEntry)findInPageEntryForJson:(NSString*)jsonStr;
- (FindInPageEntry)entryForListValue:(const base::Value&)position;
// Executes |script| which is a piece of JavaScript to move to the next or
// previous element in the page and executes |completionHandler| after moving
// with the new scroll position passed in.
- (void)moveHighlightByEvaluatingJavaScript:(NSString*)script
completionHandler:
(void (^)(CGPoint))completionHandler;
// Updates the current match index and its found position in the model.
- (void)updateIndex:(NSInteger)index atPoint:(CGPoint)point;
@end
@implementation JsFindinpageManager
@synthesize findInPageModel = _findInPageModel;
- (void)setWidth:(CGFloat)width height:(CGFloat)height {
NSString* javaScript =
[NSString stringWithFormat:kFindInPageInit, width, height];
[self executeJavaScript:javaScript completionHandler:nil];
}
- (void)findString:(NSString*)query
completionHandler:(void (^)(BOOL, CGPoint))completionHandler {
DCHECK(completionHandler);
// Save the query in the model before searching.
[self.findInPageModel updateQuery:query matches:0];
// Escape |query| before passing to js.
std::string escapedJSON;
base::EscapeJSONString(base::SysNSStringToUTF16(query), true, &escapedJSON);
NSString* JSONQuery =
[NSString stringWithFormat:kFindInPageVerbatim,
base::SysUTF8ToNSString(escapedJSON.c_str())];
__weak JsFindinpageManager* weakSelf = self;
[self executeJavaScript:JSONQuery
completionHandler:^(id result, NSError* error) {
// Conservative early return in case of error.
if (error)
return;
[weakSelf processFindInPagePumpResult:result
completionHandler:completionHandler];
}];
}
- (void)pumpWithCompletionHandler:(void (^)(BOOL, CGPoint))completionHandler {
DCHECK(completionHandler);
__weak JsFindinpageManager* weakSelf = self;
[self executeJavaScript:kFindInPagePump
completionHandler:^(id result, NSError* error) {
// Conservative early return in case of error.
if (error)
return;
[weakSelf processFindInPagePumpResult:result
completionHandler:completionHandler];
}];
}
- (void)nextMatchWithCompletionHandler:(void (^)(CGPoint))completionHandler {
[self moveHighlightByEvaluatingJavaScript:kFindInPageNext
completionHandler:completionHandler];
}
- (void)previousMatchWithCompletionHandler:
(void (^)(CGPoint))completionHandler {
[self moveHighlightByEvaluatingJavaScript:kFindInPagePrev
completionHandler:completionHandler];
}
- (void)moveHighlightByEvaluatingJavaScript:(NSString*)script
completionHandler:
(void (^)(CGPoint))completionHandler {
__weak JsFindinpageManager* weakSelf = self;
[self executeJavaScript:script
completionHandler:^(id result, NSError* error) {
JsFindinpageManager* strongSelf = weakSelf;
if (!strongSelf)
return;
// Conservative early return in case of error.
if (error)
return;
FindInPageEntry entry = kFindInPageEntryZero;
if (![result isEqual:kFindInPagePending]) {
NSString* stringResult =
base::mac::ObjCCastStrict<NSString>(result);
entry = [strongSelf findInPageEntryForJson:stringResult];
}
CGPoint newPoint = entry.point;
[strongSelf updateIndex:entry.index atPoint:newPoint];
if (completionHandler)
completionHandler(newPoint);
}];
}
- (void)disableWithCompletionHandler:(ProceduralBlock)completionHandler {
DCHECK(completionHandler);
[self executeJavaScript:kFindInPageDisable completionHandler:^(id, NSError*) {
completionHandler();
}];
}
#pragma mark -
#pragma mark FindInPageEntry
- (BOOL)processFindInPageResult:(id)result scrollPosition:(CGPoint*)point {
NSString* result_str = base::mac::ObjCCastStrict<NSString>(result);
if (!result_str)
return NO;
// Parse JSONs.
std::string json = base::SysNSStringToUTF8(result_str);
base::Optional<base::Value> root = base::JSONReader::Read(json);
if (!root.has_value())
return YES;
if (!root.value().is_list())
return YES;
base::Value::ConstListView listValues = root.value().GetList();
if (listValues.size() == 2) {
if (listValues[0].is_int()) {
int numHighlighted = listValues[0].GetInt();
if (numHighlighted > 0) {
if (listValues[1].is_list()) {
[self.findInPageModel updateQuery:nil matches:numHighlighted];
// Scroll to first match.
FindInPageEntry entry = [self entryForListValue:listValues[1]];
[self.findInPageModel updateIndex:entry.index atPoint:entry.point];
if (point)
*point = entry.point;
}
}
}
}
return YES;
}
- (void)processFindInPagePumpResult:(id)result
completionHandler:(void (^)(BOOL, CGPoint))completionHandler {
CGPoint point = CGPointZero;
if ([result isEqual:kFindInPagePending]) {
completionHandler(NO, point);
return;
}
BOOL processFIPResult =
[self processFindInPageResult:result scrollPosition:&point];
completionHandler(processFIPResult, point);
}
- (void)updateIndex:(NSInteger)index atPoint:(CGPoint)point {
[self.findInPageModel updateIndex:index atPoint:point];
}
- (FindInPageEntry)findInPageEntryForJson:(NSString*)jsonStr {
std::string json = base::SysNSStringToUTF8(jsonStr);
base::Optional<base::Value> root = base::JSONReader::Read(json);
if (!root.has_value())
return kFindInPageEntryZero;
if (!root.value().is_list())
return kFindInPageEntryZero;
return [self entryForListValue:root.value()];
}
- (FindInPageEntry)entryForListValue:(const base::Value&)position {
DCHECK(position.is_list());
// Position should always be of length 3, from [index,x,y].
base::Value::ConstListView positionList = position.GetList();
if (positionList.size() != 3)
return kFindInPageEntryZero;
// The array position comes from the JSON string [index, x, y], which
// represents the index of the currently found string, and the x and y
// position necessary to center that string. Pull out that data into a
// FindInPageEntry struct.
FindInPageEntry entry;
entry.index = positionList[0].is_int() ? positionList[0].GetInt() : 0;
entry.point.x = positionList[1].is_double() ? positionList[1].GetDouble() : 0;
entry.point.y = positionList[2].is_double() ? positionList[2].GetDouble() : 0;
return entry;
}
#pragma mark -
#pragma mark ProtectedMethods
- (NSString*)scriptPath {
return @"find_in_page";
}
@end
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/find_in_page/js_findinpage_manager.h"
#import <Foundation/Foundation.h>
#import "base/test/ios/wait_util.h"
#import "ios/chrome/browser/web/chrome_web_test.h"
#import "ios/web/public/deprecated/crw_js_injection_receiver.h"
#import "ios/web/public/web_state.h"
#import "testing/gtest_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
// Test fixture to test Find In Page JS.
class JsFindinpageManagerTest : public ChromeWebTest {
protected:
// Loads the given HTML and initializes the findInPage JS scripts.
void LoadHtml(NSString* html) {
ChromeWebTest::LoadHtml(html);
manager_ =
static_cast<JsFindinpageManager*>([web_state()->GetJSInjectionReceiver()
instanceOfClass:[JsFindinpageManager class]]);
[manager_ inject];
}
JsFindinpageManager* manager_;
};
// Tests that findString script reports a match when appropriate.
TEST_F(JsFindinpageManagerTest, FindInPageSucceeds) {
LoadHtml(@"<html><body><p>Target phrase</p></body></html>");
__block BOOL completion_handler_block_was_called = NO;
id completion_handler_block = ^(BOOL success, CGPoint scrollPosition) {
ASSERT_TRUE(success);
// 'scrollPosition' is updated if the string is found.
EXPECT_NE(FLT_MAX, scrollPosition.x);
completion_handler_block_was_called = YES;
};
[manager_ findString:@"Target phrase"
completionHandler:completion_handler_block];
base::test::ios::WaitUntilCondition(^bool() {
return completion_handler_block_was_called;
});
}
// Tests that findString script does not report a match when appropriate.
TEST_F(JsFindinpageManagerTest, FindInPageFails) {
LoadHtml(@"<html><body><p>Target phrase</p></body></html>");
__block BOOL completion_handler_block_was_called = NO;
id completion_handler_block = ^(BOOL success, CGPoint scrollPosition) {
// findString should return YES even if the target phrase is not found.
ASSERT_TRUE(success);
// 'point' is *not* updated if the string is not found.
EXPECT_TRUE(CGPointEqualToPoint(CGPointZero, scrollPosition));
completion_handler_block_was_called = YES;
};
[manager_ findString:@"Non-included phrase"
completionHandler:completion_handler_block];
base::test::ios::WaitUntilCondition(^bool() {
return completion_handler_block_was_called;
});
}
// Attepting to break out of the script and inject new script fails.
TEST_F(JsFindinpageManagerTest, InjectionTest) {
LoadHtml(@"<html><body><p>Target phrase</p></body></html>");
__block BOOL completion_handler_block_was_called = NO;
id completion_handler_block = ^(BOOL success, CGPoint scrollPosition) {
[manager_ executeJavaScript:@"token"
completionHandler:^(NSString* result, NSError*) {
EXPECT_NSNE(@YES, result);
completion_handler_block_was_called = YES;
}];
};
[manager_ findString:@"');token=true;('"
completionHandler:completion_handler_block];
base::test::ios::WaitUntilCondition(^bool() {
return completion_handler_block_was_called;
});
}
......@@ -63,10 +63,6 @@
// Wrapper around the addSubview method of the webview.
- (void)addSubview:(UIView*)view;
// Returns YES if it makes sense to search for text right now.
// TODO(crbug.com/949651): Remove once JSFindInPageManager is removed.
- (BOOL)hasSearchableTextContent;
// Returns the currently visible keyboard accessory, or nil.
- (UIView*)keyboardAccessory;
......
......@@ -175,10 +175,6 @@ UIView* GetFirstResponderSubview(UIView* view) {
return [_contentView addSubview:view];
}
- (BOOL)hasSearchableTextContent {
return _contentView != nil && [_webController contentIsHTML];
}
- (UIView*)keyboardAccessory {
if (!_contentView)
return nil;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment