Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(605)

Unified Diff: chrome/browser/extensions/extension_tabs_module.cc

Issue 9234042: Re-land alexbost's experimental offscreenTabs API. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: merge / rebase Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 f737d37cff16fb875b593f9c50bf7b21826b7e6c..57d27f7e62cded697147791220589513f0cc4f32 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() {
@@ -1096,69 +1097,15 @@ bool UpdateTabFunction::RunImpl() {
}
web_contents_ = contents->web_contents();
- NavigationController& controller = web_contents_->GetController();
// TODO(rafaelw): handle setting remaining tab properties:
// -title
// -favIconUrl
- // We wait to fire the callback when executing 'javascript:' URLs in tabs.
- bool is_async = false;
-
// 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(
- 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().
-
- is_async = 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(), web_contents_->GetURL().spec());
- }
+ bool is_async = false;
+ if (!UpdateURLIfPresent(update_props, &is_async))
+ return false;
bool active = false;
// TODO(rafaelw): Setting |active| from js doesn't make much sense.
@@ -1220,6 +1167,66 @@ bool UpdateTabFunction::RunImpl() {
return true;
}
+bool UpdateTabFunction::UpdateURLIfPresent(DictionaryValue* update_props,
+ 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().
+
+ *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;
+}
+
void UpdateTabFunction::PopulateResult() {
if (!has_callback())
return;
@@ -1271,7 +1278,7 @@ void UpdateTabFunction::OnExecuteCodeFinished(int request_id,
SendResponse(success);
Observe(NULL);
- Release(); // Balanced in RunImpl().
+ Release(); // Balanced in UpdateURLIfPresent().
}
bool MoveTabsFunction::RunImpl() {
@@ -1474,7 +1481,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;
@@ -1485,6 +1493,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.
@@ -1513,28 +1538,18 @@ 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.
- BackingStore* backing_store = render_view_host->GetBackingStore(false);
- if (backing_store && CaptureSnapshotFromBackingStore(backing_store))
+ if (CaptureSnapshotFromBackingStore(web_contents))
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,
@@ -1549,7 +1564,11 @@ bool CaptureVisibleTabFunction::RunImpl() {
// For example, some uncommon X11 visual modes are not supported by
// CopyFromBackingStore().
bool CaptureVisibleTabFunction::CaptureSnapshotFromBackingStore(
- BackingStore* backing_store) {
+ WebContents* web_contents) {
+ BackingStore* backing_store =
+ web_contents->GetRenderViewHost()->GetBackingStore(false);
+ if (!backing_store)
+ return false;
skia::PlatformCanvas temp_canvas;
if (!backing_store->CopyFromBackingStore(gfx::Rect(backing_store->size()),

Powered by Google App Engine
This is Rietveld 408576698