| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 return request; | 289 return request; |
| 290 } | 290 } |
| 291 | 291 |
| 292 void ContextualSearchDelegate::OnTextSurroundingSelectionAvailable( | 292 void ContextualSearchDelegate::OnTextSurroundingSelectionAvailable( |
| 293 const base::string16& surrounding_text, | 293 const base::string16& surrounding_text, |
| 294 int start_offset, | 294 int start_offset, |
| 295 int end_offset) { | 295 int end_offset) { |
| 296 if (context_ == nullptr) | 296 if (context_ == nullptr) |
| 297 return; | 297 return; |
| 298 | 298 |
| 299 // Sometimes the surroundings are 0, 0, '', so fall back on the selection. | 299 // Sometimes the surroundings are 0, 0, '', so run the callback with empty |
| 300 // See crbug.com/393100. | 300 // data in that case. See crbug.com/393100. |
| 301 bool use_selection = false; | |
| 302 if (start_offset == 0 && end_offset == 0 && surrounding_text.length() == 0) { | 301 if (start_offset == 0 && end_offset == 0 && surrounding_text.length() == 0) { |
| 303 use_selection = true; | 302 surrounding_text_callback_.Run(std::string(), base::string16(), 0, 0); |
| 304 end_offset = context_->GetOriginalSelectedText().length(); | 303 return; |
| 305 } | 304 } |
| 306 const base::string16& surrounding_text_or_selection( | |
| 307 use_selection ? base::UTF8ToUTF16(context_->GetOriginalSelectedText()) | |
| 308 : surrounding_text); | |
| 309 | 305 |
| 310 // Pin the start and end offsets to ensure they point within the string. | 306 // Pin the start and end offsets to ensure they point within the string. |
| 311 int surrounding_length = surrounding_text_or_selection.length(); | 307 int surrounding_length = surrounding_text.length(); |
| 312 start_offset = std::min(surrounding_length, std::max(0, start_offset)); | 308 start_offset = std::min(surrounding_length, std::max(0, start_offset)); |
| 313 end_offset = std::min(surrounding_length, std::max(0, end_offset)); | 309 end_offset = std::min(surrounding_length, std::max(0, end_offset)); |
| 314 | 310 |
| 315 context_->SetSelectionSurroundings(start_offset, end_offset, | 311 context_->SetSelectionSurroundings(start_offset, end_offset, |
| 316 surrounding_text_or_selection); | 312 surrounding_text); |
| 317 | 313 |
| 318 // Call the Java surrounding callback with a shortened copy of the | 314 // Call the Java surrounding callback with a shortened copy of the |
| 319 // surroundings to use as a sample of the surrounding text. | 315 // surroundings to use as a sample of the surrounding text. |
| 320 int sample_surrounding_size = field_trial_->GetSampleSurroundingSize(); | 316 int sample_surrounding_size = field_trial_->GetSampleSurroundingSize(); |
| 321 DCHECK(sample_surrounding_size >= 0); | 317 DCHECK(sample_surrounding_size >= 0); |
| 322 DCHECK(start_offset <= end_offset); | 318 DCHECK(start_offset <= end_offset); |
| 323 size_t selection_start = start_offset; | 319 size_t selection_start = start_offset; |
| 324 size_t selection_end = end_offset; | 320 size_t selection_end = end_offset; |
| 325 int sample_padding_each_side = sample_surrounding_size / 2; | 321 int sample_padding_each_side = sample_surrounding_size / 2; |
| 326 base::string16 sample_surrounding_text = SampleSurroundingText( | 322 base::string16 sample_surrounding_text = |
| 327 surrounding_text_or_selection, sample_padding_each_side, &selection_start, | 323 SampleSurroundingText(surrounding_text, sample_padding_each_side, |
| 328 &selection_end); | 324 &selection_start, &selection_end); |
| 329 DCHECK(selection_start <= selection_end); | 325 DCHECK(selection_start <= selection_end); |
| 330 surrounding_text_callback_.Run(context_->GetBasePageEncoding(), | 326 surrounding_text_callback_.Run(context_->GetBasePageEncoding(), |
| 331 sample_surrounding_text, selection_start, | 327 sample_surrounding_text, selection_start, |
| 332 selection_end); | 328 selection_end); |
| 333 } | 329 } |
| 334 | 330 |
| 335 void ContextualSearchDelegate::SetDiscourseContextAndAddToHeader( | 331 void ContextualSearchDelegate::SetDiscourseContextAndAddToHeader( |
| 336 const ContextualSearchContext& context) { | 332 const ContextualSearchContext& context) { |
| 337 search_term_fetcher_->AddExtraRequestHeader(GetDiscourseContext(context)); | 333 search_term_fetcher_->AddExtraRequestHeader(GetDiscourseContext(context)); |
| 338 } | 334 } |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 end_offset -= trim; | 551 end_offset -= trim; |
| 556 } | 552 } |
| 557 if (result_text.length() > end_offset + padding_each_side_pinned) { | 553 if (result_text.length() > end_offset + padding_each_side_pinned) { |
| 558 // Trim the end. | 554 // Trim the end. |
| 559 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 555 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
| 560 } | 556 } |
| 561 *start = start_offset; | 557 *start = start_offset; |
| 562 *end = end_offset; | 558 *end = end_offset; |
| 563 return result_text; | 559 return result_text; |
| 564 } | 560 } |
| OLD | NEW |