OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/renderer_host/chrome_render_view_host_observer.h" | 5 #include "chrome/browser/renderer_host/chrome_render_view_host_observer.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 #if defined(OS_WIN) && defined(USE_AURA) | 199 #if defined(OS_WIN) && defined(USE_AURA) |
200 base::win::DismissVirtualKeyboard(); | 200 base::win::DismissVirtualKeyboard(); |
201 #endif | 201 #endif |
202 } | 202 } |
203 } | 203 } |
204 | 204 |
205 // Handles the image thumbnail for the context node, composes a image search | 205 // Handles the image thumbnail for the context node, composes a image search |
206 // request based on the received thumbnail and opens the request in a new tab. | 206 // request based on the received thumbnail and opens the request in a new tab. |
207 void ChromeRenderViewHostObserver::OnRequestThumbnailForContextNodeACK( | 207 void ChromeRenderViewHostObserver::OnRequestThumbnailForContextNodeACK( |
208 const SkBitmap& bitmap) { | 208 const SkBitmap& bitmap) { |
209 const int kDefaultQualityForImageSearch = 90; | |
210 WebContents* web_contents = | 209 WebContents* web_contents = |
211 WebContents::FromRenderViewHost(render_view_host()); | 210 WebContents::FromRenderViewHost(render_view_host()); |
212 if (!web_contents) | 211 const TemplateURL* const default_provider = |
| 212 TemplateURLServiceFactory::GetForProfile(profile_)-> |
| 213 GetDefaultSearchProvider(); |
| 214 if (!web_contents || !default_provider) |
213 return; | 215 return; |
214 | 216 |
| 217 const int kDefaultQualityForImageSearch = 90; |
215 std::vector<unsigned char> data; | 218 std::vector<unsigned char> data; |
216 if (!gfx::JPEGCodec::Encode( | 219 if (!gfx::JPEGCodec::Encode( |
217 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 220 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
218 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | 221 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), |
219 static_cast<int>(bitmap.rowBytes()), kDefaultQualityForImageSearch, | 222 static_cast<int>(bitmap.rowBytes()), kDefaultQualityForImageSearch, |
220 &data)) | 223 &data)) |
221 return; | 224 return; |
222 | 225 |
223 const TemplateURL* const default_provider = | |
224 TemplateURLServiceFactory::GetForProfile(profile_)-> | |
225 GetDefaultSearchProvider(); | |
226 DCHECK(default_provider); | |
227 TemplateURLRef::SearchTermsArgs search_args = | 226 TemplateURLRef::SearchTermsArgs search_args = |
228 TemplateURLRef::SearchTermsArgs(base::string16()); | 227 TemplateURLRef::SearchTermsArgs(base::string16()); |
229 search_args.image_thumbnail_content = std::string(data.begin(), | 228 search_args.image_thumbnail_content = std::string(data.begin(), data.end()); |
230 data.end()); | |
231 // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL | 229 // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL |
232 // from the ContextMenuParams which creates current context menu. | 230 // from the ContextMenuParams which creates current context menu. |
233 search_args.image_url = GURL(); | 231 search_args.image_url = GURL(); |
234 TemplateURLRef::PostContent post_content; | 232 TemplateURLRef::PostContent post_content; |
235 GURL result(default_provider->image_url_ref().ReplaceSearchTerms( | 233 GURL result(default_provider->image_url_ref().ReplaceSearchTerms( |
236 search_args, &post_content)); | 234 search_args, &post_content)); |
237 if (!result.is_valid()) | 235 if (!result.is_valid()) |
238 return; | 236 return; |
239 | 237 |
240 OpenURLParams open_url_params(result, content::Referrer(), NEW_FOREGROUND_TAB, | 238 OpenURLParams open_url_params(result, content::Referrer(), NEW_FOREGROUND_TAB, |
241 content::PAGE_TRANSITION_LINK, false); | 239 content::PAGE_TRANSITION_LINK, false); |
242 const std::string& content_type = post_content.first; | 240 const std::string& content_type = post_content.first; |
243 std::string& post_data = post_content.second; | 241 std::string* post_data = &post_content.second; |
244 if (!post_data.empty()) { | 242 if (!post_data->empty()) { |
245 DCHECK(!content_type.empty()); | 243 DCHECK(!content_type.empty()); |
246 open_url_params.uses_post = true; | 244 open_url_params.uses_post = true; |
247 open_url_params.browser_initiated_post_data = | 245 open_url_params.browser_initiated_post_data = |
248 base::RefCountedString::TakeString(&post_data); | 246 base::RefCountedString::TakeString(post_data); |
249 open_url_params.extra_headers += base::StringPrintf( | 247 open_url_params.extra_headers += base::StringPrintf( |
250 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, | 248 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, |
251 content_type.c_str()); | 249 content_type.c_str()); |
252 } | 250 } |
253 | |
254 web_contents->OpenURL(open_url_params); | 251 web_contents->OpenURL(open_url_params); |
255 } | 252 } |
OLD | NEW |