| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/android/content_detector.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "third_party/WebKit/public/platform/WebPoint.h" | |
| 9 #include "third_party/WebKit/public/web/WebHitTestResult.h" | |
| 10 #include "third_party/WebKit/public/web/WebSurroundingText.h" | |
| 11 | |
| 12 using blink::WebURL; | |
| 13 using blink::WebHitTestResult; | |
| 14 using blink::WebSurroundingText; | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 WebURL ContentDetector::FindTappedContent(const WebHitTestResult& hit_test) { | |
| 19 if (hit_test.isNull()) | |
| 20 return WebURL(); | |
| 21 | |
| 22 std::string content_text; | |
| 23 if (!FindContentRange(hit_test, &content_text)) | |
| 24 return WebURL(); | |
| 25 | |
| 26 return GetIntentURL(content_text); | |
| 27 } | |
| 28 | |
| 29 bool ContentDetector::FindContentRange(const WebHitTestResult& hit_test, | |
| 30 std::string* content_text) { | |
| 31 // As the surrounding text extractor looks at maxLength/2 characters on | |
| 32 // either side of the hit point, we need to double max content length here. | |
| 33 WebSurroundingText surrounding_text; | |
| 34 surrounding_text.initialize(hit_test.node(), hit_test.localPoint(), | |
| 35 GetMaximumContentLength() * 2); | |
| 36 if (surrounding_text.isNull()) | |
| 37 return false; | |
| 38 | |
| 39 base::string16 content = surrounding_text.textContent().utf16(); | |
| 40 if (content.empty()) | |
| 41 return false; | |
| 42 | |
| 43 size_t selected_offset = surrounding_text.hitOffsetInTextContent(); | |
| 44 for (size_t start_offset = 0; start_offset < content.length();) { | |
| 45 size_t relative_start, relative_end; | |
| 46 if (!FindContent(content.begin() + start_offset, | |
| 47 content.end(), &relative_start, &relative_end, content_text)) { | |
| 48 break; | |
| 49 } else { | |
| 50 size_t content_start = start_offset + relative_start; | |
| 51 size_t content_end = start_offset + relative_end; | |
| 52 DCHECK(content_end <= content.length()); | |
| 53 | |
| 54 if (selected_offset >= content_start && selected_offset < content_end) | |
| 55 return true; | |
| 56 else | |
| 57 start_offset += relative_end; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 } // namespace content | |
| OLD | NEW |