| Index: chrome/browser/extensions/extension_tabs_module.cc
|
| diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc
|
| index 6333a30d3b7664f6528896f9c89614009ad865ff..68a48e47d2d70aaf2ff8e769ff59dc7b11356fe8 100644
|
| --- a/chrome/browser/extensions/extension_tabs_module.cc
|
| +++ b/chrome/browser/extensions/extension_tabs_module.cc
|
| @@ -67,6 +67,8 @@ using content::NavigationEntry;
|
| using content::OpenURLParams;
|
| using content::Referrer;
|
| using content::WebContents;
|
| +using extensions::tabs_module::IsCrashURL;
|
| +using extensions::tabs_module::ResolvePossiblyRelativeURL;
|
|
|
| const int CaptureVisibleTabFunction::kDefaultQuality = 90;
|
|
|
| @@ -138,31 +140,6 @@ bool GetTabById(int tab_id,
|
| return false;
|
| }
|
|
|
| -// Takes |url_string| and returns a GURL which is either valid and absolute
|
| -// or invalid. If |url_string| is not directly interpretable as a valid (it is
|
| -// likely a relative URL) an attempt is made to resolve it. |extension| is
|
| -// provided so it can be resolved relative to its extension base
|
| -// (chrome-extension://<id>/). Using the source frame url would be more correct,
|
| -// but because the api shipped with urls resolved relative to their extension
|
| -// base, we decided it wasn't worth breaking existing extensions to fix.
|
| -GURL ResolvePossiblyRelativeURL(const std::string& url_string,
|
| - const Extension* extension) {
|
| - GURL url = GURL(url_string);
|
| - if (!url.is_valid())
|
| - url = extension->GetResourceURL(url_string);
|
| -
|
| - return url;
|
| -}
|
| -
|
| -bool IsCrashURL(const GURL& url) {
|
| - // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
|
| - GURL fixed_url =
|
| - URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string());
|
| - return (fixed_url.SchemeIs(chrome::kChromeUIScheme) &&
|
| - (fixed_url.host() == chrome::kChromeUIBrowserCrashHost ||
|
| - fixed_url.host() == chrome::kChromeUICrashHost));
|
| -}
|
| -
|
| // Reads the |value| as either a single integer value or a list of integers.
|
| bool ReadOneOrMoreIntegers(
|
| Value* value, std::vector<int>* result) {
|
| @@ -212,6 +189,30 @@ QueryArg ParseBoolQueryArg(base::DictionaryValue* query, const char* key) {
|
|
|
| } // namespace
|
|
|
| +namespace extensions {
|
| +namespace tabs_module {
|
| +
|
| +GURL ResolvePossiblyRelativeURL(const std::string& url_string,
|
| + const Extension* extension) {
|
| + GURL url = GURL(url_string);
|
| + if (!url.is_valid())
|
| + url = extension->GetResourceURL(url_string);
|
| +
|
| + return url;
|
| +}
|
| +
|
| +bool IsCrashURL(const GURL& url) {
|
| + // Check a fixed-up URL, to normalize the scheme and parse hosts correctly.
|
| + GURL fixed_url =
|
| + URLFixerUpper::FixupURL(url.possibly_invalid_spec(), std::string());
|
| + return (fixed_url.SchemeIs(chrome::kChromeUIScheme) &&
|
| + (fixed_url.host() == chrome::kChromeUIBrowserCrashHost ||
|
| + fixed_url.host() == chrome::kChromeUICrashHost));
|
| +}
|
| +
|
| +} // namespace tabs_module
|
| +} // namespace extensions
|
| +
|
| // Windows ---------------------------------------------------------------------
|
|
|
| bool GetWindowFunction::RunImpl() {
|
| @@ -1067,67 +1068,15 @@ bool UpdateTabFunction::RunImpl() {
|
| NULL, &tab_strip, &contents, &tab_index, &error_)) {
|
| return false;
|
| }
|
| - NavigationController& controller = contents->web_contents()->GetController();
|
|
|
| // TODO(rafaelw): handle setting remaining tab properties:
|
| // -title
|
| // -favIconUrl
|
|
|
| // Navigate the tab to a new location if the url is different.
|
| - std::string url_string;
|
| - if (update_props->HasKey(keys::kUrlKey)) {
|
| - EXTENSION_FUNCTION_VALIDATE(update_props->GetString(
|
| - keys::kUrlKey, &url_string));
|
| - GURL url = ResolvePossiblyRelativeURL(url_string, GetExtension());
|
| -
|
| - if (!url.is_valid()) {
|
| - error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kInvalidUrlError,
|
| - url_string);
|
| - return false;
|
| - }
|
| -
|
| - // Don't let the extension crash the browser or renderers.
|
| - if (IsCrashURL(url)) {
|
| - error_ = keys::kNoCrashBrowserError;
|
| - return false;
|
| - }
|
| -
|
| - // JavaScript URLs can do the same kinds of things as cross-origin XHR, so
|
| - // we need to check host permissions before allowing them.
|
| - if (url.SchemeIs(chrome::kJavaScriptScheme)) {
|
| - if (!GetExtension()->CanExecuteScriptOnPage(
|
| - contents->web_contents()->GetURL(), NULL, &error_)) {
|
| - return false;
|
| - }
|
| -
|
| - ExtensionMsg_ExecuteCode_Params params;
|
| - params.request_id = request_id();
|
| - params.extension_id = extension_id();
|
| - params.is_javascript = true;
|
| - params.code = url.path();
|
| - params.all_frames = false;
|
| - params.in_main_world = true;
|
| -
|
| - RenderViewHost* render_view_host =
|
| - contents->web_contents()->GetRenderViewHost();
|
| - render_view_host->Send(
|
| - new ExtensionMsg_ExecuteCode(render_view_host->routing_id(),
|
| - params));
|
| -
|
| - Observe(contents->web_contents());
|
| - AddRef(); // balanced in Observe()
|
| -
|
| - return true;
|
| - }
|
| -
|
| - controller.LoadURL(
|
| - url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
|
| -
|
| - // The URL of a tab contents never actually changes to a JavaScript URL, so
|
| - // this check only makes sense in other cases.
|
| - if (!url.SchemeIs(chrome::kJavaScriptScheme))
|
| - DCHECK_EQ(url.spec(), contents->web_contents()->GetURL().spec());
|
| - }
|
| + bool is_async = false;
|
| + if (!UpdateURLIfPresent(update_props, contents->web_contents(), &is_async))
|
| + return false;
|
|
|
| bool active = false;
|
| // TODO(rafaelw): Setting |active| from js doesn't make much sense.
|
| @@ -1177,7 +1126,71 @@ bool UpdateTabFunction::RunImpl() {
|
| }
|
| }
|
|
|
| - SendResponse(true);
|
| + if (!is_async)
|
| + SendResponse(true);
|
| +
|
| + return true;
|
| +}
|
| +
|
| +bool UpdateTabFunction::UpdateURLIfPresent(DictionaryValue* update_props,
|
| + WebContents* web_contents,
|
| + bool* is_async) {
|
| + if (!update_props->HasKey(keys::kUrlKey))
|
| + return true;
|
| +
|
| + std::string url_string;
|
| + EXTENSION_FUNCTION_VALIDATE(update_props->GetString(
|
| + keys::kUrlKey, &url_string));
|
| + GURL url = ResolvePossiblyRelativeURL(url_string, GetExtension());
|
| +
|
| + if (!url.is_valid()) {
|
| + error_ = ExtensionErrorUtils::FormatErrorMessage(
|
| + keys::kInvalidUrlError, url_string);
|
| + return false;
|
| + }
|
| +
|
| + // Don't let the extension crash the browser or renderers.
|
| + if (IsCrashURL(url)) {
|
| + error_ = keys::kNoCrashBrowserError;
|
| + return false;
|
| + }
|
| +
|
| + // JavaScript URLs can do the same kinds of things as cross-origin XHR, so
|
| + // we need to check host permissions before allowing them.
|
| + if (url.SchemeIs(chrome::kJavaScriptScheme)) {
|
| + if (!GetExtension()->CanExecuteScriptOnPage(
|
| + web_contents->GetURL(), NULL, &error_)) {
|
| + return false;
|
| + }
|
| +
|
| + ExtensionMsg_ExecuteCode_Params params;
|
| + params.request_id = request_id();
|
| + params.extension_id = extension_id();
|
| + params.is_javascript = true;
|
| + params.code = url.path();
|
| + params.all_frames = false;
|
| + params.in_main_world = true;
|
| +
|
| + RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
|
| + render_view_host->Send(
|
| + new ExtensionMsg_ExecuteCode(render_view_host->routing_id(), params));
|
| +
|
| + Observe(web_contents);
|
| + AddRef(); // Balanced in OnExecuteCodeFinished().
|
| +
|
| + // Tell the caller to wait for the response.
|
| + *is_async = true;
|
| + return true;
|
| + }
|
| +
|
| + web_contents->GetController().LoadURL(
|
| + url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
|
| +
|
| + // The URL of a tab contents never actually changes to a JavaScript URL, so
|
| + // this check only makes sense in other cases.
|
| + if (!url.SchemeIs(chrome::kJavaScriptScheme))
|
| + DCHECK_EQ(url.spec(), web_contents->GetURL().spec());
|
| +
|
| return true;
|
| }
|
|
|
| @@ -1213,7 +1226,7 @@ void UpdateTabFunction::OnExecuteCodeFinished(int request_id,
|
| SendResponse(success);
|
|
|
| Observe(NULL);
|
| - Release(); // balanced in Execute()
|
| + Release(); // Balanced in UpdateURLIfPresent().
|
| }
|
|
|
| bool MoveTabsFunction::RunImpl() {
|
| @@ -1416,7 +1429,8 @@ bool RemoveTabsFunction::RunImpl() {
|
| return true;
|
| }
|
|
|
| -bool CaptureVisibleTabFunction::RunImpl() {
|
| +bool CaptureVisibleTabFunction::GetTabToCapture(
|
| + WebContents** web_contents, TabContentsWrapper** wrapper) {
|
| Browser* browser = NULL;
|
| // windowId defaults to "current" window.
|
| int window_id = extension_misc::kCurrentWindowId;
|
| @@ -1427,6 +1441,23 @@ bool CaptureVisibleTabFunction::RunImpl() {
|
| if (!GetBrowserFromWindowID(this, window_id, &browser))
|
| return false;
|
|
|
| + *web_contents = browser->GetSelectedWebContents();
|
| + if (*web_contents == NULL) {
|
| + error_ = keys::kInternalVisibleTabCaptureError;
|
| + return false;
|
| + }
|
| +
|
| + *wrapper = browser->GetSelectedTabContentsWrapper();
|
| +
|
| + return true;
|
| +};
|
| +
|
| +bool CaptureVisibleTabFunction::RunImpl() {
|
| + WebContents* web_contents = NULL;
|
| + TabContentsWrapper* wrapper = NULL;
|
| + if (!GetTabToCapture(&web_contents, &wrapper))
|
| + return false;
|
| +
|
| image_format_ = FORMAT_JPEG; // Default format is JPEG.
|
| image_quality_ = kDefaultQuality; // Default quality setting.
|
|
|
| @@ -1455,28 +1486,20 @@ bool CaptureVisibleTabFunction::RunImpl() {
|
| }
|
| }
|
|
|
| - WebContents* web_contents = browser->GetSelectedWebContents();
|
| - if (!web_contents) {
|
| - error_ = keys::kInternalVisibleTabCaptureError;
|
| - return false;
|
| - }
|
| -
|
| // captureVisibleTab() can return an image containing sensitive information
|
| // that the browser would otherwise protect. Ensure the extension has
|
| // permission to do this.
|
| if (!GetExtension()->CanCaptureVisiblePage(web_contents->GetURL(), &error_))
|
| return false;
|
|
|
| - RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
|
| -
|
| // If a backing store is cached for the tab we want to capture,
|
| // and it can be copied into a bitmap, then use it to generate the image.
|
| + RenderViewHost* render_view_host = web_contents->GetRenderViewHost();
|
| BackingStore* backing_store = render_view_host->GetBackingStore(false);
|
| if (backing_store && CaptureSnapshotFromBackingStore(backing_store))
|
| return true;
|
|
|
| // Ask the renderer for a snapshot of the tab.
|
| - TabContentsWrapper* wrapper = browser->GetSelectedTabContentsWrapper();
|
| wrapper->snapshot_tab_helper()->CaptureSnapshot();
|
| registrar_.Add(this,
|
| chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN,
|
|
|