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