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/ui/tab_contents/core_tab_helper.h" | 5 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_command_controller.h" | 16 #include "chrome/browser/ui/browser_command_controller.h" |
17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/render_messages.h" | 19 #include "chrome/common/render_messages.h" |
20 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
21 #include "components/search_engines/template_url.h" | 21 #include "components/search_engines/template_url.h" |
22 #include "components/search_engines/template_url_service.h" | 22 #include "components/search_engines/template_url_service.h" |
23 #include "components/web_cache/browser/web_cache_manager.h" | 23 #include "components/web_cache/browser/web_cache_manager.h" |
24 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
25 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
27 #include "net/base/load_states.h" | 27 #include "net/base/load_states.h" |
28 #include "net/http/http_request_headers.h" | 28 #include "net/http/http_request_headers.h" |
29 #include "third_party/skia/include/core/SkBitmap.h" | |
30 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
31 #include "ui/gfx/codec/jpeg_codec.h" | |
32 | 30 |
33 using content::WebContents; | 31 using content::WebContents; |
34 | 32 |
35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper); | 33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper); |
36 | 34 |
37 CoreTabHelper::CoreTabHelper(WebContents* web_contents) | 35 CoreTabHelper::CoreTabHelper(WebContents* web_contents) |
38 : content::WebContentsObserver(web_contents), | 36 : content::WebContentsObserver(web_contents), |
39 delegate_(NULL), | 37 delegate_(NULL), |
40 content_restrictions_(0) { | 38 content_restrictions_(0) { |
41 } | 39 } |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestThumbnailForContextNode_ACK, | 193 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestThumbnailForContextNode_ACK, |
196 OnRequestThumbnailForContextNodeACK) | 194 OnRequestThumbnailForContextNodeACK) |
197 IPC_MESSAGE_UNHANDLED(handled = false) | 195 IPC_MESSAGE_UNHANDLED(handled = false) |
198 IPC_END_MESSAGE_MAP() | 196 IPC_END_MESSAGE_MAP() |
199 return handled; | 197 return handled; |
200 } | 198 } |
201 | 199 |
202 // Handles the image thumbnail for the context node, composes a image search | 200 // Handles the image thumbnail for the context node, composes a image search |
203 // request based on the received thumbnail and opens the request in a new tab. | 201 // request based on the received thumbnail and opens the request in a new tab. |
204 void CoreTabHelper::OnRequestThumbnailForContextNodeACK( | 202 void CoreTabHelper::OnRequestThumbnailForContextNodeACK( |
205 const SkBitmap& bitmap, | 203 const std::string& thumbnail_data, |
206 const gfx::Size& original_size) { | 204 const gfx::Size& original_size) { |
207 if (bitmap.isNull()) | 205 if (thumbnail_data.empty()) |
208 return; | 206 return; |
| 207 |
209 Profile* profile = | 208 Profile* profile = |
210 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 209 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
211 | 210 |
212 TemplateURLService* template_url_service = | 211 TemplateURLService* template_url_service = |
213 TemplateURLServiceFactory::GetForProfile(profile); | 212 TemplateURLServiceFactory::GetForProfile(profile); |
214 if (!template_url_service) | 213 if (!template_url_service) |
215 return; | 214 return; |
216 const TemplateURL* const default_provider = | 215 const TemplateURL* const default_provider = |
217 template_url_service->GetDefaultSearchProvider(); | 216 template_url_service->GetDefaultSearchProvider(); |
218 if (!default_provider) | 217 if (!default_provider) |
219 return; | 218 return; |
220 | 219 |
221 const int kDefaultQualityForImageSearch = 90; | |
222 std::vector<unsigned char> data; | |
223 if (!gfx::JPEGCodec::Encode( | |
224 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
225 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | |
226 static_cast<int>(bitmap.rowBytes()), kDefaultQualityForImageSearch, | |
227 &data)) | |
228 return; | |
229 | |
230 TemplateURLRef::SearchTermsArgs search_args = | 220 TemplateURLRef::SearchTermsArgs search_args = |
231 TemplateURLRef::SearchTermsArgs(base::string16()); | 221 TemplateURLRef::SearchTermsArgs(base::string16()); |
232 search_args.image_thumbnail_content = std::string(data.begin(), data.end()); | 222 search_args.image_thumbnail_content = thumbnail_data; |
233 // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL | 223 // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL |
234 // from the ContextMenuParams which creates current context menu. | 224 // from the ContextMenuParams which creates current context menu. |
235 search_args.image_url = GURL(); | 225 search_args.image_url = GURL(); |
236 search_args.image_original_size = original_size; | 226 search_args.image_original_size = original_size; |
237 TemplateURLRef::PostContent post_content; | 227 TemplateURLRef::PostContent post_content; |
238 GURL result(default_provider->image_url_ref().ReplaceSearchTerms( | 228 GURL result(default_provider->image_url_ref().ReplaceSearchTerms( |
239 search_args, template_url_service->search_terms_data(), &post_content)); | 229 search_args, template_url_service->search_terms_data(), &post_content)); |
240 if (!result.is_valid()) | 230 if (!result.is_valid()) |
241 return; | 231 return; |
242 | 232 |
243 content::OpenURLParams open_url_params( | 233 content::OpenURLParams open_url_params( |
244 result, content::Referrer(), NEW_FOREGROUND_TAB, | 234 result, content::Referrer(), NEW_FOREGROUND_TAB, |
245 ui::PAGE_TRANSITION_LINK, false); | 235 ui::PAGE_TRANSITION_LINK, false); |
246 const std::string& content_type = post_content.first; | 236 const std::string& content_type = post_content.first; |
247 std::string* post_data = &post_content.second; | 237 std::string* post_data = &post_content.second; |
248 if (!post_data->empty()) { | 238 if (!post_data->empty()) { |
249 DCHECK(!content_type.empty()); | 239 DCHECK(!content_type.empty()); |
250 open_url_params.uses_post = true; | 240 open_url_params.uses_post = true; |
251 open_url_params.browser_initiated_post_data = | 241 open_url_params.browser_initiated_post_data = |
252 base::RefCountedString::TakeString(post_data); | 242 base::RefCountedString::TakeString(post_data); |
253 open_url_params.extra_headers += base::StringPrintf( | 243 open_url_params.extra_headers += base::StringPrintf( |
254 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, | 244 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, |
255 content_type.c_str()); | 245 content_type.c_str()); |
256 } | 246 } |
257 web_contents()->OpenURL(open_url_params); | 247 web_contents()->OpenURL(open_url_params); |
258 } | 248 } |
OLD | NEW |