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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
10 #include "base/command_line.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_command_controller.h"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/render_messages.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/search_engines/template_url.h"
22 #include "components/search_engines/template_url_service.h"
23 #include "components/web_cache/browser/web_cache_manager.h"
24 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/browser/web_contents.h"
27 #include "net/base/load_states.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"
31 #include "ui/gfx/codec/jpeg_codec.h"
33 using content::WebContents;
35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper);
37 CoreTabHelper::CoreTabHelper(WebContents* web_contents)
38 : content::WebContentsObserver(web_contents),
40 content_restrictions_(0) {
43 CoreTabHelper::~CoreTabHelper() {
46 base::string16 CoreTabHelper::GetDefaultTitle() {
47 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
50 base::string16 CoreTabHelper::GetStatusText() const {
51 if (!web_contents()->IsLoading() ||
52 web_contents()->GetLoadState().state == net::LOAD_STATE_IDLE) {
53 return base::string16();
56 switch (web_contents()->GetLoadState().state) {
57 case net::LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL:
58 case net::LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET:
59 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_SOCKET_SLOT);
60 case net::LOAD_STATE_WAITING_FOR_DELEGATE:
61 if (!web_contents()->GetLoadState().param.empty()) {
62 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_DELEGATE,
63 web_contents()->GetLoadState().param);
65 return l10n_util::GetStringUTF16(
66 IDS_LOAD_STATE_WAITING_FOR_DELEGATE_GENERIC);
68 case net::LOAD_STATE_WAITING_FOR_CACHE:
69 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE);
70 case net::LOAD_STATE_WAITING_FOR_APPCACHE:
71 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_APPCACHE);
72 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
74 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
75 case net::LOAD_STATE_DOWNLOADING_PROXY_SCRIPT:
76 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_DOWNLOADING_PROXY_SCRIPT);
77 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
78 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
79 case net::LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT:
80 return l10n_util::GetStringUTF16(
81 IDS_LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT);
82 case net::LOAD_STATE_RESOLVING_HOST:
83 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST);
84 case net::LOAD_STATE_CONNECTING:
85 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING);
86 case net::LOAD_STATE_SSL_HANDSHAKE:
87 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE);
88 case net::LOAD_STATE_SENDING_REQUEST:
89 if (web_contents()->GetUploadSize()) {
90 return l10n_util::GetStringFUTF16Int(
91 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
92 static_cast<int>((100 * web_contents()->GetUploadPosition()) /
93 web_contents()->GetUploadSize()));
95 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST);
97 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
98 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
99 web_contents()->GetLoadStateHost());
100 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
101 case net::LOAD_STATE_IDLE:
102 case net::LOAD_STATE_READING_RESPONSE:
106 return base::string16();
109 void CoreTabHelper::OnCloseStarted() {
110 if (close_start_time_.is_null())
111 close_start_time_ = base::TimeTicks::Now();
114 void CoreTabHelper::OnCloseCanceled() {
115 close_start_time_ = base::TimeTicks();
116 before_unload_end_time_ = base::TimeTicks();
117 unload_detached_start_time_ = base::TimeTicks();
120 void CoreTabHelper::OnUnloadStarted() {
121 before_unload_end_time_ = base::TimeTicks::Now();
124 void CoreTabHelper::OnUnloadDetachedStarted() {
125 if (unload_detached_start_time_.is_null())
126 unload_detached_start_time_ = base::TimeTicks::Now();
129 void CoreTabHelper::UpdateContentRestrictions(int content_restrictions) {
130 content_restrictions_ = content_restrictions;
131 #if !defined(OS_ANDROID)
132 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
136 browser->command_controller()->ContentRestrictionsChanged();
140 ////////////////////////////////////////////////////////////////////////////////
141 // WebContentsObserver overrides
143 void CoreTabHelper::DidStartLoading(content::RenderViewHost* render_view_host) {
144 UpdateContentRestrictions(0);
147 void CoreTabHelper::WasShown() {
148 web_cache::WebCacheManager::GetInstance()->ObserveActivity(
149 web_contents()->GetRenderProcessHost()->GetID());
152 void CoreTabHelper::WebContentsDestroyed() {
153 // OnCloseStarted isn't called in unit tests.
154 if (!close_start_time_.is_null()) {
155 bool fast_tab_close_enabled = CommandLine::ForCurrentProcess()->HasSwitch(
156 switches::kEnableFastUnload);
158 if (fast_tab_close_enabled) {
159 base::TimeTicks now = base::TimeTicks::Now();
160 base::TimeDelta close_time = now - close_start_time_;
161 UMA_HISTOGRAM_TIMES("Tab.Close", close_time);
163 base::TimeTicks unload_start_time = close_start_time_;
164 base::TimeTicks unload_end_time = now;
165 if (!before_unload_end_time_.is_null())
166 unload_start_time = before_unload_end_time_;
167 if (!unload_detached_start_time_.is_null())
168 unload_end_time = unload_detached_start_time_;
169 base::TimeDelta unload_time = unload_end_time - unload_start_time;
170 UMA_HISTOGRAM_TIMES("Tab.Close.UnloadTime", unload_time);
172 base::TimeTicks now = base::TimeTicks::Now();
173 base::TimeTicks unload_start_time = close_start_time_;
174 if (!before_unload_end_time_.is_null())
175 unload_start_time = before_unload_end_time_;
176 UMA_HISTOGRAM_TIMES("Tab.Close", now - close_start_time_);
177 UMA_HISTOGRAM_TIMES("Tab.Close.UnloadTime", now - unload_start_time);
182 void CoreTabHelper::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
183 before_unload_end_time_ = proceed_time;
186 void CoreTabHelper::BeforeUnloadDialogCancelled() {
190 bool CoreTabHelper::OnMessageReceived(
191 const IPC::Message& message,
192 content::RenderFrameHost* render_frame_host) {
194 IPC_BEGIN_MESSAGE_MAP(CoreTabHelper, message)
195 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestThumbnailForContextNode_ACK,
196 OnRequestThumbnailForContextNodeACK)
197 IPC_MESSAGE_UNHANDLED(handled = false)
198 IPC_END_MESSAGE_MAP()
202 // 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.
204 void CoreTabHelper::OnRequestThumbnailForContextNodeACK(
205 const SkBitmap& bitmap,
206 const gfx::Size& original_size) {
210 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
212 TemplateURLService* template_url_service =
213 TemplateURLServiceFactory::GetForProfile(profile);
214 if (!template_url_service)
216 const TemplateURL* const default_provider =
217 template_url_service->GetDefaultSearchProvider();
218 if (!default_provider)
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,
230 TemplateURLRef::SearchTermsArgs search_args =
231 TemplateURLRef::SearchTermsArgs(base::string16());
232 search_args.image_thumbnail_content = std::string(data.begin(), data.end());
233 // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL
234 // from the ContextMenuParams which creates current context menu.
235 search_args.image_url = GURL();
236 search_args.image_original_size = original_size;
237 TemplateURLRef::PostContent post_content;
238 GURL result(default_provider->image_url_ref().ReplaceSearchTerms(
239 search_args, template_url_service->search_terms_data(), &post_content));
240 if (!result.is_valid())
243 content::OpenURLParams open_url_params(
244 result, content::Referrer(), NEW_FOREGROUND_TAB,
245 ui::PAGE_TRANSITION_LINK, false);
246 const std::string& content_type = post_content.first;
247 std::string* post_data = &post_content.second;
248 if (!post_data->empty()) {
249 DCHECK(!content_type.empty());
250 open_url_params.uses_post = true;
251 open_url_params.browser_initiated_post_data =
252 base::RefCountedString::TakeString(post_data);
253 open_url_params.extra_headers += base::StringPrintf(
254 "%s: %s\r\n", net::HttpRequestHeaders::kContentType,
255 content_type.c_str());
257 web_contents()->OpenURL(open_url_params);