| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/capture_web_contents_function.h" | 5 #include "chrome/browser/extensions/api/capture_web_contents_function.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 9 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 10 #include "content/public/browser/render_view_host.h" | 10 #include "content/public/browser/render_view_host.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 OnCaptureSuccess(bitmap); | 88 OnCaptureSuccess(bitmap); |
| 89 return; | 89 return; |
| 90 } | 90 } |
| 91 | 91 |
| 92 WebContents* contents = GetWebContentsForID(context_id_); | 92 WebContents* contents = GetWebContentsForID(context_id_); |
| 93 if (!contents) { | 93 if (!contents) { |
| 94 OnCaptureFailure(FAILURE_REASON_CONTENT_NOT_FOUND); | 94 OnCaptureFailure(FAILURE_REASON_CONTENT_NOT_FOUND); |
| 95 return; | 95 return; |
| 96 } | 96 } |
| 97 | 97 |
| 98 // Ask the renderer for a snapshot of the page. | 98 OnCaptureFailure(FAILURE_REASON_UNKNOWN); |
| 99 RenderWidgetHost* render_widget_host = contents->GetRenderViewHost(); | |
| 100 render_widget_host->GetSnapshotFromRenderer( | |
| 101 gfx::Rect(), | |
| 102 base::Bind(&CaptureWebContentsFunction::GetSnapshotFromRendererComplete, | |
| 103 this)); | |
| 104 } | |
| 105 | |
| 106 void CaptureWebContentsFunction::GetSnapshotFromRendererComplete( | |
| 107 bool succeeded, | |
| 108 const SkBitmap& bitmap) { | |
| 109 if (succeeded) | |
| 110 OnCaptureSuccess(bitmap); | |
| 111 else | |
| 112 OnCaptureFailure(FAILURE_REASON_UNKNOWN); | |
| 113 } | 99 } |
| 114 | 100 |
| 115 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { | 101 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { |
| 116 std::vector<unsigned char> data; | 102 std::vector<unsigned char> data; |
| 117 SkAutoLockPixels screen_capture_lock(bitmap); | 103 SkAutoLockPixels screen_capture_lock(bitmap); |
| 118 bool encoded = false; | 104 bool encoded = false; |
| 119 std::string mime_type; | 105 std::string mime_type; |
| 120 switch (image_format_) { | 106 switch (image_format_) { |
| 121 case ImageDetails::FORMAT_JPEG: | 107 case ImageDetails::FORMAT_JPEG: |
| 122 encoded = gfx::JPEGCodec::Encode( | 108 encoded = gfx::JPEGCodec::Encode( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 150 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | 136 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
| 151 | 137 |
| 152 base::Base64Encode(stream_as_string, &base64_result); | 138 base::Base64Encode(stream_as_string, &base64_result); |
| 153 base64_result.insert(0, base::StringPrintf("data:%s;base64,", | 139 base64_result.insert(0, base::StringPrintf("data:%s;base64,", |
| 154 mime_type.c_str())); | 140 mime_type.c_str())); |
| 155 SetResult(new base::StringValue(base64_result)); | 141 SetResult(new base::StringValue(base64_result)); |
| 156 SendResponse(true); | 142 SendResponse(true); |
| 157 } | 143 } |
| 158 | 144 |
| 159 } // namespace extensions | 145 } // namespace extensions |
| OLD | NEW |