From 3c0e906cf98d0af11a67dc9c44ba2b93a2bba80a Mon Sep 17 00:00:00 2001 From: "finnur@chromium.org" Date: Thu, 14 May 2009 20:51:06 +0000 Subject: Extend the chrome.tabs API to allow relative paths. BUG=None TEST=In an extension, use chrome.tabs.create(...) to create a tab using a relative path ("foo/bar.html"). It should resolve that path by prepending the extension path ("chrome-extension://0000000000000000000000000000000000000042/foo/bar.html). Review URL: http://codereview.chromium.org/113374 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16098 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/extensions/extension_tabs_module.cc | 31 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/chrome/browser/extensions/extension_tabs_module.cc b/chrome/browser/extensions/extension_tabs_module.cc index 782bfe8..82a2770 100644 --- a/chrome/browser/extensions/extension_tabs_module.cc +++ b/chrome/browser/extensions/extension_tabs_module.cc @@ -7,8 +7,10 @@ #include "base/string_util.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" +#include "chrome/browser/extensions/extension.h" #include "chrome/browser/extensions/extension_error_utils.h" #include "chrome/browser/extensions/extension_function_dispatcher.h" +#include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/renderer_host/render_view_host_delegate.h" #include "chrome/browser/tab_contents/navigation_entry.h" @@ -58,6 +60,10 @@ static bool GetTabById(int tab_id, Profile* profile, Browser** browser, TabContents** contents, int* tab_index, std::string* error_message); +// Construct an absolute path from a relative path. +static GURL AbsolutePath(Profile* profile, std::string extension_id, + std::string relative_url); + // ExtensionTabUtil int ExtensionTabUtil::GetWindowId(const Browser* browser) { return browser->session_id().id(); @@ -385,9 +391,13 @@ bool CreateTabFunction::RunImpl() { EXTENSION_FUNCTION_VALIDATE(args->GetString(kUrlKey, &url_string)); url.reset(new GURL(url_string)); if (!url->is_valid()) { - error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError, - url_string); - return false; + // The path as passed in is not valid. Try converting to absolute path. + *url = AbsolutePath(profile(), extension_id(), url_string); + if (!url->is_valid()) { + error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError, + url_string); + return false; + } } } @@ -466,8 +476,12 @@ bool UpdateTabFunction::RunImpl() { GURL new_gurl(url); if (!new_gurl.is_valid()) { - error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError, url); - return false; + // The path as passed in is not valid. Try converting to absolute path. + new_gurl = AbsolutePath(profile(), extension_id(), url); + if (!new_gurl.is_valid()) { + error_ = ExtensionErrorUtils::FormatErrorMessage(kInvalidUrlError, url); + return false; + } } controller.LoadURL(new_gurl, GURL(), PageTransition::LINK); @@ -617,6 +631,13 @@ static Browser* GetBrowserInProfileWithId(Profile* profile, return NULL; } +static GURL AbsolutePath(Profile* profile, std::string extension_id, + std::string relative_url) { + ExtensionsService* service = profile->GetExtensionsService(); + Extension* extension = service->GetExtensionByID(extension_id); + return Extension::GetResourceURL(extension->url(), relative_url); +} + static bool GetTabById(int tab_id, Profile* profile, Browser** browser, TabStripModel** tab_strip, TabContents** contents, -- cgit v1.1