| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/renderer/android/content_detector.h" | 5 #include "content/renderer/android/content_detector.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "third_party/WebKit/public/platform/WebPoint.h" | 8 #include "third_party/WebKit/public/platform/WebPoint.h" |
| 9 #include "third_party/WebKit/public/web/WebHitTestResult.h" | 9 #include "third_party/WebKit/public/web/WebHitTestResult.h" |
| 10 #include "third_party/WebKit/public/web/WebSurroundingText.h" | 10 #include "third_party/WebKit/public/web/WebSurroundingText.h" |
| 11 | 11 |
| 12 using blink::WebRange; | 12 using blink::WebURL; |
| 13 using blink::WebHitTestResult; |
| 13 using blink::WebSurroundingText; | 14 using blink::WebSurroundingText; |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| 17 ContentDetector::Result::Result() : valid(false) {} | 18 WebURL ContentDetector::FindTappedContent(const WebHitTestResult& hit_test) { |
| 19 if (hit_test.isNull()) |
| 20 return WebURL(); |
| 18 | 21 |
| 19 ContentDetector::Result::Result(const blink::WebRange& content_boundaries, | 22 std::string content_text; |
| 20 const std::string& text, | 23 if (!FindContentRange(hit_test, &content_text)) |
| 21 const GURL& intent_url) | 24 return WebURL(); |
| 22 : valid(true), | 25 |
| 23 content_boundaries(content_boundaries), | 26 return GetIntentURL(content_text); |
| 24 text(text), | |
| 25 intent_url(intent_url) { | |
| 26 } | 27 } |
| 27 | 28 |
| 28 ContentDetector::Result::Result(const Result& other) = default; | 29 bool ContentDetector::FindContentRange(const WebHitTestResult& hit_test, |
| 29 | 30 std::string* content_text) { |
| 30 ContentDetector::Result::~Result() {} | |
| 31 | |
| 32 ContentDetector::Result ContentDetector::FindTappedContent( | |
| 33 const blink::WebHitTestResult& hit_test) { | |
| 34 if (hit_test.isNull()) | |
| 35 return Result(); | |
| 36 | |
| 37 std::string content_text; | |
| 38 blink::WebRange range = FindContentRange(hit_test, &content_text); | |
| 39 if (range.isNull()) | |
| 40 return Result(); | |
| 41 | |
| 42 GURL intent_url = GetIntentURL(content_text); | |
| 43 return Result(range, content_text, intent_url); | |
| 44 } | |
| 45 | |
| 46 WebRange ContentDetector::FindContentRange( | |
| 47 const blink::WebHitTestResult& hit_test, | |
| 48 std::string* content_text) { | |
| 49 // As the surrounding text extractor looks at maxLength/2 characters on | 31 // As the surrounding text extractor looks at maxLength/2 characters on |
| 50 // either side of the hit point, we need to double max content length here. | 32 // either side of the hit point, we need to double max content length here. |
| 51 WebSurroundingText surrounding_text; | 33 WebSurroundingText surrounding_text; |
| 52 surrounding_text.initialize(hit_test.node(), hit_test.localPoint(), | 34 surrounding_text.initialize(hit_test.node(), hit_test.localPoint(), |
| 53 GetMaximumContentLength() * 2); | 35 GetMaximumContentLength() * 2); |
| 54 if (surrounding_text.isNull()) | 36 if (surrounding_text.isNull()) |
| 55 return WebRange(); | 37 return false; |
| 56 | 38 |
| 57 base::string16 content = surrounding_text.textContent(); | 39 base::string16 content = surrounding_text.textContent(); |
| 58 if (content.empty()) | 40 if (content.empty()) |
| 59 return WebRange(); | 41 return false; |
| 60 | 42 |
| 61 size_t selected_offset = surrounding_text.hitOffsetInTextContent(); | 43 size_t selected_offset = surrounding_text.hitOffsetInTextContent(); |
| 62 for (size_t start_offset = 0; start_offset < content.length();) { | 44 for (size_t start_offset = 0; start_offset < content.length();) { |
| 63 size_t relative_start, relative_end; | 45 size_t relative_start, relative_end; |
| 64 if (!FindContent(content.begin() + start_offset, | 46 if (!FindContent(content.begin() + start_offset, |
| 65 content.end(), &relative_start, &relative_end, content_text)) { | 47 content.end(), &relative_start, &relative_end, content_text)) { |
| 66 break; | 48 break; |
| 67 } else { | 49 } else { |
| 68 size_t content_start = start_offset + relative_start; | 50 size_t content_start = start_offset + relative_start; |
| 69 size_t content_end = start_offset + relative_end; | 51 size_t content_end = start_offset + relative_end; |
| 70 DCHECK(content_end <= content.length()); | 52 DCHECK(content_end <= content.length()); |
| 71 | 53 |
| 72 if (selected_offset >= content_start && selected_offset < content_end) { | 54 if (selected_offset >= content_start && selected_offset < content_end) |
| 73 WebRange range = surrounding_text.rangeFromContentOffsets( | 55 return true; |
| 74 content_start, content_end); | 56 else |
| 75 DCHECK(!range.isNull()); | |
| 76 return range; | |
| 77 } else { | |
| 78 start_offset += relative_end; | 57 start_offset += relative_end; |
| 79 } | |
| 80 } | 58 } |
| 81 } | 59 } |
| 82 | 60 |
| 83 return WebRange(); | 61 return false; |
| 84 } | 62 } |
| 85 | 63 |
| 86 } // namespace content | 64 } // namespace content |
| OLD | NEW |