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

Side by Side Diff: extensions/browser/api/guest_view/web_view/web_view_internal_api.cc

Issue 1582053002: Implement webview.captureVisibleRegion() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix test so it waits for the first frame to be generated. Created 4 years, 11 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" 5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 int instance_id = 0; 252 int instance_id = 0;
253 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); 253 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id));
254 WebViewGuest* guest = WebViewGuest::From( 254 WebViewGuest* guest = WebViewGuest::From(
255 render_frame_host()->GetProcess()->GetID(), instance_id); 255 render_frame_host()->GetProcess()->GetID(), instance_id);
256 if (!guest) 256 if (!guest)
257 return false; 257 return false;
258 258
259 return RunAsyncSafe(guest); 259 return RunAsyncSafe(guest);
260 } 260 }
261 261
262 bool WebViewInternalCaptureVisibleRegionFunction::RunAsyncSafe(
263 WebViewGuest* guest) {
264 using api::extension_types::ImageDetails;
265
266 scoped_ptr<web_view_internal::CaptureVisibleRegion::Params> params(
267 web_view_internal::CaptureVisibleRegion::Params::Create(*args_));
268 EXTENSION_FUNCTION_VALIDATE(params.get());
269
270 scoped_ptr<ImageDetails> image_details;
271 if (args_->GetSize() > 1) {
272 base::Value* spec = NULL;
273 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec);
274 image_details = ImageDetails::FromValue(*spec);
275 }
276
277 return CaptureAsync(guest->web_contents(), image_details.get(),
278 base::Bind(&WebViewInternalCaptureVisibleRegionFunction::
279 CopyFromBackingStoreComplete,
280 this));
281 }
282 bool WebViewInternalCaptureVisibleRegionFunction::IsScreenshotEnabled() {
283 // TODO(wjmaclean): Is it ok to always return true here?
284 return true;
285 }
286
287 void WebViewInternalCaptureVisibleRegionFunction::OnCaptureSuccess(
288 const SkBitmap& bitmap) {
289 std::string base64_result;
290 if (!EncodeBitmap(bitmap, &base64_result)) {
291 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED);
292 return;
293 }
294
295 SetResult(new base::StringValue(base64_result));
296 SendResponse(true);
297 }
298
299 void WebViewInternalCaptureVisibleRegionFunction::OnCaptureFailure(
300 FailureReason reason) {
301 const char* reason_description = "internal error";
302 switch (reason) {
303 case FAILURE_REASON_UNKNOWN:
304 reason_description = "unknown error";
305 break;
306 case FAILURE_REASON_ENCODING_FAILED:
307 reason_description = "encoding failed";
308 break;
309 case FAILURE_REASON_VIEW_INVISIBLE:
310 reason_description = "view is invisible";
311 break;
312 }
313 error_ = ErrorUtils::FormatErrorMessage("Failed to capture webview: *",
314 reason_description);
315 SendResponse(false);
316 }
317
262 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) { 318 bool WebViewInternalNavigateFunction::RunAsyncSafe(WebViewGuest* guest) {
263 scoped_ptr<web_view_internal::Navigate::Params> params( 319 scoped_ptr<web_view_internal::Navigate::Params> params(
264 web_view_internal::Navigate::Params::Create(*args_)); 320 web_view_internal::Navigate::Params::Create(*args_));
265 EXTENSION_FUNCTION_VALIDATE(params.get()); 321 EXTENSION_FUNCTION_VALIDATE(params.get());
266 std::string src = params->src; 322 std::string src = params->src;
267 guest->NavigateGuest(src, true /* force_navigation */); 323 guest->NavigateGuest(src, true /* force_navigation */);
268 return true; 324 return true;
269 } 325 }
270 326
271 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction() 327 WebViewInternalExecuteCodeFunction::WebViewInternalExecuteCodeFunction()
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after
896 // Will finish asynchronously. 952 // Will finish asynchronously.
897 return true; 953 return true;
898 } 954 }
899 955
900 void WebViewInternalClearDataFunction::ClearDataDone() { 956 void WebViewInternalClearDataFunction::ClearDataDone() {
901 Release(); // Balanced in RunAsync(). 957 Release(); // Balanced in RunAsync().
902 SendResponse(true); 958 SendResponse(true);
903 } 959 }
904 960
905 } // namespace extensions 961 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698