Commit 2e7ae44d authored by kkania@chromium.org's avatar kkania@chromium.org

[chromedriver] Fallback to devtools for screenshotting of forbidden urls.

BUG=none
R=chrisgao@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@222200 0039d316-1c4b-4281-b951-d872f2087c98
parent 38d32753
...@@ -24,7 +24,7 @@ Status AutomationExtension::CaptureScreenshot(std::string* screenshot) { ...@@ -24,7 +24,7 @@ Status AutomationExtension::CaptureScreenshot(std::string* screenshot) {
base::TimeDelta::FromSeconds(10), base::TimeDelta::FromSeconds(10),
&result); &result);
if (status.IsError()) if (status.IsError())
return Status(kUnknownError, "cannot take screenshot", status); return Status(status.code(), "cannot take screenshot", status);
if (!result->GetAsString(screenshot)) if (!result->GetAsString(screenshot))
return Status(kUnknownError, "screenshot is not a string"); return Status(kUnknownError, "screenshot is not a string");
return Status(kOk); return Status(kOk);
......
...@@ -24,6 +24,7 @@ class AutomationExtension { ...@@ -24,6 +24,7 @@ class AutomationExtension {
~AutomationExtension(); ~AutomationExtension();
// Captures the visible part of the current tab as a base64-encoded PNG. // Captures the visible part of the current tab as a base64-encoded PNG.
// Returns |kForbidden| for security restricted pages.
Status CaptureScreenshot(std::string* screenshot); Status CaptureScreenshot(std::string* screenshot);
// Gets the position of the current window. // Gets the position of the current window.
......
...@@ -55,6 +55,8 @@ const char* DefaultMessageForStatusCode(StatusCode code) { ...@@ -55,6 +55,8 @@ const char* DefaultMessageForStatusCode(StatusCode code) {
return "chrome not reachable"; return "chrome not reachable";
case kDisconnected: case kDisconnected:
return "disconnected"; return "disconnected";
case kForbidden:
return "forbidden";
default: default:
return "<unknown>"; return "<unknown>";
} }
......
...@@ -32,6 +32,7 @@ enum StatusCode { ...@@ -32,6 +32,7 @@ enum StatusCode {
kChromeNotReachable = 100, kChromeNotReachable = 100,
kNoSuchExecutionContext, kNoSuchExecutionContext,
kDisconnected, kDisconnected,
kForbidden = 103,
}; };
// Represents a WebDriver status, which may be an error or ok. // Represents a WebDriver status, which may be an error or ok.
......
...@@ -27,6 +27,13 @@ function checkForExtensionError(errCallback) { ...@@ -27,6 +27,13 @@ function checkForExtensionError(errCallback) {
*/ */
function captureScreenshot(callback, errCallback) { function captureScreenshot(callback, errCallback) {
chrome.tabs.captureVisibleTab({format:'png'}, function(dataUrl) { chrome.tabs.captureVisibleTab({format:'png'}, function(dataUrl) {
if (chrome.extension.lastError &&
chrome.extension.lastError.message.indexOf('permission') != -1) {
var error = new Error(chrome.extension.lastError.message);
error.code = 103; // kForbidden
errCallback(error);
return;
}
checkForExtensionError(errCallback); checkForExtensionError(errCallback);
var base64 = ';base64,'; var base64 = ';base64,';
callback(dataUrl.substr(dataUrl.indexOf(base64) + base64.length)) callback(dataUrl.substr(dataUrl.indexOf(base64) + base64.length))
......
...@@ -743,13 +743,15 @@ Status ExecuteScreenshot( ...@@ -743,13 +743,15 @@ Status ExecuteScreenshot(
if (status.IsError()) if (status.IsError())
return status; return status;
status = extension->CaptureScreenshot(&screenshot); status = extension->CaptureScreenshot(&screenshot);
if (status.IsError()) // If the screenshot was forbidden, fallback to DevTools.
return status; if (status.code() != kForbidden)
} else {
Status status = web_view->CaptureScreenshot(&screenshot);
if (status.IsError())
return status; return status;
} }
status = web_view->CaptureScreenshot(&screenshot);
if (status.IsError())
return status;
value->reset(new base::StringValue(screenshot)); value->reset(new base::StringValue(screenshot));
return Status(kOk); return Status(kOk);
} }
......
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