Chromium Code Reviews| 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" |
| 11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/android/contextualsearch/contextual_search_field_trial. h" | 15 #include "chrome/browser/android/contextualsearch/contextual_search_field_trial. h" |
| 16 #include "chrome/browser/android/contextualsearch/resolved_search_term.h" | 16 #include "chrome/browser/android/contextualsearch/resolved_search_term.h" |
| 17 #include "chrome/browser/android/proto/client_discourse_context.pb.h" | 17 #include "chrome/browser/android/proto/client_discourse_context.pb.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
| 20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 20 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 21 #include "chrome/browser/translate/translate_service.h" | 21 #include "chrome/browser/translate/translate_service.h" |
| 22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 23 #include "components/browser_sync/browser/profile_sync_service.h" | 23 #include "components/browser_sync/browser/profile_sync_service.h" |
| 24 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
| 25 #include "components/search_engines/template_url_service.h" | 25 #include "components/search_engines/template_url_service.h" |
| 26 #include "components/variations/net/variations_http_headers.h" | 26 #include "components/variations/net/variations_http_headers.h" |
| 27 #include "components/variations/variations_associated_data.h" | 27 #include "components/variations/variations_associated_data.h" |
| 28 #include "content/public/browser/android/content_view_core.h" | 28 #include "content/public/browser/android/content_view_core.h" |
| 29 #include "content/public/browser/render_frame_host.h" | |
| 29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 30 #include "net/base/escape.h" | 31 #include "net/base/escape.h" |
| 31 #include "net/http/http_status_code.h" | 32 #include "net/http/http_status_code.h" |
| 32 #include "net/url_request/url_fetcher.h" | 33 #include "net/url_request/url_fetcher.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 using content::ContentViewCore; | 36 using content::ContentViewCore; |
| 37 using content::RenderFrameHost; | |
| 36 | 38 |
| 37 namespace { | 39 namespace { |
| 38 | 40 |
| 39 const char kContextualSearchResponseDisplayTextParam[] = "display_text"; | 41 const char kContextualSearchResponseDisplayTextParam[] = "display_text"; |
| 40 const char kContextualSearchResponseSelectedTextParam[] = "selected_text"; | 42 const char kContextualSearchResponseSelectedTextParam[] = "selected_text"; |
| 41 const char kContextualSearchResponseSearchTermParam[] = "search_term"; | 43 const char kContextualSearchResponseSearchTermParam[] = "search_term"; |
| 42 const char kContextualSearchResponseLanguageParam[] = "lang"; | 44 const char kContextualSearchResponseLanguageParam[] = "lang"; |
| 43 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term"; | 45 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term"; |
| 44 const char kContextualSearchPreventPreload[] = "prevent_preload"; | 46 const char kContextualSearchPreventPreload[] = "prevent_preload"; |
| 45 const char kContextualSearchMentions[] = "mentions"; | 47 const char kContextualSearchMentions[] = "mentions"; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 } | 229 } |
| 228 return request; | 230 return request; |
| 229 } | 231 } |
| 230 | 232 |
| 231 void ContextualSearchDelegate::GatherSurroundingTextWithCallback( | 233 void ContextualSearchDelegate::GatherSurroundingTextWithCallback( |
| 232 const std::string& selection, | 234 const std::string& selection, |
| 233 bool use_resolved_search_term, | 235 bool use_resolved_search_term, |
| 234 content::ContentViewCore* content_view_core, | 236 content::ContentViewCore* content_view_core, |
| 235 bool may_send_base_page_url, | 237 bool may_send_base_page_url, |
| 236 HandleSurroundingsCallback callback) { | 238 HandleSurroundingsCallback callback) { |
| 239 DCHECK(content_view_core && content_view_core->GetWebContents()); | |
|
Ted C
2016/07/20 22:23:29
this seems unneeded to me. we weren't null checki
AKV
2016/07/21 09:13:52
Done.
| |
| 240 DCHECK(!callback.is_null()); | |
| 241 DCHECK(!selection.empty()); | |
| 242 RenderFrameHost* focused_frame = | |
| 243 content_view_core->GetWebContents()->GetFocusedFrame(); | |
| 244 if (!focused_frame) { | |
| 245 callback.Run(base::string16(), 0, 0); | |
| 246 return; | |
| 247 } | |
| 237 // Immediately cancel any request that's in flight, since we're building a new | 248 // Immediately cancel any request that's in flight, since we're building a new |
| 238 // context (and the response disposes of any existing context). | 249 // context (and the response disposes of any existing context). |
| 239 search_term_fetcher_.reset(); | 250 search_term_fetcher_.reset(); |
| 240 BuildContext(selection, use_resolved_search_term, content_view_core, | 251 BuildContext(selection, use_resolved_search_term, content_view_core, |
| 241 may_send_base_page_url); | 252 may_send_base_page_url); |
| 242 content_view_core->RequestTextSurroundingSelection( | 253 focused_frame->RequestTextSurroundingSelection( |
| 243 field_trial_->GetSurroundingSize(), callback); | 254 callback, field_trial_->GetSurroundingSize()); |
| 244 } | 255 } |
| 245 | 256 |
| 246 void ContextualSearchDelegate::BuildContext( | 257 void ContextualSearchDelegate::BuildContext( |
| 247 const std::string& selection, | 258 const std::string& selection, |
| 248 bool use_resolved_search_term, | 259 bool use_resolved_search_term, |
| 249 content::ContentViewCore* content_view_core, | 260 content::ContentViewCore* content_view_core, |
| 250 bool may_send_base_page_url) { | 261 bool may_send_base_page_url) { |
| 251 // Decide if the URL should be sent with the context. | 262 // Decide if the URL should be sent with the context. |
| 252 GURL page_url(content_view_core->GetWebContents()->GetURL()); | 263 GURL page_url(content_view_core->GetWebContents()->GetURL()); |
| 253 GURL url_to_send; | 264 GURL url_to_send; |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 end_offset -= trim; | 517 end_offset -= trim; |
| 507 } | 518 } |
| 508 if (result_text.length() > end_offset + padding_each_side_pinned) { | 519 if (result_text.length() > end_offset + padding_each_side_pinned) { |
| 509 // Trim the end. | 520 // Trim the end. |
| 510 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 521 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
| 511 } | 522 } |
| 512 *start = start_offset; | 523 *start = start_offset; |
| 513 *end = end_offset; | 524 *end = end_offset; |
| 514 return result_text; | 525 return result_text; |
| 515 } | 526 } |
| OLD | NEW |