OLD | NEW |
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 "chrome/browser/android/contextualsearch/contextual_search_delegate.h" | 5 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 241 } |
242 return request; | 242 return request; |
243 } | 243 } |
244 | 244 |
245 void ContextualSearchDelegate::GatherSurroundingTextWithCallback( | 245 void ContextualSearchDelegate::GatherSurroundingTextWithCallback( |
246 const std::string& selection, | 246 const std::string& selection, |
247 bool use_resolved_search_term, | 247 bool use_resolved_search_term, |
248 content::WebContents* web_contents, | 248 content::WebContents* web_contents, |
249 bool may_send_base_page_url, | 249 bool may_send_base_page_url, |
250 HandleSurroundingsCallback callback) { | 250 HandleSurroundingsCallback callback) { |
251 DCHECK(web_contents); | |
252 DCHECK(!callback.is_null()); | |
253 DCHECK(!selection.empty()); | |
254 RenderFrameHost* focused_frame = web_contents->GetFocusedFrame(); | |
255 if (!focused_frame) { | |
256 callback.Run(base::string16(), 0, 0); | |
257 return; | |
258 } | |
259 // Immediately cancel any request that's in flight, since we're building a new | 251 // Immediately cancel any request that's in flight, since we're building a new |
260 // context (and the response disposes of any existing context). | 252 // context (and the response disposes of any existing context). |
261 search_term_fetcher_.reset(); | 253 search_term_fetcher_.reset(); |
262 BuildContext(selection, use_resolved_search_term, web_contents, | 254 BuildContext(selection, use_resolved_search_term, web_contents, |
263 may_send_base_page_url); | 255 may_send_base_page_url); |
264 focused_frame->RequestTextSurroundingSelection( | 256 DCHECK(web_contents); |
265 callback, field_trial_->GetSurroundingSize()); | 257 RenderFrameHost* focused_frame = web_contents->GetFocusedFrame(); |
| 258 if (focused_frame) { |
| 259 focused_frame->RequestTextSurroundingSelection( |
| 260 callback, field_trial_->GetSurroundingSize()); |
| 261 } else { |
| 262 callback.Run(base::string16(), 0, 0); |
| 263 } |
266 } | 264 } |
267 | 265 |
268 void ContextualSearchDelegate::BuildContext( | 266 void ContextualSearchDelegate::BuildContext( |
269 const std::string& selection, | 267 const std::string& selection, |
270 bool use_resolved_search_term, | 268 bool use_resolved_search_term, |
271 content::WebContents* web_contents, | 269 content::WebContents* web_contents, |
272 bool may_send_base_page_url) { | 270 bool may_send_base_page_url) { |
273 // Decide if the URL should be sent with the context. | 271 // Decide if the URL should be sent with the context. |
274 GURL page_url(web_contents->GetURL()); | 272 GURL page_url(web_contents->GetURL()); |
275 GURL url_to_send; | 273 GURL url_to_send; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 end_offset -= trim; | 537 end_offset -= trim; |
540 } | 538 } |
541 if (result_text.length() > end_offset + padding_each_side_pinned) { | 539 if (result_text.length() > end_offset + padding_each_side_pinned) { |
542 // Trim the end. | 540 // Trim the end. |
543 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 541 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
544 } | 542 } |
545 *start = start_offset; | 543 *start = start_offset; |
546 *end = end_offset; | 544 *end = end_offset; |
547 return result_text; | 545 return result_text; |
548 } | 546 } |
OLD | NEW |