diff options
author | yoshiki@chromium.org <yoshiki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 06:48:44 +0000 |
---|---|---|
committer | yoshiki@chromium.org <yoshiki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 06:48:44 +0000 |
commit | 0909a448cdbaa3b71a4fd354f9dc62d8d39b58fe (patch) | |
tree | 761d3b6a0d532527486784d5a5ec4fbe1f93dbc7 | |
parent | 8ac09b213e743837f51f441e51ddc5840f79cc6c (diff) | |
download | chromium_src-0909a448cdbaa3b71a4fd354f9dc62d8d39b58fe.zip chromium_src-0909a448cdbaa3b71a4fd354f9dc62d8d39b58fe.tar.gz chromium_src-0909a448cdbaa3b71a4fd354f9dc62d8d39b58fe.tar.bz2 |
Files.app: Add an private API to get a download URL
This patch just adds the API to get a download URL with an drive read only token.
BUG=305511
TEST=manually teste
R=asargent@chromium.org, hashimoto@chromium.org, kinaba@chromium.org
TBR=isherman@chromium.org
Review URL: https://codereview.chromium.org/371883003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283671 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 140 insertions, 4 deletions
diff --git a/chrome/browser/chromeos/extensions/file_manager/private_api_drive.cc b/chrome/browser/chromeos/extensions/file_manager/private_api_drive.cc index e0685b2..9691914 100644 --- a/chrome/browser/chromeos/extensions/file_manager/private_api_drive.cc +++ b/chrome/browser/chromeos/extensions/file_manager/private_api_drive.cc @@ -4,6 +4,7 @@ #include "chrome/browser/chromeos/extensions/file_manager/private_api_drive.h" +#include "base/command_line.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chromeos/drive/drive_integration_service.h" #include "chrome/browser/chromeos/extensions/file_manager/private_api_util.h" @@ -16,8 +17,13 @@ #include "chrome/browser/drive/event_logger.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" -#include "chrome/common/extensions/api/file_browser_private.h" +#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" +#include "chrome/browser/signin/signin_manager_factory.h" +#include "chromeos/chromeos_switches.h" +#include "components/signin/core/browser/profile_oauth2_token_service.h" +#include "components/signin/core/browser/signin_manager.h" #include "content/public/browser/browser_thread.h" +#include "google_apis/drive/auth_service.h" #include "webkit/common/fileapi/file_system_info.h" #include "webkit/common/fileapi/file_system_util.h" @@ -887,4 +893,96 @@ void FileBrowserPrivateRequestDriveShareFunction::OnAddPermission( SendResponse(error == drive::FILE_ERROR_OK); } +FileBrowserPrivateGetDownloadUrlFunction:: + FileBrowserPrivateGetDownloadUrlFunction() { +} + +FileBrowserPrivateGetDownloadUrlFunction:: + ~FileBrowserPrivateGetDownloadUrlFunction() { +} + +bool FileBrowserPrivateGetDownloadUrlFunction::RunAsync() { + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); + if (!command_line->HasSwitch( + chromeos::switches::kEnableVideoPlayerChromecastSupport)) { + SetError("Cast support is disabled."); + SetResult(new base::StringValue("")); // Intentionally returns a blank. + return false; + } + + using extensions::api::file_browser_private::GetShareUrl::Params; + const scoped_ptr<Params> params(Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params); + + // Start getting the file info. + drive::FileSystemInterface* const file_system = + drive::util::GetFileSystemByProfile(GetProfile()); + if (!file_system) { + // |file_system| is NULL if Drive is disabled or not mounted. + SetError("Drive is disabled or not mounted."); + SetResult(new base::StringValue("")); // Intentionally returns a blank. + return false; + } + + const base::FilePath path = file_manager::util::GetLocalPathFromURL( + render_view_host(), GetProfile(), GURL(params->url)); + DCHECK(drive::util::IsUnderDriveMountPoint(path)); + base::FilePath file_path = drive::util::ExtractDrivePath(path); + + file_system->GetResourceEntry( + file_path, + base::Bind(&FileBrowserPrivateGetDownloadUrlFunction::OnGetResourceEntry, + this)); + return true; +} + +void FileBrowserPrivateGetDownloadUrlFunction::OnGetResourceEntry( + drive::FileError error, + scoped_ptr<drive::ResourceEntry> entry) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + + if (error != drive::FILE_ERROR_OK) { + SetError("Download Url for this item is not available."); + SetResult(new base::StringValue("")); // Intentionally returns a blank. + SendResponse(false); + return; + } + + download_url_ = + google_apis::DriveApiUrlGenerator::kBaseDownloadUrlForProduction + + entry->resource_id(); + + ProfileOAuth2TokenService* oauth2_token_service = + ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile()); + SigninManagerBase* signin_manager = + SigninManagerFactory::GetForProfile(GetProfile()); + const std::string& account_id = signin_manager->GetAuthenticatedAccountId(); + std::vector<std::string> scopes; + scopes.push_back("https://www.googleapis.com/auth/drive.readonly"); + + auth_service_.reset( + new google_apis::AuthService(oauth2_token_service, + account_id, + GetProfile()->GetRequestContext(), + scopes)); + auth_service_->StartAuthentication(base::Bind( + &FileBrowserPrivateGetDownloadUrlFunction::OnTokenFetched, this)); +} + +void FileBrowserPrivateGetDownloadUrlFunction::OnTokenFetched( + google_apis::GDataErrorCode code, + const std::string& access_token) { + if (code != google_apis::HTTP_SUCCESS) { + SetError("Not able to fetch the token."); + SetResult(new base::StringValue("")); // Intentionally returns a blank. + SendResponse(false); + return; + } + + const std::string url = download_url_ + "?access_token=" + access_token; + SetResult(new base::StringValue(url)); + + SendResponse(true); +} + } // namespace extensions diff --git a/chrome/browser/chromeos/extensions/file_manager/private_api_drive.h b/chrome/browser/chromeos/extensions/file_manager/private_api_drive.h index b3eec92..0db5964 100644 --- a/chrome/browser/chromeos/extensions/file_manager/private_api_drive.h +++ b/chrome/browser/chromeos/extensions/file_manager/private_api_drive.h @@ -18,6 +18,10 @@ class ResourceEntry; struct SearchResultInfo; } +namespace google_apis { +class AuthService; +} + namespace extensions { namespace api { @@ -240,6 +244,34 @@ class FileBrowserPrivateRequestDriveShareFunction void OnAddPermission(drive::FileError error); }; +// Implements the chrome.fileBrowserPrivate.getDownloadUrl method. +class FileBrowserPrivateGetDownloadUrlFunction + : public LoggedAsyncExtensionFunction { + public: + FileBrowserPrivateGetDownloadUrlFunction(); + + DECLARE_EXTENSION_FUNCTION("fileBrowserPrivate.getDownloadUrl", + FILEBROWSERPRIVATE_GETDOWNLOADURL) + + protected: + virtual ~FileBrowserPrivateGetDownloadUrlFunction(); + + // AsyncExtensionFunction overrides. + virtual bool RunAsync() OVERRIDE; + + void OnGetResourceEntry(drive::FileError error, + scoped_ptr<drive::ResourceEntry> entry); + + // Callback with an |access_token|, called by + // drive::DriveReadonlyTokenFetcher. + void OnTokenFetched(google_apis::GDataErrorCode code, + const std::string& access_token); + + private: + std::string download_url_; + scoped_ptr<google_apis::AuthService> auth_service_; +}; + } // namespace extensions #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_FILE_MANAGER_PRIVATE_API_DRIVE_H_ diff --git a/chrome/common/extensions/api/file_browser_private.idl b/chrome/common/extensions/api/file_browser_private.idl index 173d164..0a4a7a6 100644 --- a/chrome/common/extensions/api/file_browser_private.idl +++ b/chrome/common/extensions/api/file_browser_private.idl @@ -533,8 +533,8 @@ callback RequestAccessTokenCallback = void(DOMString accessToken); // |accessToken| OAuth2 access token, or an empty string if failed to fetch. callback RequestWebStoreAccessTokenCallback = void(DOMString accessToken); -// |shareUrl| Share Url for the sharing dialog. -callback GetShareUrlCallback = void(DOMString shareUrl); +// |url| Result url. +callback GetUrlCallback = void(DOMString url); // |profiles| List of profile information. // |runningProfile| ID of the profile that runs the application instance. @@ -769,7 +769,12 @@ interface Functions { // Requests a share dialog url for the specified file. // |url| Url for the file. // |callback| - static void getShareUrl(DOMString url, GetShareUrlCallback callback); + static void getShareUrl(DOMString url, GetUrlCallback callback); + + // Requests a download url to download the file contents. + // |url| Url for the file. + // |callback| + static void getDownloadUrl(DOMString url, GetUrlCallback callback); // Requests to share drive files. // |url| URL of a file to be shared. diff --git a/extensions/browser/extension_function_histogram_value.h b/extensions/browser/extension_function_histogram_value.h index 188ec7c..a8eeb79 100644 --- a/extensions/browser/extension_function_histogram_value.h +++ b/extensions/browser/extension_function_histogram_value.h @@ -899,6 +899,7 @@ enum HistogramValue { GCDPRIVATE_GETCOMMANDSLIST, APPVIEWINTERNAL_ATTACHFRAME, APPVIEWINTERNAL_DENYREQUEST, + FILEBROWSERPRIVATE_GETDOWNLOADURL, // Last entry: Add new entries above and ensure to update // tools/metrics/histograms/histograms/histograms.xml. ENUM_BOUNDARY |