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" | |
29 #include "content/public/browser/render_frame_host.h" | 28 #include "content/public/browser/render_frame_host.h" |
30 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
31 #include "net/base/escape.h" | 30 #include "net/base/escape.h" |
32 #include "net/http/http_status_code.h" | 31 #include "net/http/http_status_code.h" |
33 #include "net/url_request/url_fetcher.h" | 32 #include "net/url_request/url_fetcher.h" |
34 #include "url/gurl.h" | 33 #include "url/gurl.h" |
35 | 34 |
36 using content::ContentViewCore; | 35 using content::ContentViewCore; |
37 using content::RenderFrameHost; | 36 using content::RenderFrameHost; |
38 | 37 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 icing_callback_(icing_callback) { | 75 icing_callback_(icing_callback) { |
77 field_trial_.reset(new ContextualSearchFieldTrial()); | 76 field_trial_.reset(new ContextualSearchFieldTrial()); |
78 } | 77 } |
79 | 78 |
80 ContextualSearchDelegate::~ContextualSearchDelegate() { | 79 ContextualSearchDelegate::~ContextualSearchDelegate() { |
81 } | 80 } |
82 | 81 |
83 void ContextualSearchDelegate::StartSearchTermResolutionRequest( | 82 void ContextualSearchDelegate::StartSearchTermResolutionRequest( |
84 const std::string& selection, | 83 const std::string& selection, |
85 bool use_resolved_search_term, | 84 bool use_resolved_search_term, |
86 content::ContentViewCore* content_view_core, | 85 content::WebContents* web_contents, |
87 bool may_send_base_page_url) { | 86 bool may_send_base_page_url) { |
88 GatherSurroundingTextWithCallback( | 87 GatherSurroundingTextWithCallback( |
89 selection, use_resolved_search_term, content_view_core, | 88 selection, use_resolved_search_term, web_contents, may_send_base_page_url, |
90 may_send_base_page_url, | |
91 base::Bind(&ContextualSearchDelegate::StartSearchTermRequestFromSelection, | 89 base::Bind(&ContextualSearchDelegate::StartSearchTermRequestFromSelection, |
92 AsWeakPtr())); | 90 AsWeakPtr())); |
93 } | 91 } |
94 | 92 |
95 void ContextualSearchDelegate::GatherAndSaveSurroundingText( | 93 void ContextualSearchDelegate::GatherAndSaveSurroundingText( |
96 const std::string& selection, | 94 const std::string& selection, |
97 bool use_resolved_search_term, | 95 bool use_resolved_search_term, |
98 content::ContentViewCore* content_view_core, | 96 content::WebContents* web_contents, |
99 bool may_send_base_page_url) { | 97 bool may_send_base_page_url) { |
100 GatherSurroundingTextWithCallback( | 98 GatherSurroundingTextWithCallback( |
101 selection, use_resolved_search_term, content_view_core, | 99 selection, use_resolved_search_term, web_contents, may_send_base_page_url, |
102 may_send_base_page_url, | |
103 base::Bind(&ContextualSearchDelegate::SaveSurroundingText, AsWeakPtr())); | 100 base::Bind(&ContextualSearchDelegate::SaveSurroundingText, AsWeakPtr())); |
104 // TODO(donnd): clear the context here, since we're done with it (but risky). | 101 // TODO(donnd): clear the context here, since we're done with it (but risky). |
105 } | 102 } |
106 | 103 |
107 void ContextualSearchDelegate::ContinueSearchTermResolutionRequest() { | 104 void ContextualSearchDelegate::ContinueSearchTermResolutionRequest() { |
108 DCHECK(context_.get()); | 105 DCHECK(context_.get()); |
109 if (!context_.get()) | 106 if (!context_.get()) |
110 return; | 107 return; |
111 GURL request_url(BuildRequestUrl(context_->selected_text)); | 108 GURL request_url(BuildRequestUrl(context_->selected_text)); |
112 DCHECK(request_url.is_valid()); | 109 DCHECK(request_url.is_valid()); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 request.replace(0, pos + strlen(kContextualSearchServerEndpoint), | 223 request.replace(0, pos + strlen(kContextualSearchServerEndpoint), |
227 replacement_url); | 224 replacement_url); |
228 } | 225 } |
229 } | 226 } |
230 return request; | 227 return request; |
231 } | 228 } |
232 | 229 |
233 void ContextualSearchDelegate::GatherSurroundingTextWithCallback( | 230 void ContextualSearchDelegate::GatherSurroundingTextWithCallback( |
234 const std::string& selection, | 231 const std::string& selection, |
235 bool use_resolved_search_term, | 232 bool use_resolved_search_term, |
236 content::ContentViewCore* content_view_core, | 233 content::WebContents* web_contents, |
237 bool may_send_base_page_url, | 234 bool may_send_base_page_url, |
238 HandleSurroundingsCallback callback) { | 235 HandleSurroundingsCallback callback) { |
239 DCHECK(content_view_core); | 236 DCHECK(web_contents); |
240 DCHECK(content_view_core->GetWebContents()); | |
241 DCHECK(!callback.is_null()); | 237 DCHECK(!callback.is_null()); |
242 DCHECK(!selection.empty()); | 238 DCHECK(!selection.empty()); |
243 RenderFrameHost* focused_frame = | 239 RenderFrameHost* focused_frame = web_contents->GetFocusedFrame(); |
244 content_view_core->GetWebContents()->GetFocusedFrame(); | |
245 if (!focused_frame) { | 240 if (!focused_frame) { |
246 callback.Run(base::string16(), 0, 0); | 241 callback.Run(base::string16(), 0, 0); |
247 return; | 242 return; |
248 } | 243 } |
249 // Immediately cancel any request that's in flight, since we're building a new | 244 // Immediately cancel any request that's in flight, since we're building a new |
250 // context (and the response disposes of any existing context). | 245 // context (and the response disposes of any existing context). |
251 search_term_fetcher_.reset(); | 246 search_term_fetcher_.reset(); |
252 BuildContext(selection, use_resolved_search_term, content_view_core, | 247 BuildContext(selection, use_resolved_search_term, web_contents, |
253 may_send_base_page_url); | 248 may_send_base_page_url); |
254 focused_frame->RequestTextSurroundingSelection( | 249 focused_frame->RequestTextSurroundingSelection( |
255 callback, field_trial_->GetSurroundingSize()); | 250 callback, field_trial_->GetSurroundingSize()); |
256 } | 251 } |
257 | 252 |
258 void ContextualSearchDelegate::BuildContext( | 253 void ContextualSearchDelegate::BuildContext( |
259 const std::string& selection, | 254 const std::string& selection, |
260 bool use_resolved_search_term, | 255 bool use_resolved_search_term, |
261 content::ContentViewCore* content_view_core, | 256 content::WebContents* web_contents, |
262 bool may_send_base_page_url) { | 257 bool may_send_base_page_url) { |
263 // Decide if the URL should be sent with the context. | 258 // Decide if the URL should be sent with the context. |
264 GURL page_url(content_view_core->GetWebContents()->GetURL()); | 259 GURL page_url(web_contents->GetURL()); |
265 GURL url_to_send; | 260 GURL url_to_send; |
266 if (may_send_base_page_url && | 261 if (may_send_base_page_url && |
267 CanSendPageURL(page_url, ProfileManager::GetActiveUserProfile(), | 262 CanSendPageURL(page_url, ProfileManager::GetActiveUserProfile(), |
268 template_url_service_)) { | 263 template_url_service_)) { |
269 url_to_send = page_url; | 264 url_to_send = page_url; |
270 } | 265 } |
271 std::string encoding(content_view_core->GetWebContents()->GetEncoding()); | 266 std::string encoding(web_contents->GetEncoding()); |
272 context_.reset(new ContextualSearchContext( | 267 context_.reset(new ContextualSearchContext( |
273 selection, use_resolved_search_term, url_to_send, encoding)); | 268 selection, use_resolved_search_term, url_to_send, encoding)); |
274 } | 269 } |
275 | 270 |
276 void ContextualSearchDelegate::StartSearchTermRequestFromSelection( | 271 void ContextualSearchDelegate::StartSearchTermRequestFromSelection( |
277 const base::string16& surrounding_text, | 272 const base::string16& surrounding_text, |
278 int start_offset, | 273 int start_offset, |
279 int end_offset) { | 274 int end_offset) { |
280 // TODO(donnd): figure out how to gather text surrounding the selection | 275 // TODO(donnd): figure out how to gather text surrounding the selection |
281 // for other purposes too: e.g. to determine if we should select the | 276 // for other purposes too: e.g. to determine if we should select the |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 end_offset -= trim; | 513 end_offset -= trim; |
519 } | 514 } |
520 if (result_text.length() > end_offset + padding_each_side_pinned) { | 515 if (result_text.length() > end_offset + padding_each_side_pinned) { |
521 // Trim the end. | 516 // Trim the end. |
522 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 517 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
523 } | 518 } |
524 *start = start_offset; | 519 *start = start_offset; |
525 *end = end_offset; | 520 *end = end_offset; |
526 return result_text; | 521 return result_text; |
527 } | 522 } |
OLD | NEW |