core_tab_helper.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
5#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
6
7#include <string>
8#include <vector>
9
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/renderer_host/web_cache_manager.h"
15#include "chrome/browser/search_engines/search_terms_data.h"
16#include "chrome/browser/search_engines/template_url.h"
17#include "chrome/browser/search_engines/template_url_service.h"
18#include "chrome/browser/search_engines/template_url_service_factory.h"
19#include "chrome/browser/ui/browser.h"
20#include "chrome/browser/ui/browser_command_controller.h"
21#include "chrome/browser/ui/browser_finder.h"
22#include "chrome/common/chrome_switches.h"
23#include "chrome/common/render_messages.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 "grit/generated_resources.h"
28#include "net/base/load_states.h"
29#include "net/http/http_request_headers.h"
30#include "third_party/skia/include/core/SkBitmap.h"
31#include "ui/base/l10n/l10n_util.h"
32#include "ui/gfx/codec/jpeg_codec.h"
33
34using content::WebContents;
35
36DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper);
37
38CoreTabHelper::CoreTabHelper(WebContents* web_contents)
39    : content::WebContentsObserver(web_contents),
40      delegate_(NULL),
41      content_restrictions_(0) {
42}
43
44CoreTabHelper::~CoreTabHelper() {
45}
46
47base::string16 CoreTabHelper::GetDefaultTitle() {
48  return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
49}
50
51base::string16 CoreTabHelper::GetStatusText() const {
52  if (!web_contents()->IsLoading() ||
53      web_contents()->GetLoadState().state == net::LOAD_STATE_IDLE) {
54    return base::string16();
55  }
56
57  switch (web_contents()->GetLoadState().state) {
58    case net::LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL:
59    case net::LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET:
60      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_SOCKET_SLOT);
61    case net::LOAD_STATE_WAITING_FOR_DELEGATE:
62      if (!web_contents()->GetLoadState().param.empty()) {
63        return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_DELEGATE,
64                                          web_contents()->GetLoadState().param);
65      } else {
66        return l10n_util::GetStringUTF16(
67            IDS_LOAD_STATE_WAITING_FOR_DELEGATE_GENERIC);
68      }
69    case net::LOAD_STATE_WAITING_FOR_CACHE:
70      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE);
71    case net::LOAD_STATE_WAITING_FOR_APPCACHE:
72      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_APPCACHE);
73    case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
74      return
75          l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
76    case net::LOAD_STATE_DOWNLOADING_PROXY_SCRIPT:
77      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_DOWNLOADING_PROXY_SCRIPT);
78    case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
79      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
80    case net::LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT:
81      return l10n_util::GetStringUTF16(
82          IDS_LOAD_STATE_RESOLVING_HOST_IN_PROXY_SCRIPT);
83    case net::LOAD_STATE_RESOLVING_HOST:
84      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST);
85    case net::LOAD_STATE_CONNECTING:
86      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING);
87    case net::LOAD_STATE_SSL_HANDSHAKE:
88      return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE);
89    case net::LOAD_STATE_SENDING_REQUEST:
90      if (web_contents()->GetUploadSize()) {
91        return l10n_util::GetStringFUTF16Int(
92            IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
93            static_cast<int>((100 * web_contents()->GetUploadPosition()) /
94                web_contents()->GetUploadSize()));
95      } else {
96        return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST);
97      }
98    case net::LOAD_STATE_WAITING_FOR_RESPONSE:
99      return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
100                                        web_contents()->GetLoadStateHost());
101    // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
102    case net::LOAD_STATE_IDLE:
103    case net::LOAD_STATE_READING_RESPONSE:
104      break;
105  }
106
107  return base::string16();
108}
109
110void CoreTabHelper::OnCloseStarted() {
111  if (close_start_time_.is_null())
112    close_start_time_ = base::TimeTicks::Now();
113}
114
115void CoreTabHelper::OnCloseCanceled() {
116  close_start_time_ = base::TimeTicks();
117  before_unload_end_time_ = base::TimeTicks();
118  unload_detached_start_time_ = base::TimeTicks();
119}
120
121void CoreTabHelper::OnUnloadStarted() {
122  before_unload_end_time_ = base::TimeTicks::Now();
123}
124
125void CoreTabHelper::OnUnloadDetachedStarted() {
126  if (unload_detached_start_time_.is_null())
127    unload_detached_start_time_ = base::TimeTicks::Now();
128}
129
130void CoreTabHelper::UpdateContentRestrictions(int content_restrictions) {
131  content_restrictions_ = content_restrictions;
132#if !defined(OS_ANDROID)
133  Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
134  if (!browser)
135    return;
136
137  browser->command_controller()->ContentRestrictionsChanged();
138#endif
139}
140
141////////////////////////////////////////////////////////////////////////////////
142// WebContentsObserver overrides
143
144void CoreTabHelper::DidStartLoading(content::RenderViewHost* render_view_host) {
145  UpdateContentRestrictions(0);
146}
147
148void CoreTabHelper::WasShown() {
149  WebCacheManager::GetInstance()->ObserveActivity(
150      web_contents()->GetRenderProcessHost()->GetID());
151}
152
153void CoreTabHelper::WebContentsDestroyed() {
154  // OnCloseStarted isn't called in unit tests.
155  if (!close_start_time_.is_null()) {
156    bool fast_tab_close_enabled = CommandLine::ForCurrentProcess()->HasSwitch(
157        switches::kEnableFastUnload);
158
159    if (fast_tab_close_enabled) {
160      base::TimeTicks now = base::TimeTicks::Now();
161      base::TimeDelta close_time = now - close_start_time_;
162      UMA_HISTOGRAM_TIMES("Tab.Close", close_time);
163
164      base::TimeTicks unload_start_time = close_start_time_;
165      base::TimeTicks unload_end_time = now;
166      if (!before_unload_end_time_.is_null())
167        unload_start_time = before_unload_end_time_;
168      if (!unload_detached_start_time_.is_null())
169        unload_end_time = unload_detached_start_time_;
170      base::TimeDelta unload_time = unload_end_time - unload_start_time;
171      UMA_HISTOGRAM_TIMES("Tab.Close.UnloadTime", unload_time);
172    } else {
173      base::TimeTicks now = base::TimeTicks::Now();
174      base::TimeTicks unload_start_time = close_start_time_;
175      if (!before_unload_end_time_.is_null())
176        unload_start_time = before_unload_end_time_;
177      UMA_HISTOGRAM_TIMES("Tab.Close", now - close_start_time_);
178      UMA_HISTOGRAM_TIMES("Tab.Close.UnloadTime", now - unload_start_time);
179    }
180  }
181}
182
183void CoreTabHelper::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
184  before_unload_end_time_ = proceed_time;
185}
186
187void CoreTabHelper::BeforeUnloadDialogCancelled() {
188  OnCloseCanceled();
189}
190
191bool CoreTabHelper::OnMessageReceived(
192    const IPC::Message& message,
193    content::RenderFrameHost* render_frame_host) {
194  bool handled = true;
195  IPC_BEGIN_MESSAGE_MAP(CoreTabHelper, message)
196    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_RequestThumbnailForContextNode_ACK,
197                        OnRequestThumbnailForContextNodeACK)
198    IPC_MESSAGE_UNHANDLED(handled = false)
199  IPC_END_MESSAGE_MAP()
200  return handled;
201}
202
203// Handles the image thumbnail for the context node, composes a image search
204// request based on the received thumbnail and opens the request in a new tab.
205void CoreTabHelper::OnRequestThumbnailForContextNodeACK(
206    const SkBitmap& bitmap,
207    const gfx::Size& original_size) {
208  if (bitmap.isNull())
209    return;
210  Profile* profile =
211      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
212
213  TemplateURLService* template_url_service =
214      TemplateURLServiceFactory::GetForProfile(profile);
215  if (!template_url_service)
216    return;
217  const TemplateURL* const default_provider =
218      template_url_service->GetDefaultSearchProvider();
219  if (!default_provider)
220    return;
221
222  const int kDefaultQualityForImageSearch = 90;
223  std::vector<unsigned char> data;
224  if (!gfx::JPEGCodec::Encode(
225      reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
226      gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(),
227      static_cast<int>(bitmap.rowBytes()), kDefaultQualityForImageSearch,
228      &data))
229    return;
230
231  TemplateURLRef::SearchTermsArgs search_args =
232      TemplateURLRef::SearchTermsArgs(base::string16());
233  search_args.image_thumbnail_content = std::string(data.begin(), data.end());
234  // TODO(jnd): Add a method in WebContentsViewDelegate to get the image URL
235  // from the ContextMenuParams which creates current context menu.
236  search_args.image_url = GURL();
237  search_args.image_original_size = original_size;
238  TemplateURLRef::PostContent post_content;
239  GURL result(default_provider->image_url_ref().ReplaceSearchTerms(
240      search_args, &post_content));
241  if (!result.is_valid())
242    return;
243
244  content::OpenURLParams open_url_params(
245      result, content::Referrer(), NEW_FOREGROUND_TAB,
246      content::PAGE_TRANSITION_LINK, false);
247  const std::string& content_type = post_content.first;
248  std::string* post_data = &post_content.second;
249  if (!post_data->empty()) {
250    DCHECK(!content_type.empty());
251    open_url_params.uses_post = true;
252    open_url_params.browser_initiated_post_data =
253        base::RefCountedString::TakeString(post_data);
254    open_url_params.extra_headers += base::StringPrintf(
255        "%s: %s\r\n", net::HttpRequestHeaders::kContentType,
256        content_type.c_str());
257  }
258  web_contents()->OpenURL(open_url_params);
259}
260