1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 // Magic pretend-to-be-a-chromium-build flags
34 #undef WEBKIT_IMPLEMENTATION
35 #undef LOG
36
37 #include "content/content_detector.h"
38
39 #include "public/android/WebDOMTextContentWalker.h"
40 #include "public/android/WebHitTestInfo.h"
41
42 #include "Document.h"
43 #include "Node.h"
44 #include "Page.h"
45 #include "Settings.h"
46
47 using WebKit::WebDOMTextContentWalker;
48 using WebKit::WebRange;
49
FindTappedContent(const WebKit::WebHitTestInfo & hit_test)50 ContentDetector::Result ContentDetector::FindTappedContent(
51 const WebKit::WebHitTestInfo& hit_test) {
52 if (!IsEnabled(hit_test))
53 return Result();
54 WebKit::WebRange range = FindContentRange(hit_test);
55 if (range.isNull())
56 return Result();
57
58 std::string text = GetContentText(range);
59 GURL intent_url = GetIntentURL(text);
60 return Result(range, text, intent_url);
61 }
62
FindContentRange(const WebKit::WebHitTestInfo & hit_test)63 WebRange ContentDetector::FindContentRange(
64 const WebKit::WebHitTestInfo& hit_test) {
65 WebDOMTextContentWalker content_walker(hit_test, GetMaximumContentLength());
66 string16 content = content_walker.content();
67 if (content.empty())
68 return WebRange();
69
70 size_t selected_offset = content_walker.hitOffsetInContent();
71 for (size_t start_offset = 0; start_offset < content.length();) {
72 size_t relative_start, relative_end;
73 if (!FindContent(content.begin() + start_offset,
74 content.end(), &relative_start, &relative_end)) {
75 break;
76 } else {
77 size_t content_start = start_offset + relative_start;
78 size_t content_end = start_offset + relative_end;
79 DCHECK(content_end <= content.length());
80
81 if (selected_offset >= content_start && selected_offset < content_end) {
82 WebRange range = content_walker.contentOffsetsToRange(
83 content_start, content_end);
84 DCHECK(!range.isNull());
85 return range;
86 } else {
87 start_offset += relative_end;
88 }
89 }
90 }
91
92 return WebRange();
93 }
94
GetSettings(const WebKit::WebHitTestInfo & hit_test)95 WebCore::Settings* ContentDetector::GetSettings(const WebKit::WebHitTestInfo& hit_test) {
96 if (!hit_test.node() || !hit_test.node()->document())
97 return 0;
98 return hit_test.node()->document()->page()->settings();
99 }
100