Chromium Code Reviews| Index: chrome/browser/android/contextualsearch/contextual_search_delegate.cc |
| diff --git a/chrome/browser/android/contextualsearch/contextual_search_delegate.cc b/chrome/browser/android/contextualsearch/contextual_search_delegate.cc |
| index bde0a2f5594dfb2e5fc30f4954fa134fa1754bab..9847af79f77a7e346ef134e46564a93f726d0117 100644 |
| --- a/chrome/browser/android/contextualsearch/contextual_search_delegate.cc |
| +++ b/chrome/browser/android/contextualsearch/contextual_search_delegate.cc |
| @@ -98,31 +98,54 @@ ContextualSearchDelegate::ContextualSearchDelegate( |
| ContextualSearchDelegate::~ContextualSearchDelegate() { |
| } |
| -void ContextualSearchDelegate::StartSearchTermResolutionRequest( |
| - const std::string& selection, |
| - const std::string& home_country, |
| - content::WebContents* web_contents, |
| - bool may_send_base_page_url) { |
| - GatherSurroundingTextWithCallback( |
| - selection, home_country, web_contents, may_send_base_page_url, |
| - base::Bind(&ContextualSearchDelegate::StartSearchTermRequestFromSelection, |
| - AsWeakPtr())); |
| +void ContextualSearchDelegate::GatherAndSaveSurroundingText( |
| + ContextualSearchContext* contextual_search_context, |
| + content::WebContents* web_contents) { |
| + DCHECK(web_contents); |
| + RenderFrameHost::TextSurroundingSelectionCallback callback = |
| + base::Bind(&ContextualSearchDelegate::OnTextSurroundingSelectionAvailable, |
| + AsWeakPtr()); |
| + context_ = contextual_search_context; |
| + int surroundingTextSize = context_->IsBrief() |
| + ? field_trial_->GetIcingSurroundingSize() |
| + : field_trial_->GetSurroundingSize(); |
| + RenderFrameHost* focused_frame = web_contents->GetFocusedFrame(); |
| + if (focused_frame) { |
| + focused_frame->RequestTextSurroundingSelection(callback, |
| + surroundingTextSize); |
| + } else { |
| + callback.Run(base::string16(), 0, 0); |
| + } |
| } |
| -void ContextualSearchDelegate::GatherAndSaveSurroundingText( |
| - const std::string& selection, |
| - const std::string& home_country, |
| - content::WebContents* web_contents, |
| - bool may_send_base_page_url) { |
| - GatherSurroundingTextWithCallback( |
| - selection, home_country, web_contents, may_send_base_page_url, |
| - base::Bind(&ContextualSearchDelegate::SaveSurroundingText, AsWeakPtr())); |
| - // TODO(donnd): clear the context here, since we're done with it (but risky). |
| +void ContextualSearchDelegate::StartSearchTermResolutionRequest( |
|
Theresa
2017/03/08 01:54:38
nit: It looks like this method and the one above g
Donn Denman
2017/03/09 17:35:05
Fixed -- Changed the order in the header file to b
|
| + ContextualSearchContext* contextual_search_context, |
| + content::WebContents* web_contents) { |
| + DCHECK(web_contents); |
| + DCHECK(context_ == contextual_search_context); |
| + DCHECK(!context_->IsBrief()); |
| + |
| + // Immediately cancel any request that's in flight, since we're building a new |
| + // context (and the response disposes of any existing context). |
| + search_term_fetcher_.reset(); |
| + |
| + // Decide if the URL should be sent with the context. |
| + GURL page_url(web_contents->GetURL()); |
| + GURL url_to_send; |
| + if (context_->MaySendBasePageUrl() && |
| + CanSendPageURL(page_url, ProfileManager::GetActiveUserProfile(), |
| + template_url_service_)) { |
| + url_to_send = page_url; |
| + } |
| + std::string encoding(web_contents->GetEncoding()); |
| + context_->page_url = url_to_send; |
| + context_->encoding = encoding; |
| + ResolveSearchTermFromContext(); |
| } |
| -void ContextualSearchDelegate::ContinueSearchTermResolutionRequest() { |
| - DCHECK(context_.get()); |
| - if (!context_.get()) |
| +void ContextualSearchDelegate::ResolveSearchTermFromContext() { |
| + DCHECK(context_); |
| + if (!context_) |
| return; |
| GURL request_url(BuildRequestUrl(context_->home_country)); |
| DCHECK(request_url.is_valid()); |
| @@ -166,9 +189,6 @@ void ContextualSearchDelegate::OnURLFetchComplete( |
| } |
| } |
| search_term_callback_.Run(*resolved_search_term); |
| - |
| - // The ContextualSearchContext is consumed once the request has completed. |
| - context_.reset(); |
| } |
| std::unique_ptr<ResolvedSearchTerm> |
| @@ -270,64 +290,20 @@ std::string ContextualSearchDelegate::BuildRequestUrl( |
| return request; |
| } |
| -void ContextualSearchDelegate::GatherSurroundingTextWithCallback( |
| - const std::string& selection, |
| - const std::string& home_country, |
| - content::WebContents* web_contents, |
| - bool may_send_base_page_url, |
| - HandleSurroundingsCallback callback) { |
| - // Immediately cancel any request that's in flight, since we're building a new |
| - // context (and the response disposes of any existing context). |
| - search_term_fetcher_.reset(); |
| - BuildContext(selection, home_country, web_contents, may_send_base_page_url); |
| - DCHECK(web_contents); |
| - RenderFrameHost* focused_frame = web_contents->GetFocusedFrame(); |
| - if (focused_frame) { |
| - focused_frame->RequestTextSurroundingSelection( |
| - callback, field_trial_->GetSurroundingSize()); |
| - } else { |
| - callback.Run(base::string16(), 0, 0); |
| - } |
| -} |
| - |
| -void ContextualSearchDelegate::BuildContext(const std::string& selection, |
| - const std::string& home_country, |
| - content::WebContents* web_contents, |
| - bool may_send_base_page_url) { |
| - // Decide if the URL should be sent with the context. |
| - GURL page_url(web_contents->GetURL()); |
| - GURL url_to_send; |
| - if (may_send_base_page_url && |
| - CanSendPageURL(page_url, ProfileManager::GetActiveUserProfile(), |
| - template_url_service_)) { |
| - url_to_send = page_url; |
| - } |
| - std::string encoding(web_contents->GetEncoding()); |
| - context_.reset(new ContextualSearchContext(selection, home_country, |
| - url_to_send, encoding)); |
| -} |
| - |
| -void ContextualSearchDelegate::StartSearchTermRequestFromSelection( |
| +void ContextualSearchDelegate::OnTextSurroundingSelectionAvailable( |
| const base::string16& surrounding_text, |
| int start_offset, |
| int end_offset) { |
| - // TODO(donnd): figure out how to gather text surrounding the selection |
| - // for other purposes too: e.g. to determine if we should select the |
| - // word where the user tapped. |
| - if (context_.get()) { |
| - SaveSurroundingText(surrounding_text, start_offset, end_offset); |
| + SaveSurroundingText(surrounding_text, start_offset, end_offset); |
| + if (!context_->IsBrief()) |
| SendSurroundingText(kSurroundingSizeForUI); |
| - ContinueSearchTermResolutionRequest(); |
| - } else { |
| - DVLOG(1) << "ctxs: Null context, ignored!"; |
| - } |
| } |
| void ContextualSearchDelegate::SaveSurroundingText( |
| const base::string16& surrounding_text, |
| int start_offset, |
| int end_offset) { |
| - DCHECK(context_.get()); |
| + DCHECK(context_); |
| // Sometimes the surroundings are 0, 0, '', so fall back on the selection. |
| // See crbug.com/393100. |
| if (start_offset == 0 && end_offset == 0 && surrounding_text.length() == 0) { |
| @@ -351,12 +327,12 @@ void ContextualSearchDelegate::SaveSurroundingText( |
| int icing_surrounding_size = field_trial_->GetIcingSurroundingSize(); |
| size_t selection_start = context_->start_offset; |
| size_t selection_end = context_->end_offset; |
| - if (icing_surrounding_size >= 0 && selection_start < selection_end) { |
| + if (icing_surrounding_size >= 0 && selection_start <= selection_end) { |
| int icing_padding_each_side = icing_surrounding_size / 2; |
| base::string16 icing_surrounding_text = SurroundingTextForIcing( |
| context_->surrounding_text, icing_padding_each_side, &selection_start, |
| &selection_end); |
| - if (selection_start < selection_end) |
| + if (selection_start <= selection_end) |
| icing_callback_.Run(context_->encoding, icing_surrounding_text, |
| selection_start, selection_end); |
| } |