| 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/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/profiler/scoped_tracker.h" | 14 #include "base/profiler/scoped_tracker.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 19 #include "chrome/browser/ui/browser_command_controller.h" | 19 #include "chrome/browser/ui/browser_command_controller.h" |
| 20 #include "chrome/browser/ui/browser_finder.h" | 20 #include "chrome/browser/ui/browser_finder.h" |
| 21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/render_messages.h" | 22 #include "chrome/common/render_messages.h" |
| 23 #include "chrome/common/thumbnail_capturer.mojom.h" |
| 23 #include "chrome/grit/generated_resources.h" | 24 #include "chrome/grit/generated_resources.h" |
| 24 #include "components/guest_view/browser/guest_view_manager.h" | 25 #include "components/guest_view/browser/guest_view_manager.h" |
| 25 #include "components/search_engines/template_url.h" | 26 #include "components/search_engines/template_url.h" |
| 26 #include "components/search_engines/template_url_service.h" | 27 #include "components/search_engines/template_url_service.h" |
| 27 #include "components/strings/grit/components_strings.h" | 28 #include "components/strings/grit/components_strings.h" |
| 28 #include "components/web_cache/browser/web_cache_manager.h" | 29 #include "components/web_cache/browser/web_cache_manager.h" |
| 29 #include "content/public/browser/navigation_controller.h" | 30 #include "content/public/browser/navigation_controller.h" |
| 30 #include "content/public/browser/navigation_entry.h" | 31 #include "content/public/browser/navigation_entry.h" |
| 31 #include "content/public/browser/render_frame_host.h" | 32 #include "content/public/browser/render_frame_host.h" |
| 32 #include "content/public/browser/render_process_host.h" | 33 #include "content/public/browser/render_process_host.h" |
| 33 #include "content/public/browser/render_view_host.h" | 34 #include "content/public/browser/render_view_host.h" |
| 34 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 35 #include "content/public/common/content_switches.h" | 36 #include "content/public/common/content_switches.h" |
| 36 #include "content/public/common/context_menu_params.h" | 37 #include "content/public/common/context_menu_params.h" |
| 37 #include "net/base/load_states.h" | 38 #include "net/base/load_states.h" |
| 38 #include "net/http/http_request_headers.h" | 39 #include "net/http/http_request_headers.h" |
| 40 #include "services/service_manager/public/cpp/interface_provider.h" |
| 39 #include "ui/base/l10n/l10n_util.h" | 41 #include "ui/base/l10n/l10n_util.h" |
| 40 | 42 |
| 41 #if !defined(OS_ANDROID) | 43 #if !defined(OS_ANDROID) |
| 42 #include "chrome/browser/ui/browser.h" | 44 #include "chrome/browser/ui/browser.h" |
| 43 #endif | 45 #endif |
| 44 | 46 |
| 45 using content::WebContents; | 47 using content::WebContents; |
| 46 | 48 |
| 47 namespace { | 49 namespace { |
| 48 | 50 |
| 49 const int kImageSearchThumbnailMinSize = 300 * 300; | 51 const int kImageSearchThumbnailMinSize = 300 * 300; |
| 50 const int kImageSearchThumbnailMaxWidth = 600; | 52 const int kImageSearchThumbnailMaxWidth = 600; |
| 51 const int kImageSearchThumbnailMaxHeight = 600; | 53 const int kImageSearchThumbnailMaxHeight = 600; |
| 52 | 54 |
| 53 } // namespace | 55 } // namespace |
| 54 | 56 |
| 55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper); | 57 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper); |
| 56 | 58 |
| 57 CoreTabHelper::CoreTabHelper(WebContents* web_contents) | 59 CoreTabHelper::CoreTabHelper(WebContents* web_contents) |
| 58 : content::WebContentsObserver(web_contents), | 60 : content::WebContentsObserver(web_contents), |
| 59 delegate_(NULL), | 61 delegate_(NULL), |
| 60 content_restrictions_(0) { | 62 content_restrictions_(0), |
| 61 } | 63 weak_factory_(this) {} |
| 62 | 64 |
| 63 CoreTabHelper::~CoreTabHelper() { | 65 CoreTabHelper::~CoreTabHelper() {} |
| 64 } | |
| 65 | 66 |
| 66 base::string16 CoreTabHelper::GetDefaultTitle() { | 67 base::string16 CoreTabHelper::GetDefaultTitle() { |
| 67 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); | 68 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE); |
| 68 } | 69 } |
| 69 | 70 |
| 70 base::string16 CoreTabHelper::GetStatusText() const { | 71 base::string16 CoreTabHelper::GetStatusText() const { |
| 71 base::string16 status_text; | 72 base::string16 status_text; |
| 72 GetStatusTextForWebContents(&status_text, web_contents()); | 73 GetStatusTextForWebContents(&status_text, web_contents()); |
| 73 return status_text; | 74 return status_text; |
| 74 } | 75 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 98 #if !defined(OS_ANDROID) | 99 #if !defined(OS_ANDROID) |
| 99 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 100 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
| 100 if (!browser) | 101 if (!browser) |
| 101 return; | 102 return; |
| 102 | 103 |
| 103 browser->command_controller()->ContentRestrictionsChanged(); | 104 browser->command_controller()->ContentRestrictionsChanged(); |
| 104 #endif | 105 #endif |
| 105 } | 106 } |
| 106 | 107 |
| 107 void CoreTabHelper::SearchByImageInNewTab( | 108 void CoreTabHelper::SearchByImageInNewTab( |
| 108 content::RenderFrameHost* render_frame_host, const GURL& src_url) { | 109 content::RenderFrameHost* render_frame_host, |
| 109 RequestThumbnailForContextNode( | 110 const GURL& src_url) { |
| 110 render_frame_host, | 111 chrome::mojom::ThumbnailCapturerPtr thumbnail_capturer; |
| 112 render_frame_host->GetRemoteInterfaces()->GetInterface(&thumbnail_capturer); |
| 113 // Bind the InterfacePtr into the callback so that it's kept alive until |
| 114 // there's either a connection error or a response. |
| 115 auto* thumbnail_capturer_proxy = thumbnail_capturer.get(); |
| 116 thumbnail_capturer_proxy->RequestThumbnailForContextNode( |
| 111 kImageSearchThumbnailMinSize, | 117 kImageSearchThumbnailMinSize, |
| 112 gfx::Size(kImageSearchThumbnailMaxWidth, | 118 gfx::Size(kImageSearchThumbnailMaxWidth, kImageSearchThumbnailMaxHeight), |
| 113 kImageSearchThumbnailMaxHeight), | |
| 114 base::Bind(&CoreTabHelper::DoSearchByImageInNewTab, | 119 base::Bind(&CoreTabHelper::DoSearchByImageInNewTab, |
| 115 base::Unretained(this), | 120 weak_factory_.GetWeakPtr(), base::Passed(&thumbnail_capturer), |
| 116 src_url)); | 121 src_url)); |
| 117 } | 122 } |
| 118 | 123 |
| 119 void CoreTabHelper::RequestThumbnailForContextNode( | |
| 120 content::RenderFrameHost* render_frame_host, | |
| 121 int minimum_size, | |
| 122 gfx::Size maximum_size, | |
| 123 const ContextNodeThumbnailCallback& callback) { | |
| 124 int callback_id = thumbnail_callbacks_.Add( | |
| 125 base::MakeUnique<ContextNodeThumbnailCallback>(callback)); | |
| 126 | |
| 127 render_frame_host->Send( | |
| 128 new ChromeViewMsg_RequestThumbnailForContextNode( | |
| 129 render_frame_host->GetRoutingID(), | |
| 130 minimum_size, | |
| 131 maximum_size, | |
| 132 callback_id)); | |
| 133 } | |
| 134 | |
| 135 // static | 124 // static |
| 136 bool CoreTabHelper::GetStatusTextForWebContents( | 125 bool CoreTabHelper::GetStatusTextForWebContents( |
| 137 base::string16* status_text, content::WebContents* source) { | 126 base::string16* status_text, content::WebContents* source) { |
| 138 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185 is | 127 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185 is |
| 139 // fixed. | 128 // fixed. |
| 140 tracked_objects::ScopedTracker tracking_profile1( | 129 tracked_objects::ScopedTracker tracking_profile1( |
| 141 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 130 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 142 "467185 CoreTabHelper::GetStatusTextForWebContents1")); | 131 "467185 CoreTabHelper::GetStatusTextForWebContents1")); |
| 143 auto* guest_manager = guest_view::GuestViewManager::FromBrowserContext( | 132 auto* guest_manager = guest_view::GuestViewManager::FromBrowserContext( |
| 144 source->GetBrowserContext()); | 133 source->GetBrowserContext()); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 } | 283 } |
| 295 | 284 |
| 296 void CoreTabHelper::BeforeUnloadFired(const base::TimeTicks& proceed_time) { | 285 void CoreTabHelper::BeforeUnloadFired(const base::TimeTicks& proceed_time) { |
| 297 before_unload_end_time_ = proceed_time; | 286 before_unload_end_time_ = proceed_time; |
| 298 } | 287 } |
| 299 | 288 |
| 300 void CoreTabHelper::BeforeUnloadDialogCancelled() { | 289 void CoreTabHelper::BeforeUnloadDialogCancelled() { |
| 301 OnCloseCanceled(); | 290 OnCloseCanceled(); |
| 302 } | 291 } |
| 303 | 292 |
| 304 bool CoreTabHelper::OnMessageReceived( | |
| 305 const IPC::Message& message, | |
| 306 content::RenderFrameHost* render_frame_host) { | |
| 307 bool handled = true; | |
| 308 IPC_BEGIN_MESSAGE_MAP(CoreTabHelper, message) | |
| 309 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestThumbnailForContextNode_ACK, | |
| 310 OnRequestThumbnailForContextNodeACK) | |
| 311 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 312 IPC_END_MESSAGE_MAP() | |
| 313 return handled; | |
| 314 } | |
| 315 | |
| 316 void CoreTabHelper::OnRequestThumbnailForContextNodeACK( | |
| 317 const std::string& thumbnail_data, | |
| 318 const gfx::Size& original_size, | |
| 319 int callback_id) { | |
| 320 ContextNodeThumbnailCallback* callback = | |
| 321 thumbnail_callbacks_.Lookup(callback_id); | |
| 322 if (!callback) | |
| 323 return; | |
| 324 callback->Run(thumbnail_data, original_size); | |
| 325 thumbnail_callbacks_.Remove(callback_id); | |
| 326 } | |
| 327 | |
| 328 // Handles the image thumbnail for the context node, composes a image search | 293 // Handles the image thumbnail for the context node, composes a image search |
| 329 // request based on the received thumbnail and opens the request in a new tab. | 294 // request based on the received thumbnail and opens the request in a new tab. |
| 330 void CoreTabHelper::DoSearchByImageInNewTab(const GURL& src_url, | 295 void CoreTabHelper::DoSearchByImageInNewTab( |
| 331 const std::string& thumbnail_data, | 296 chrome::mojom::ThumbnailCapturerPtr thumbnail_capturer, |
| 332 const gfx::Size& original_size) { | 297 const GURL& src_url, |
| 298 const std::vector<uint8_t>& thumbnail_data, |
| 299 const gfx::Size& original_size) { |
| 333 if (thumbnail_data.empty()) | 300 if (thumbnail_data.empty()) |
| 334 return; | 301 return; |
| 335 | 302 |
| 336 Profile* profile = | 303 Profile* profile = |
| 337 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 304 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 338 | 305 |
| 339 TemplateURLService* template_url_service = | 306 TemplateURLService* template_url_service = |
| 340 TemplateURLServiceFactory::GetForProfile(profile); | 307 TemplateURLServiceFactory::GetForProfile(profile); |
| 341 if (!template_url_service) | 308 if (!template_url_service) |
| 342 return; | 309 return; |
| 343 const TemplateURL* const default_provider = | 310 const TemplateURL* const default_provider = |
| 344 template_url_service->GetDefaultSearchProvider(); | 311 template_url_service->GetDefaultSearchProvider(); |
| 345 if (!default_provider) | 312 if (!default_provider) |
| 346 return; | 313 return; |
| 347 | 314 |
| 348 TemplateURLRef::SearchTermsArgs search_args = | 315 TemplateURLRef::SearchTermsArgs search_args = |
| 349 TemplateURLRef::SearchTermsArgs(base::string16()); | 316 TemplateURLRef::SearchTermsArgs(base::string16()); |
| 350 search_args.image_thumbnail_content = thumbnail_data; | 317 search_args.image_thumbnail_content.assign(thumbnail_data.begin(), |
| 318 thumbnail_data.end()); |
| 351 search_args.image_url = src_url; | 319 search_args.image_url = src_url; |
| 352 search_args.image_original_size = original_size; | 320 search_args.image_original_size = original_size; |
| 353 TemplateURLRef::PostContent post_content; | 321 TemplateURLRef::PostContent post_content; |
| 354 GURL result(default_provider->image_url_ref().ReplaceSearchTerms( | 322 GURL result(default_provider->image_url_ref().ReplaceSearchTerms( |
| 355 search_args, template_url_service->search_terms_data(), &post_content)); | 323 search_args, template_url_service->search_terms_data(), &post_content)); |
| 356 if (!result.is_valid()) | 324 if (!result.is_valid()) |
| 357 return; | 325 return; |
| 358 | 326 |
| 359 content::OpenURLParams open_url_params( | 327 content::OpenURLParams open_url_params( |
| 360 result, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB, | 328 result, content::Referrer(), WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 361 ui::PAGE_TRANSITION_LINK, false); | 329 ui::PAGE_TRANSITION_LINK, false); |
| 362 const std::string& content_type = post_content.first; | 330 const std::string& content_type = post_content.first; |
| 363 const std::string& post_data = post_content.second; | 331 const std::string& post_data = post_content.second; |
| 364 if (!post_data.empty()) { | 332 if (!post_data.empty()) { |
| 365 DCHECK(!content_type.empty()); | 333 DCHECK(!content_type.empty()); |
| 366 open_url_params.uses_post = true; | 334 open_url_params.uses_post = true; |
| 367 open_url_params.post_data = content::ResourceRequestBody::CreateFromBytes( | 335 open_url_params.post_data = content::ResourceRequestBody::CreateFromBytes( |
| 368 post_data.data(), post_data.size()); | 336 post_data.data(), post_data.size()); |
| 369 open_url_params.extra_headers += base::StringPrintf( | 337 open_url_params.extra_headers += base::StringPrintf( |
| 370 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, | 338 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, |
| 371 content_type.c_str()); | 339 content_type.c_str()); |
| 372 } | 340 } |
| 373 web_contents()->OpenURL(open_url_params); | 341 web_contents()->OpenURL(open_url_params); |
| 374 } | 342 } |
| OLD | NEW |