Commit b83f02af authored by lfg's avatar lfg Committed by Commit bot

Moving capture_web_contents_function to extensions.

BUG=352290

Review URL: https://codereview.chromium.org/522763002

Cr-Commit-Position: refs/heads/master@{#293356}
parent 626da5b7
......@@ -64,6 +64,7 @@
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/api/capture_web_contents_function_impl.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/extension_function_dispatcher.h"
#include "extensions/browser/extension_function_util.h"
......@@ -105,6 +106,8 @@ namespace tabs = api::tabs;
using api::tabs::InjectDetails;
template class CaptureWebContentsFunction<ChromeAsyncExtensionFunction>;
namespace {
bool GetBrowserFromWindowID(ChromeUIThreadExtensionFunction* function,
......
......@@ -9,13 +9,13 @@
#include <vector>
#include "base/compiler_specific.h"
#include "chrome/browser/extensions/api/capture_web_contents_function.h"
#include "chrome/browser/extensions/api/execute_code_function.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/ui/zoom/zoom_controller.h"
#include "chrome/common/extensions/api/tabs.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/api/capture_web_contents_function.h"
#include "extensions/common/extension_resource.h"
#include "extensions/common/user_script.h"
#include "url/gurl.h"
......@@ -195,7 +195,8 @@ class TabsDetectLanguageFunction : public ChromeAsyncExtensionFunction,
DECLARE_EXTENSION_FUNCTION("tabs.detectLanguage", TABS_DETECTLANGUAGE)
};
class TabsCaptureVisibleTabFunction
: public extensions::CaptureWebContentsFunction {
: public extensions::CaptureWebContentsFunction<
ChromeAsyncExtensionFunction> {
public:
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
......
......@@ -51,10 +51,6 @@ const char kWindowTypeLongKey[] = "windowType";
const char kZoomSettingsMode[] = "mode";
const char kZoomSettingsScope[] = "scope";
const char kFormatValueJpeg[] = "jpeg";
const char kFormatValuePng[] = "png";
const char kMimeTypeJpeg[] = "image/jpeg";
const char kMimeTypePng[] = "image/png";
const char kShowStateValueNormal[] = "normal";
const char kShowStateValueMinimized[] = "minimized";
const char kShowStateValueMaximized[] = "maximized";
......
......@@ -58,10 +58,6 @@ extern const char kZoomSettingsScope[];
// Value consts.
extern const char kCanOnlyMoveTabsWithinNormalWindowsError[];
extern const char kCanOnlyMoveTabsWithinSameProfileError[];
extern const char kFormatValueJpeg[];
extern const char kFormatValuePng[];
extern const char kMimeTypeJpeg[];
extern const char kMimeTypePng[];
extern const char kShowStateValueNormal[];
extern const char kShowStateValueMinimized[];
extern const char kShowStateValueMaximized[];
......
......@@ -16,6 +16,7 @@
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/stop_find_action.h"
#include "extensions/browser/api/capture_web_contents_function_impl.h"
#include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
#include "extensions/common/error_utils.h"
#include "third_party/WebKit/public/web/WebFindOptions.h"
......
......@@ -5,9 +5,10 @@
#ifndef CHROME_BROWSER_EXTENSIONS_API_WEB_VIEW_WEB_VIEW_INTERNAL_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_WEB_VIEW_WEB_VIEW_INTERNAL_API_H_
#include "chrome/browser/extensions/api/capture_web_contents_function.h"
#include "chrome/browser/extensions/api/execute_code_function.h"
#include "chrome/browser/guest_view/web_view/web_view_find_helper.h"
#include "extensions/browser/api/capture_web_contents_function.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
// WARNING: WebViewInternal could be loaded in an unblessed context, thus any
......@@ -214,7 +215,7 @@ class WebViewInternalInsertCSSFunction
};
class WebViewInternalCaptureVisibleRegionFunction
: public extensions::CaptureWebContentsFunction {
: public extensions::CaptureWebContentsFunction<AsyncExtensionFunction> {
DECLARE_EXTENSION_FUNCTION("webViewInternal.captureVisibleRegion",
WEBVIEWINTERNAL_CAPTUREVISIBLEREGION);
......
......@@ -136,8 +136,6 @@
'browser/extensions/api/browser/browser_api.h',
'browser/extensions/api/browsing_data/browsing_data_api.cc',
'browser/extensions/api/browsing_data/browsing_data_api.h',
'browser/extensions/api/capture_web_contents_function.cc',
'browser/extensions/api/capture_web_contents_function.h',
'browser/extensions/api/chrome_extensions_api_client.cc',
'browser/extensions/api/chrome_extensions_api_client.h',
'browser/extensions/api/cloud_print_private/cloud_print_private_api.cc',
......
......@@ -5,7 +5,6 @@
#ifndef CHROME_BROWSER_EXTENSIONS_API_CAPTURE_WEB_CONTENTS_FUNCTION_H_
#define CHROME_BROWSER_EXTENSIONS_API_CAPTURE_WEB_CONTENTS_FUNCTION_H_
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "extensions/common/api/extension_types.h"
class SkBitmap;
......@@ -18,12 +17,15 @@ namespace extensions {
// Base class for capturing visibile area of a WebContents.
// This is used by both webview.captureVisibleRegion and tabs.captureVisibleTab.
class CaptureWebContentsFunction : public ChromeAsyncExtensionFunction {
// The template parameter T should be either AsyncExtensionFunction or
// ChromeAsyncExtensionFunction (in the cases the chrome Profile is required)
template <typename T>
class CaptureWebContentsFunction : public T {
public:
CaptureWebContentsFunction();
CaptureWebContentsFunction() {}
protected:
virtual ~CaptureWebContentsFunction();
virtual ~CaptureWebContentsFunction() {}
// ExtensionFunction implementation.
virtual bool HasPermission() OVERRIDE;
......@@ -39,6 +41,9 @@ class CaptureWebContentsFunction : public ChromeAsyncExtensionFunction {
};
virtual void OnCaptureFailure(FailureReason reason) = 0;
// ValidationFailure override to match RunAsync().
static bool ValidationFailure(CaptureWebContentsFunction* function);
private:
typedef core_api::extension_types::ImageDetails ImageDetails;
......
......@@ -2,11 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/capture_web_contents_function.h"
#include "extensions/browser/api/capture_web_contents_function.h"
#include "base/base64.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
......@@ -22,26 +21,22 @@ using content::WebContents;
namespace extensions {
CaptureWebContentsFunction::CaptureWebContentsFunction() {
}
CaptureWebContentsFunction::~CaptureWebContentsFunction() {
}
bool CaptureWebContentsFunction::HasPermission() {
template <typename T>
bool CaptureWebContentsFunction<T>::HasPermission() {
return true;
}
bool CaptureWebContentsFunction::RunAsync() {
EXTENSION_FUNCTION_VALIDATE(args_);
template <typename T>
bool CaptureWebContentsFunction<T>::RunAsync() {
EXTENSION_FUNCTION_VALIDATE(T::args_);
context_id_ = extension_misc::kCurrentWindowId;
args_->GetInteger(0, &context_id_);
T::args_->GetInteger(0, &context_id_);
scoped_ptr<ImageDetails> image_details;
if (args_->GetSize() > 1) {
if (T::args_->GetSize() > 1) {
base::Value* spec = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec);
EXTENSION_FUNCTION_VALIDATE(T::args_->Get(1, &spec) && spec);
image_details = ImageDetails::FromValue(*spec);
}
......@@ -81,7 +76,8 @@ bool CaptureWebContentsFunction::RunAsync() {
return true;
}
void CaptureWebContentsFunction::CopyFromBackingStoreComplete(
template <typename T>
void CaptureWebContentsFunction<T>::CopyFromBackingStoreComplete(
bool succeeded,
const SkBitmap& bitmap) {
if (succeeded) {
......@@ -91,7 +87,8 @@ void CaptureWebContentsFunction::CopyFromBackingStoreComplete(
OnCaptureFailure(FAILURE_REASON_UNKNOWN);
}
void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) {
template <typename T>
void CaptureWebContentsFunction<T>::OnCaptureSuccess(const SkBitmap& bitmap) {
std::vector<unsigned char> data;
SkAutoLockPixels screen_capture_lock(bitmap);
bool encoded = false;
......@@ -106,14 +103,14 @@ void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) {
static_cast<int>(bitmap.rowBytes()),
image_quality_,
&data);
mime_type = tabs_constants::kMimeTypeJpeg;
mime_type = kMimeTypeJpeg;
break;
case ImageDetails::FORMAT_PNG:
encoded = gfx::PNGCodec::EncodeBGRASkBitmap(
bitmap,
true, // Discard transparency.
&data);
mime_type = tabs_constants::kMimeTypePng;
encoded =
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap,
true, // Discard transparency.
&data);
mime_type = kMimeTypePng;
break;
default:
NOTREACHED() << "Invalid image format.";
......@@ -129,10 +126,17 @@ void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) {
reinterpret_cast<const char*>(vector_as_array(&data)), data.size());
base::Base64Encode(stream_as_string, &base64_result);
base64_result.insert(0, base::StringPrintf("data:%s;base64,",
mime_type.c_str()));
SetResult(new base::StringValue(base64_result));
SendResponse(true);
base64_result.insert(
0, base::StringPrintf("data:%s;base64,", mime_type.c_str()));
T::SetResult(new base::StringValue(base64_result));
T::SendResponse(true);
}
template <typename T>
bool CaptureWebContentsFunction<T>::ValidationFailure(
CaptureWebContentsFunction<T>* function) {
return T::ValidationFailure(function);
}
} // namespace extensions
......@@ -53,7 +53,7 @@ class Sender;
#define EXTENSION_FUNCTION_VALIDATE(test) \
do { \
if (!(test)) { \
bad_message_ = true; \
this->bad_message_ = true; \
return ValidationFailure(this); \
} \
} while (0)
......@@ -64,7 +64,7 @@ class Sender;
#define EXTENSION_FUNCTION_ERROR(error) \
do { \
error_ = error; \
bad_message_ = true; \
this->bad_message_ = true; \
return ValidationFailure(this); \
} while (0)
......
......@@ -54,6 +54,9 @@ const char kStateStoreName[] = "Extension State";
const char kRulesStoreName[] = "Extension Rules";
const char kWebStoreAppId[] = "ahfgeienlihckogmohjhadlkjgocpleb";
const char kMimeTypeJpeg[] = "image/jpeg";
const char kMimeTypePng[] = "image/png";
} // namespace extensions
namespace extension_misc {
......
......@@ -101,6 +101,10 @@ extern const char kRulesStoreName[];
// The URL query parameter key corresponding to multi-login user index.
extern const char kAuthUserQueryKey[];
// Mime type strings
extern const char kMimeTypeJpeg[];
extern const char kMimeTypePng[];
// The extension id of the Web Store component application.
extern const char kWebStoreAppId[];
......
......@@ -335,6 +335,8 @@
'browser/api/bluetooth_socket/bluetooth_socket_api.h',
'browser/api/bluetooth_socket/bluetooth_socket_event_dispatcher.cc',
'browser/api/bluetooth_socket/bluetooth_socket_event_dispatcher.h',
'browser/api/capture_web_contents_function.h',
'browser/api/capture_web_contents_function_impl.h',
'browser/api/cast_channel/cast_auth_util.cc',
'browser/api/cast_channel/cast_auth_util.h',
'browser/api/cast_channel/cast_channel_api.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment