Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: chrome/browser/android/contextualsearch/contextual_search_delegate.cc

Issue 2703473002: [TTS] Extract tapped text before showing UI. (Closed)
Patch Set: Moved handleHideContextualSearch into hideContextualSearchUI, and updated comments in response to T… Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <utility> 8 #include <utility>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 return request; 290 return request;
291 } 291 }
292 292
293 void ContextualSearchDelegate::OnTextSurroundingSelectionAvailable( 293 void ContextualSearchDelegate::OnTextSurroundingSelectionAvailable(
294 const base::string16& surrounding_text, 294 const base::string16& surrounding_text,
295 int start_offset, 295 int start_offset,
296 int end_offset) { 296 int end_offset) {
297 if (context_ == nullptr) 297 if (context_ == nullptr)
298 return; 298 return;
299 299
300 // Sometimes the surroundings are 0, 0, '', so fall back on the selection. 300 // Sometimes the surroundings are 0, 0, '', so run the callback with empty
301 // See crbug.com/393100. 301 // data in that case. See crbug.com/393100.
302 bool use_selection = false;
303 if (start_offset == 0 && end_offset == 0 && surrounding_text.length() == 0) { 302 if (start_offset == 0 && end_offset == 0 && surrounding_text.length() == 0) {
304 use_selection = true; 303 surrounding_text_callback_.Run(std::string(), base::string16(), 0, 0);
305 end_offset = context_->GetOriginalSelectedText().length(); 304 return;
306 } 305 }
307 const base::string16& surrounding_text_or_selection(
308 use_selection ? base::UTF8ToUTF16(context_->GetOriginalSelectedText())
309 : surrounding_text);
310 306
311 // Pin the start and end offsets to ensure they point within the string. 307 // Pin the start and end offsets to ensure they point within the string.
312 int surrounding_length = surrounding_text_or_selection.length(); 308 int surrounding_length = surrounding_text.length();
313 start_offset = std::min(surrounding_length, std::max(0, start_offset)); 309 start_offset = std::min(surrounding_length, std::max(0, start_offset));
314 end_offset = std::min(surrounding_length, std::max(0, end_offset)); 310 end_offset = std::min(surrounding_length, std::max(0, end_offset));
315 311
316 context_->SetSelectionSurroundings(start_offset, end_offset, 312 context_->SetSelectionSurroundings(start_offset, end_offset,
317 surrounding_text_or_selection); 313 surrounding_text);
318 314
319 // Call the Java surrounding callback with a shortened copy of the 315 // Call the Java surrounding callback with a shortened copy of the
320 // surroundings to use as a sample of the surrounding text. 316 // surroundings to use as a sample of the surrounding text.
321 int sample_surrounding_size = field_trial_->GetSampleSurroundingSize(); 317 int sample_surrounding_size = field_trial_->GetSampleSurroundingSize();
322 DCHECK(sample_surrounding_size >= 0); 318 DCHECK(sample_surrounding_size >= 0);
323 DCHECK(start_offset <= end_offset); 319 DCHECK(start_offset <= end_offset);
324 size_t selection_start = start_offset; 320 size_t selection_start = start_offset;
325 size_t selection_end = end_offset; 321 size_t selection_end = end_offset;
326 int sample_padding_each_side = sample_surrounding_size / 2; 322 int sample_padding_each_side = sample_surrounding_size / 2;
327 base::string16 sample_surrounding_text = SampleSurroundingText( 323 base::string16 sample_surrounding_text =
328 surrounding_text_or_selection, sample_padding_each_side, &selection_start, 324 SampleSurroundingText(surrounding_text, sample_padding_each_side,
329 &selection_end); 325 &selection_start, &selection_end);
330 DCHECK(selection_start <= selection_end); 326 DCHECK(selection_start <= selection_end);
331 surrounding_text_callback_.Run(context_->GetBasePageEncoding(), 327 surrounding_text_callback_.Run(context_->GetBasePageEncoding(),
332 sample_surrounding_text, selection_start, 328 sample_surrounding_text, selection_start,
333 selection_end); 329 selection_end);
334 } 330 }
335 331
336 void ContextualSearchDelegate::SetDiscourseContextAndAddToHeader( 332 void ContextualSearchDelegate::SetDiscourseContextAndAddToHeader(
337 const ContextualSearchContext& context) { 333 const ContextualSearchContext& context) {
338 search_term_fetcher_->AddExtraRequestHeader(GetDiscourseContext(context)); 334 search_term_fetcher_->AddExtraRequestHeader(GetDiscourseContext(context));
339 } 335 }
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 end_offset -= trim; 552 end_offset -= trim;
557 } 553 }
558 if (result_text.length() > end_offset + padding_each_side_pinned) { 554 if (result_text.length() > end_offset + padding_each_side_pinned) {
559 // Trim the end. 555 // Trim the end.
560 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); 556 result_text = result_text.substr(0, end_offset + padding_each_side_pinned);
561 } 557 }
562 *start = start_offset; 558 *start = start_offset;
563 *end = end_offset; 559 *end = end_offset;
564 return result_text; 560 return result_text;
565 } 561 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698