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/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"
32
33using content::WebContents;
34
35DEFINE_WEB_CONTENTS_USER_DATA_KEY(CoreTabHelper);
36
37CoreTabHelper::CoreTabHelper(WebContents* web_contents)
38    : content::WebContentsObserver(web_contents),
39      delegate_(NULL),
40      content_restrictions_(0) {
41}
42
43CoreTabHelper::~CoreTabHelper() {
44}
45
46base::string16 CoreTabHelper::GetDefaultTitle() {
47  return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
48}
49
50base::string16 CoreTabHelper::GetStatusText() const {
51  if (!web_contents()->IsLoading() ||
52      web_contents()->GetLoadState().state == net::LOAD_STATE_IDLE) {
53    return base::string16();
54  }
55
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);
64      } else {
65        return l10n_util::GetStringUTF16(
66            IDS_LOAD_STATE_WAITING_FOR_DELEGATE_GENERIC);
67      }
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:
73      return
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()));
94      } else {
95        return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST);
96      }
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:
103      break;
104  }
105
106  return base::string16();
107}
108
109void CoreTabHelper::OnCloseStarted() {
110  if (close_start_time_.is_null())
111    close_start_time_ = base::TimeTicks::Now();
112}
113
114void CoreTabHelper::OnCloseCanceled() {
115  close_start_time_ = base::TimeTicks();
116  before_unload_end_time_ = base::TimeTicks();
117  unload_detached_start_time_ = base::TimeTicks();
118}
119
120void CoreTabHelper::OnUnloadStarted() {
121  before_unload_end_time_ = base::TimeTicks::Now();
122}
123
124void CoreTabHelper::OnUnloadDetachedStarted() {
125  if (unload_detached_start_time_.is_null())
126    unload_detached_start_time_ = base::TimeTicks::Now();
127}
128
129void CoreTabHelper::UpdateContentRestrictions(int content_restrictions) {
130  content_restrictions_ = content_restrictions;
131#if !defined(OS_ANDROID)
132  Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
133  if (!browser)
134    return;
135
136  browser->command_controller()->ContentRestrictionsChanged();
137#endif
138}
139
140////////////////////////////////////////////////////////////////////////////////
141// WebContentsObserver overrides
142
143void CoreTabHelper::DidStartLoading(content::RenderViewHost* render_view_host) {
144  UpdateContentRestrictions(0);
145}
146
147void CoreTabHelper::WasShown() {
148  web_cache::WebCacheManager::GetInstance()->ObserveActivity(
149      web_contents()->GetRenderProcessHost()->GetID());
150}
151
152void 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);
157
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);
162
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);
171    } else {
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);
178    }
179  }
180}
181
182void CoreTabHelper::BeforeUnloadFired(const base::TimeTicks& proceed_time) {
183  before_unload_end_time_ = proceed_time;
184}
185
186void CoreTabHelper::BeforeUnloadDialogCancelled() {
187  OnCloseCanceled();
188}
189
190bool CoreTabHelper::OnMessageReceived(
191    const IPC::Message& message,
192    content::RenderFrameHost* render_frame_host) {
193  bool handled = true;
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()
199  return handled;
200}
201
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.
204void CoreTabHelper::OnRequestThumbnailForContextNodeACK(
205    const SkBitmap& bitmap,
206    const gfx::Size& original_size) {
207  if (bitmap.isNull())
208    return;
209  Profile* profile =
210      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
211
212  TemplateURLService* template_url_service =
213      TemplateURLServiceFactory::GetForProfile(profile);
214  if (!template_url_service)
215    return;
216  const TemplateURL* const default_provider =
217      template_url_service->GetDefaultSearchProvider();
218  if (!default_provider)
219    return;
220
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,
227      &data))
228    return;
229
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())
241    return;
242
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());
256  }
257  web_contents()->OpenURL(open_url_params);
258}
259