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> | |
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 Loading... | |
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 OnGetImageThumbnail) | |
Avi (use Gerrit)
2013/08/01 18:46:09
We name message handlers "OnXXX" where the message
Johnny(Jianning) Ding
2013/08/01 21:46:32
Done.
| |
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 Loading... | |
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 void ChromeRenderViewHostObserver::OnGetImageThumbnail(const SkBitmap& bitmap) { | |
206 const int kDefaultQualityForImageSearch = 90; | |
207 WebContents* web_contents = | |
208 WebContents::FromRenderViewHost(render_view_host()); | |
209 if (!web_contents) | |
210 return; | |
211 | |
212 std::vector<unsigned char> data; | |
213 if (!gfx::JPEGCodec::Encode( | |
214 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | |
215 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), | |
216 static_cast<int>(bitmap.rowBytes()), kDefaultQualityForImageSearch, | |
217 &data)) | |
218 return; | |
219 | |
220 const TemplateURL* const default_provider = | |
221 TemplateURLServiceFactory::GetForProfile(profile_)-> | |
222 GetDefaultSearchProvider(); | |
Avi (use Gerrit)
2013/08/01 18:46:09
indent another 4.
Johnny(Jianning) Ding
2013/08/01 21:46:32
Done.
| |
223 DCHECK(default_provider); | |
224 TemplateURLRef::SearchTermsArgs search_args(ASCIIToUTF16("")); | |
Avi (use Gerrit)
2013/08/01 18:46:09
TemplateURLRef::SearchTermsArgs search_args(base::
Johnny(Jianning) Ding
2013/08/01 21:46:32
Done.
| |
225 search_args.image_thumbnail_content = std::string(data.begin(), | |
226 data.end()); | |
227 // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL | |
228 // from the ContextMenuParams which creates current context menu. | |
229 search_args.image_url = GURL(); | |
230 TemplateURLRef::PostContent post_content; | |
231 GURL result(default_provider->image_url_ref().ReplaceSearchTerms( | |
232 search_args, &post_content)); | |
233 if (!result.is_valid()) | |
234 return; | |
235 | |
236 OpenURLParams open_url_params(result, content::Referrer(), NEW_FOREGROUND_TAB, | |
237 content::PAGE_TRANSITION_LINK, false); | |
238 const std::string& content_type = post_content.first; | |
239 const std::string& post_data = post_content.second; | |
240 if (!post_data.empty()) { | |
241 DCHECK(!content_type.empty()); | |
242 open_url_params.uses_post = true; | |
243 open_url_params.post_data = post_data; | |
244 open_url_params.extra_headers += base::StringPrintf( | |
245 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, | |
246 content_type.c_str()); | |
247 } | |
248 | |
249 web_contents->OpenURL(open_url_params); | |
250 } | |
OLD | NEW |