Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: extensions/browser/api/capture_web_contents_function_impl.h

Issue 545973002: Cleaning up capture_web_contents_function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/browser/api/capture_web_contents_function.cc ('k') | extensions/extensions.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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 #ifndef EXTENSIONS_BROWSER_API_CAPTURE_WEB_CONTENTS_FUNCTION_IMPL_H_
6 #define EXTENSIONS_BROWSER_API_CAPTURE_WEB_CONTENTS_FUNCTION_IMPL_H_
7
8 #include "extensions/browser/api/capture_web_contents_function.h"
9
10 #include "base/base64.h"
11 #include "base/strings/stringprintf.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "content/public/browser/web_contents.h"
15 #include "extensions/browser/extension_function.h"
16 #include "extensions/common/constants.h"
17 #include "ui/gfx/codec/jpeg_codec.h"
18 #include "ui/gfx/codec/png_codec.h"
19
20 using content::RenderViewHost;
21 using content::RenderWidgetHost;
22 using content::RenderWidgetHostView;
23 using content::WebContents;
24
25 namespace extensions {
26
27 template <typename T>
28 bool CaptureWebContentsFunction<T>::HasPermission() {
29 return true;
30 }
31
32 template <typename T>
33 bool CaptureWebContentsFunction<T>::RunAsync() {
34 EXTENSION_FUNCTION_VALIDATE(T::args_);
35
36 context_id_ = extension_misc::kCurrentWindowId;
37 T::args_->GetInteger(0, &context_id_);
38
39 scoped_ptr<ImageDetails> image_details;
40 if (T::args_->GetSize() > 1) {
41 base::Value* spec = NULL;
42 EXTENSION_FUNCTION_VALIDATE(T::args_->Get(1, &spec) && spec);
43 image_details = ImageDetails::FromValue(*spec);
44 }
45
46 if (!IsScreenshotEnabled())
47 return false;
48
49 WebContents* contents = GetWebContentsForID(context_id_);
50 if (!contents)
51 return false;
52
53 // The default format and quality setting used when encoding jpegs.
54 const ImageDetails::Format kDefaultFormat = ImageDetails::FORMAT_JPEG;
55 const int kDefaultQuality = 90;
56
57 image_format_ = kDefaultFormat;
58 image_quality_ = kDefaultQuality;
59
60 if (image_details) {
61 if (image_details->format != ImageDetails::FORMAT_NONE)
62 image_format_ = image_details->format;
63 if (image_details->quality.get())
64 image_quality_ = *image_details->quality;
65 }
66
67 RenderViewHost* render_view_host = contents->GetRenderViewHost();
68 RenderWidgetHostView* view = render_view_host->GetView();
69 if (!view) {
70 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE);
71 return false;
72 }
73 render_view_host->CopyFromBackingStore(
74 gfx::Rect(),
75 view->GetViewBounds().size(),
76 base::Bind(&CaptureWebContentsFunction::CopyFromBackingStoreComplete,
77 this),
78 kN32_SkColorType);
79 return true;
80 }
81
82 template <typename T>
83 void CaptureWebContentsFunction<T>::CopyFromBackingStoreComplete(
84 bool succeeded,
85 const SkBitmap& bitmap) {
86 if (succeeded) {
87 OnCaptureSuccess(bitmap);
88 return;
89 }
90 OnCaptureFailure(FAILURE_REASON_UNKNOWN);
91 }
92
93 template <typename T>
94 void CaptureWebContentsFunction<T>::OnCaptureSuccess(const SkBitmap& bitmap) {
95 std::vector<unsigned char> data;
96 SkAutoLockPixels screen_capture_lock(bitmap);
97 bool encoded = false;
98 std::string mime_type;
99 switch (image_format_) {
100 case ImageDetails::FORMAT_JPEG:
101 encoded = gfx::JPEGCodec::Encode(
102 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)),
103 gfx::JPEGCodec::FORMAT_SkBitmap,
104 bitmap.width(),
105 bitmap.height(),
106 static_cast<int>(bitmap.rowBytes()),
107 image_quality_,
108 &data);
109 mime_type = kMimeTypeJpeg;
110 break;
111 case ImageDetails::FORMAT_PNG:
112 encoded =
113 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap,
114 true, // Discard transparency.
115 &data);
116 mime_type = kMimeTypePng;
117 break;
118 default:
119 NOTREACHED() << "Invalid image format.";
120 }
121
122 if (!encoded) {
123 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED);
124 return;
125 }
126
127 std::string base64_result;
128 base::StringPiece stream_as_string(
129 reinterpret_cast<const char*>(vector_as_array(&data)), data.size());
130
131 base::Base64Encode(stream_as_string, &base64_result);
132 base64_result.insert(
133 0, base::StringPrintf("data:%s;base64,", mime_type.c_str()));
134 T::SetResult(new base::StringValue(base64_result));
135 T::SendResponse(true);
136 }
137
138 template <typename T>
139 bool CaptureWebContentsFunction<T>::ValidationFailure(
140 CaptureWebContentsFunction<T>* function) {
141 return T::ValidationFailure(function);
142 }
143
144 } // namespace extensions
145
146 #endif // EXTENSIONS_BROWSER_API_CAPTURE_WEB_CONTENTS_FUNCTION_IMPL_H_
OLDNEW
« no previous file with comments | « extensions/browser/api/capture_web_contents_function.cc ('k') | extensions/extensions.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698