Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: chrome/browser/renderer_host/chrome_render_view_host_observer.cc

Issue 21323003: The UI part of search-by-image search. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: address jam's comment Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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>
8
7 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h" 14 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/net/predictor.h" 15 #include "chrome/browser/net/predictor.h"
12 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search_engines/search_terms_data.h"
18 #include "chrome/browser/search_engines/template_url.h"
19 #include "chrome/browser/search_engines/template_url_service.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/extensions/extension.h" 22 #include "chrome/common/extensions/extension.h"
15 #include "chrome/common/extensions/extension_messages.h" 23 #include "chrome/common/extensions/extension_messages.h"
16 #include "chrome/common/extensions/manifest.h" 24 #include "chrome/common/extensions/manifest.h"
17 #include "chrome/common/render_messages.h" 25 #include "chrome/common/render_messages.h"
18 #include "chrome/common/url_constants.h" 26 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/child_process_security_policy.h" 27 #include "content/public/browser/child_process_security_policy.h"
20 #include "content/public/browser/notification_service.h" 28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/page_navigator.h"
21 #include "content/public/browser/render_process_host.h" 30 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h" 31 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/site_instance.h" 32 #include "content/public/browser/site_instance.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/common/page_transition_types.h"
24 #include "extensions/common/constants.h" 35 #include "extensions/common/constants.h"
36 #include "net/http/http_request_headers.h"
37 #include "third_party/skia/include/core/SkBitmap.h"
38 #include "ui/base/window_open_disposition.h"
39 #include "ui/gfx/codec/jpeg_codec.h"
25 40
26 #if defined(OS_WIN) 41 #if defined(OS_WIN)
27 #include "base/win/win_util.h" 42 #include "base/win/win_util.h"
28 #endif // OS_WIN 43 #endif // OS_WIN
29 44
30 using content::ChildProcessSecurityPolicy; 45 using content::ChildProcessSecurityPolicy;
46 using content::OpenURLParams;
31 using content::RenderViewHost; 47 using content::RenderViewHost;
32 using content::SiteInstance; 48 using content::SiteInstance;
49 using content::WebContents;
33 using extensions::Extension; 50 using extensions::Extension;
34 using extensions::Manifest; 51 using extensions::Manifest;
35 52
36 ChromeRenderViewHostObserver::ChromeRenderViewHostObserver( 53 ChromeRenderViewHostObserver::ChromeRenderViewHostObserver(
37 RenderViewHost* render_view_host, chrome_browser_net::Predictor* predictor) 54 RenderViewHost* render_view_host, chrome_browser_net::Predictor* predictor)
38 : content::RenderViewHostObserver(render_view_host), 55 : content::RenderViewHostObserver(render_view_host),
39 predictor_(predictor) { 56 predictor_(predictor) {
40 SiteInstance* site_instance = render_view_host->GetSiteInstance(); 57 SiteInstance* site_instance = render_view_host->GetSiteInstance();
41 profile_ = Profile::FromBrowserContext( 58 profile_ = Profile::FromBrowserContext(
42 site_instance->GetBrowserContext()); 59 site_instance->GetBrowserContext());
(...skipping 25 matching lines...) Expand all
68 (url.SchemeIs(chrome::kHttpScheme) || url.SchemeIs(chrome::kHttpsScheme))) 85 (url.SchemeIs(chrome::kHttpScheme) || url.SchemeIs(chrome::kHttpsScheme)))
69 predictor_->PreconnectUrlAndSubresources(url, GURL()); 86 predictor_->PreconnectUrlAndSubresources(url, GURL());
70 } 87 }
71 88
72 bool ChromeRenderViewHostObserver::OnMessageReceived( 89 bool ChromeRenderViewHostObserver::OnMessageReceived(
73 const IPC::Message& message) { 90 const IPC::Message& message) {
74 bool handled = true; 91 bool handled = true;
75 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewHostObserver, message) 92 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewHostObserver, message)
76 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusedNodeTouched, 93 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusedNodeTouched,
77 OnFocusedNodeTouched) 94 OnFocusedNodeTouched)
95 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestThumbnailForContextNode_ACK,
96 OnRequestThumbnailForContextNodeACK)
78 IPC_MESSAGE_UNHANDLED(handled = false) 97 IPC_MESSAGE_UNHANDLED(handled = false)
79 IPC_END_MESSAGE_MAP() 98 IPC_END_MESSAGE_MAP()
80 return handled; 99 return handled;
81 } 100 }
82 101
83 void ChromeRenderViewHostObserver::InitRenderViewForExtensions() { 102 void ChromeRenderViewHostObserver::InitRenderViewForExtensions() {
84 const Extension* extension = GetExtension(); 103 const Extension* extension = GetExtension();
85 if (!extension) 104 if (!extension)
86 return; 105 return;
87 106
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 content::NotificationService::current()->Notify( 194 content::NotificationService::current()->Notify(
176 chrome::NOTIFICATION_FOCUSED_NODE_TOUCHED, 195 chrome::NOTIFICATION_FOCUSED_NODE_TOUCHED,
177 content::Source<RenderViewHost>(render_view_host()), 196 content::Source<RenderViewHost>(render_view_host()),
178 content::Details<bool>(&editable)); 197 content::Details<bool>(&editable));
179 } else { 198 } else {
180 #if defined(OS_WIN) && defined(USE_AURA) 199 #if defined(OS_WIN) && defined(USE_AURA)
181 base::win::DismissVirtualKeyboard(); 200 base::win::DismissVirtualKeyboard();
182 #endif 201 #endif
183 } 202 }
184 } 203 }
204
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.
207 void ChromeRenderViewHostObserver::OnRequestThumbnailForContextNodeACK(
208 const SkBitmap& bitmap) {
209 const int kDefaultQualityForImageSearch = 90;
210 WebContents* web_contents =
211 WebContents::FromRenderViewHost(render_view_host());
212 if (!web_contents)
213 return;
214
215 std::vector<unsigned char> data;
216 if (!gfx::JPEGCodec::Encode(
217 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
218 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(),
219 static_cast<int>(bitmap.rowBytes()), kDefaultQualityForImageSearch,
220 &data))
221 return;
222
223 const TemplateURL* const default_provider =
224 TemplateURLServiceFactory::GetForProfile(profile_)->
225 GetDefaultSearchProvider();
226 DCHECK(default_provider);
227 TemplateURLRef::SearchTermsArgs search_args =
228 TemplateURLRef::SearchTermsArgs(base::string16());
229 search_args.image_thumbnail_content = std::string(data.begin(),
230 data.end());
231 // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL
232 // from the ContextMenuParams which creates current context menu.
233 search_args.image_url = GURL();
234 TemplateURLRef::PostContent post_content;
235 GURL result(default_provider->image_url_ref().ReplaceSearchTerms(
236 search_args, &post_content));
237 if (!result.is_valid())
238 return;
239
240 OpenURLParams open_url_params(result, content::Referrer(), NEW_FOREGROUND_TAB,
241 content::PAGE_TRANSITION_LINK, false);
242 const std::string& content_type = post_content.first;
243 std::string& post_data = post_content.second;
244 if (!post_data.empty()) {
245 DCHECK(!content_type.empty());
246 open_url_params.uses_post = true;
247 open_url_params.browser_initiated_post_data =
248 base::RefCountedString::TakeString(&post_data);
249 open_url_params.extra_headers += base::StringPrintf(
250 "%s: %s\r\n", net::HttpRequestHeaders::kContentType,
251 content_type.c_str());
252 }
253
254 web_contents->OpenURL(open_url_params);
255 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698