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

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

Issue 2703473002: [TTS] Extract tapped text before showing UI. (Closed)
Patch Set: Minor update. Created 3 years, 9 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 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 SaveSurroundingText(surrounding_text, start_offset, end_offset); 303 SaveSurroundingText(surrounding_text, start_offset, end_offset);
304 if (context_->CanResolve()) 304 if (context_->CanResolve())
305 SendSurroundingText(kSurroundingSizeForUI); 305 SendSurroundingText(kSurroundingSizeForUI);
306 } 306 }
307 307
308 void ContextualSearchDelegate::SaveSurroundingText( 308 void ContextualSearchDelegate::SaveSurroundingText(
309 const base::string16& surrounding_text, 309 const base::string16& surrounding_text,
310 int start_offset, 310 int start_offset,
311 int end_offset) { 311 int end_offset) {
312 DCHECK(context_ != nullptr); 312 DCHECK(context_ != nullptr);
313 // Sometimes the surroundings are 0, 0, '', so fall back on the selection. 313 // Sometimes the surroundings are 0, 0, '', so exit in that case.
314 // See crbug.com/393100. 314 // See crbug.com/393100.
315 bool use_selection = false; 315 if (start_offset == 0 && end_offset == 0 && surrounding_text.length() == 0)
316 if (start_offset == 0 && end_offset == 0 && surrounding_text.length() == 0) { 316 return;
317 use_selection = true;
318 end_offset = context_->GetOriginalSelectedText().length();
319 }
320 const base::string16& surrounding_text_or_selection(
321 use_selection ? base::UTF8ToUTF16(context_->GetOriginalSelectedText())
322 : surrounding_text);
323 317
324 // Pin the start and end offsets to ensure they point within the string. 318 // Pin the start and end offsets to ensure they point within the string.
325 int surrounding_length = surrounding_text_or_selection.length(); 319 int surrounding_length = surrounding_text.length();
326 start_offset = std::min(surrounding_length, std::max(0, start_offset)); 320 start_offset = std::min(surrounding_length, std::max(0, start_offset));
327 end_offset = std::min(surrounding_length, std::max(0, end_offset)); 321 end_offset = std::min(surrounding_length, std::max(0, end_offset));
328 322
329 context_->SetSelectionSurroundings(start_offset, end_offset, 323 context_->SetSelectionSurroundings(start_offset, end_offset,
330 surrounding_text_or_selection); 324 surrounding_text);
331 325
332 // Call the Icing callback with a shortened copy of the surroundings. 326 // Call the Icing callback with a shortened copy of the surroundings.
333 int icing_surrounding_size = field_trial_->GetIcingSurroundingSize(); 327 int icing_surrounding_size = field_trial_->GetIcingSurroundingSize();
334 size_t selection_start = start_offset; 328 if (icing_surrounding_size >= 0 && start_offset <= end_offset) {
335 size_t selection_end = end_offset; 329 size_t selection_start = start_offset;
336 if (icing_surrounding_size >= 0 && selection_start <= selection_end) { 330 size_t selection_end = end_offset;
337 int icing_padding_each_side = icing_surrounding_size / 2; 331 int icing_padding_each_side = icing_surrounding_size / 2;
338 base::string16 icing_surrounding_text = SurroundingTextForIcing( 332 base::string16 icing_surrounding_text =
339 surrounding_text_or_selection, icing_padding_each_side, 333 SurroundingTextForIcing(surrounding_text, icing_padding_each_side,
340 &selection_start, &selection_end); 334 &selection_start, &selection_end);
341 if (selection_start <= selection_end) 335 if (selection_start <= selection_end)
342 icing_callback_.Run(context_->GetBasePageEncoding(), 336 icing_callback_.Run(context_->GetBasePageEncoding(),
343 icing_surrounding_text, selection_start, 337 icing_surrounding_text, selection_start,
344 selection_end); 338 selection_end);
345 } 339 }
346 } 340 }
347 341
348 void ContextualSearchDelegate::SendSurroundingText(int max_surrounding_chars) { 342 void ContextualSearchDelegate::SendSurroundingText(int max_surrounding_chars) {
349 DCHECK(context_ != nullptr); 343 DCHECK(context_ != nullptr);
350 const base::string16& surrounding = context_->GetSurroundingText(); 344 const base::string16& surrounding = context_->GetSurroundingText();
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 end_offset -= trim; 577 end_offset -= trim;
584 } 578 }
585 if (result_text.length() > end_offset + padding_each_side_pinned) { 579 if (result_text.length() > end_offset + padding_each_side_pinned) {
586 // Trim the end. 580 // Trim the end.
587 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); 581 result_text = result_text.substr(0, end_offset + padding_each_side_pinned);
588 } 582 }
589 *start = start_offset; 583 *start = start_offset;
590 *end = end_offset; 584 *end = end_offset;
591 return result_text; 585 return result_text;
592 } 586 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698