Commit 116c89e1 authored by haruki@chromium.org's avatar haruki@chromium.org

drive: Add Profile* as a member of DriveProtocolHandler.

A Profile* can be passed to the constructor of DriveProtocolHandler and
the handler uses the given Profile* for processing the request.
This will help us adding unittests and introducting support for multiple profiles.
If the handler is constructed without Profile*,  it will use the profile from
ProfileManager::GetDefaultProfile().

BUG=167249
TEST=unittests. try opening a URL of 'drive://' (e.g. HTML file stored in Google Drive) on Chrome OS.


Review URL: https://chromiumcodereview.appspot.com/11785018

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176062 0039d316-1c4b-4281-b951-d872f2087c98
parent 4c2323ac
......@@ -17,6 +17,7 @@
#include "base/string_util.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/drive_file_system_interface.h"
#include "chrome/browser/chromeos/drive/drive_system_service.h"
......@@ -92,20 +93,24 @@ bool ParseDriveUrl(const std::string& path, std::string* resource_id) {
}
// Helper function to get DriveSystemService from Profile.
DriveSystemService* GetSystemService() {
return DriveSystemServiceFactory::GetForProfile(
ProfileManager::GetDefaultProfile());
DriveSystemService* GetSystemService(void* profile_id) {
Profile* profile = reinterpret_cast<Profile*>(profile_id);
if (!g_browser_process->profile_manager()->IsValidProfile(profile))
return NULL;
return DriveSystemServiceFactory::GetForProfile(profile);
}
// Helper function to get DriveFileSystem from Profile on UI thread.
DriveFileSystemInterface* GetFileSystemOnUIThread() {
DriveSystemService* system_service = GetSystemService();
DriveFileSystemInterface* GetFileSystemOnUIThread(void* profile_id) {
DriveSystemService* system_service = GetSystemService(profile_id);
return system_service ? system_service->file_system() : NULL;
}
// Helper function to cancel Drive download operation on UI thread.
void CancelDriveDownloadOnUIThread(const FilePath& drive_file_path) {
DriveSystemService* system_service = GetSystemService();
void CancelDriveDownloadOnUIThread(
void* profile_id, const FilePath& drive_file_path) {
DriveSystemService* system_service = GetSystemService(profile_id);
if (system_service)
system_service->drive_service()->CancelForFilePath(drive_file_path);
}
......@@ -115,7 +120,8 @@ void CancelDriveDownloadOnUIThread(const FilePath& drive_file_path) {
// formatted as drive://<resource-id>.
class DriveURLRequestJob : public net::URLRequestJob {
public:
DriveURLRequestJob(net::URLRequest* request,
DriveURLRequestJob(void* profile_id,
net::URLRequest* request,
net::NetworkDelegate* network_delegate);
// net::URLRequestJob overrides:
......@@ -188,6 +194,9 @@ class DriveURLRequestJob : public net::URLRequestJob {
// Helper method to close |stream_|.
void CloseFileStream();
// The profile for processing Drive accesses. Should not be NULL and needs to
// be checked with ProfileManager::IsValidProfile before using it.
void* profile_id_;
DriveFileSystemInterface* file_system_;
bool error_; // True if we've encountered an error.
......@@ -211,9 +220,11 @@ class DriveURLRequestJob : public net::URLRequestJob {
DISALLOW_COPY_AND_ASSIGN(DriveURLRequestJob);
};
DriveURLRequestJob::DriveURLRequestJob(net::URLRequest* request,
DriveURLRequestJob::DriveURLRequestJob(void* profile_id,
net::URLRequest* request,
net::NetworkDelegate* network_delegate)
: net::URLRequestJob(request, network_delegate),
profile_id_(profile_id),
file_system_(NULL),
error_(false),
headers_set_(false),
......@@ -292,7 +303,7 @@ void DriveURLRequestJob::Start() {
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::UI,
FROM_HERE,
base::Bind(&GetFileSystemOnUIThread),
base::Bind(&GetFileSystemOnUIThread, profile_id_),
base::Bind(&DriveURLRequestJob::StartAsync,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -321,6 +332,7 @@ void DriveURLRequestJob::Kill() {
BrowserThread::UI,
FROM_HERE,
base::Bind(&CancelDriveDownloadOnUIThread,
profile_id_,
drive_file_path_));
}
......@@ -909,7 +921,8 @@ void DriveURLRequestJob::HeadersCompleted(int status_code,
///////////////////////////////////////////////////////////////////////////////
// DriveProtocolHandler class
DriveProtocolHandler::DriveProtocolHandler() {
DriveProtocolHandler::DriveProtocolHandler(void* profile_id)
: profile_id_(profile_id) {
}
DriveProtocolHandler::~DriveProtocolHandler() {
......@@ -918,7 +931,7 @@ DriveProtocolHandler::~DriveProtocolHandler() {
net::URLRequestJob* DriveProtocolHandler::MaybeCreateJob(
net::URLRequest* request, net::NetworkDelegate* network_delegate) const {
DVLOG(1) << "Handling url: " << request->url().spec();
return new DriveURLRequestJob(request, network_delegate);
return new DriveURLRequestJob(profile_id_, request, network_delegate);
}
} // namespace drive
......@@ -11,12 +11,15 @@ namespace drive {
class DriveProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
public:
DriveProtocolHandler();
explicit DriveProtocolHandler(void* profile_id);
virtual ~DriveProtocolHandler();
// Creates URLRequestJobs for drive:// URLs.
virtual net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE;
// The profile for processing Drive accesses. Should not be NULL.
void* profile_id_;
};
} // namespace drive
......
......@@ -639,9 +639,10 @@ scoped_ptr<net::URLRequestJobFactory> ProfileIOData::SetUpJobFactoryDefaults(
chrome::kDataScheme, new net::DataProtocolHandler());
DCHECK(set_protocol);
#if defined(OS_CHROMEOS)
if (!is_incognito()) {
if (!is_incognito() && profile_params_.get()) {
set_protocol = job_factory->SetProtocolHandler(
chrome::kDriveScheme, new drive::DriveProtocolHandler());
chrome::kDriveScheme,
new drive::DriveProtocolHandler(profile_params_->profile));
DCHECK(set_protocol);
}
#endif // defined(OS_CHROMEOS)
......
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