summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/safe_browsing/phishing_thumbnailer.cc
blob: e8a3b4fcc1f432e8700d4cfb99d9058ed4b5036e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/safe_browsing/phishing_thumbnailer.h"

#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/time.h"
#include "content/renderer/render_view.h"
#include "skia/ext/image_operations.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
#include "ui/gfx/size.h"
#include "webkit/glue/webkit_glue.h"

using WebKit::WebRect;
using WebKit::WebSize;
using WebKit::WebView;

namespace safe_browsing {

SkBitmap GrabPhishingThumbnail(RenderView* render_view,
                               const gfx::Size& view_size,
                               const gfx::Size& thumbnail_size) {
  if (!render_view || !render_view->webview()) {
    return SkBitmap();  // The WebView is going away.
  }
  WebView* view = render_view->webview();
  base::TimeTicks beginning_time = base::TimeTicks::Now();
  skia::PlatformCanvas canvas;
  if (!canvas.initialize(view_size.width(), view_size.height(), true)) {
    return SkBitmap();
  }

  // Make sure we are not using any zoom when we take the snapshot.  We will
  // restore the previous zoom level after the snapshot is taken.
  double old_zoom_level = view->zoomLevel();
  if (view->zoomLevel() != 0.0) {
    view->setZoomLevel(false, 0.0);
  }
  WebSize old_size = view->size();
  // TODO(noelutz): not only should we hide all scroll bars but we should also
  // make sure that all scroll-bars are at the top.
  view->mainFrame()->setCanHaveScrollbars(false);  // always hide scrollbars.
  view->resize(view_size);
  view->layout();
  view->paint(webkit_glue::ToWebCanvas(&canvas),
              WebRect(0, 0, view_size.width(), view_size.height()));

  SkDevice* device = skia::GetTopDevice(canvas);

  // Now resize the thumbnail to the right size.  Note: it is important that we
  // use this resize algorithm here.
  const SkBitmap& bitmap = device->accessBitmap(false);
  SkBitmap thumbnail = skia::ImageOperations::Resize(
      bitmap,
      skia::ImageOperations::RESIZE_LANCZOS3,
      thumbnail_size.width(),
      thumbnail_size.height());

  // Put things back as they were before.
  if (view->zoomLevel() != old_zoom_level) {
    view->setZoomLevel(false, old_zoom_level);
  }
  // Maybe re-display the scrollbars and resize the view to its old size.
  view->mainFrame()->setCanHaveScrollbars(
      render_view->should_display_scrollbars(old_size.width, old_size.height));
  view->resize(old_size);

  UMA_HISTOGRAM_TIMES("SBClientPhishing.GrabPhishingThumbnail",
                      base::TimeTicks::Now() - beginning_time);
  return thumbnail;
}

}  // namespace safe_browsing