Chromium Code Reviews| 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 thumbnail_capturer->RequestThumbnailForContextNode( | |
| 111 kImageSearchThumbnailMinSize, | 116 kImageSearchThumbnailMinSize, |
| 112 gfx::Size(kImageSearchThumbnailMaxWidth, | 117 gfx::Size(kImageSearchThumbnailMaxWidth, kImageSearchThumbnailMaxHeight), |
| 113 kImageSearchThumbnailMaxHeight), | |
| 114 base::Bind(&CoreTabHelper::DoSearchByImageInNewTab, | 118 base::Bind(&CoreTabHelper::DoSearchByImageInNewTab, |
| 115 base::Unretained(this), | 119 weak_factory_.GetWeakPtr(), base::Passed(&thumbnail_capturer), |
|
tibell
2017/03/09 02:21:49
Ditto
| |
| 116 src_url)); | 120 src_url)); |
| 117 } | 121 } |
| 118 | 122 |
| 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 | 123 // static |
| 136 bool CoreTabHelper::GetStatusTextForWebContents( | 124 bool CoreTabHelper::GetStatusTextForWebContents( |
| 137 base::string16* status_text, content::WebContents* source) { | 125 base::string16* status_text, content::WebContents* source) { |
| 138 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185 is | 126 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/467185 is |
| 139 // fixed. | 127 // fixed. |
| 140 tracked_objects::ScopedTracker tracking_profile1( | 128 tracked_objects::ScopedTracker tracking_profile1( |
| 141 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 129 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 142 "467185 CoreTabHelper::GetStatusTextForWebContents1")); | 130 "467185 CoreTabHelper::GetStatusTextForWebContents1")); |
| 143 auto* guest_manager = guest_view::GuestViewManager::FromBrowserContext( | 131 auto* guest_manager = guest_view::GuestViewManager::FromBrowserContext( |
| 144 source->GetBrowserContext()); | 132 source->GetBrowserContext()); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 } | 282 } |
| 295 | 283 |
| 296 void CoreTabHelper::BeforeUnloadFired(const base::TimeTicks& proceed_time) { | 284 void CoreTabHelper::BeforeUnloadFired(const base::TimeTicks& proceed_time) { |
| 297 before_unload_end_time_ = proceed_time; | 285 before_unload_end_time_ = proceed_time; |
| 298 } | 286 } |
| 299 | 287 |
| 300 void CoreTabHelper::BeforeUnloadDialogCancelled() { | 288 void CoreTabHelper::BeforeUnloadDialogCancelled() { |
| 301 OnCloseCanceled(); | 289 OnCloseCanceled(); |
| 302 } | 290 } |
| 303 | 291 |
| 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 | 292 // 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. | 293 // request based on the received thumbnail and opens the request in a new tab. |
| 330 void CoreTabHelper::DoSearchByImageInNewTab(const GURL& src_url, | 294 void CoreTabHelper::DoSearchByImageInNewTab( |
| 331 const std::string& thumbnail_data, | 295 chrome::mojom::ThumbnailCapturerPtr thumbnail_capturer, |
| 332 const gfx::Size& original_size) { | 296 const GURL& src_url, |
| 297 const std::string& thumbnail_data, | |
| 298 const gfx::Size& original_size) { | |
| 333 if (thumbnail_data.empty()) | 299 if (thumbnail_data.empty()) |
| 334 return; | 300 return; |
| 335 | 301 |
| 336 Profile* profile = | 302 Profile* profile = |
| 337 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 303 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 338 | 304 |
| 339 TemplateURLService* template_url_service = | 305 TemplateURLService* template_url_service = |
| 340 TemplateURLServiceFactory::GetForProfile(profile); | 306 TemplateURLServiceFactory::GetForProfile(profile); |
| 341 if (!template_url_service) | 307 if (!template_url_service) |
| 342 return; | 308 return; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 365 DCHECK(!content_type.empty()); | 331 DCHECK(!content_type.empty()); |
| 366 open_url_params.uses_post = true; | 332 open_url_params.uses_post = true; |
| 367 open_url_params.post_data = content::ResourceRequestBody::CreateFromBytes( | 333 open_url_params.post_data = content::ResourceRequestBody::CreateFromBytes( |
| 368 post_data.data(), post_data.size()); | 334 post_data.data(), post_data.size()); |
| 369 open_url_params.extra_headers += base::StringPrintf( | 335 open_url_params.extra_headers += base::StringPrintf( |
| 370 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, | 336 "%s: %s\r\n", net::HttpRequestHeaders::kContentType, |
| 371 content_type.c_str()); | 337 content_type.c_str()); |
| 372 } | 338 } |
| 373 web_contents()->OpenURL(open_url_params); | 339 web_contents()->OpenURL(open_url_params); |
| 374 } | 340 } |
| OLD | NEW |