Commit db107487 authored by kinaba@chromium.org's avatar kinaba@chromium.org

Enable Drive integration service in incognito windows.

BUG=359418

Review URL: https://codereview.chromium.org/306023012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275415 0039d316-1c4b-4281-b951-d872f2087c98
parent 42c7cbbf
...@@ -141,11 +141,18 @@ void DownloadHandler::Initialize( ...@@ -141,11 +141,18 @@ void DownloadHandler::Initialize(
download_manager->GetAllDownloads(&downloads); download_manager->GetAllDownloads(&downloads);
for (size_t i = 0; i < downloads.size(); ++i) { for (size_t i = 0; i < downloads.size(); ++i) {
if (IsPersistedDriveDownload(drive_tmp_download_path_, downloads[i])) if (IsPersistedDriveDownload(drive_tmp_download_path_, downloads[i]))
RemoveDownload(downloads[i]->GetId()); downloads[i]->Remove();
} }
} }
} }
void DownloadHandler::ObserveIncognitoDownloadManager(
DownloadManager* download_manager) {
DCHECK(!notifier_incognito_);
notifier_incognito_.reset(new AllDownloadItemNotifier(download_manager,
this));
}
void DownloadHandler::SubstituteDriveDownloadPath( void DownloadHandler::SubstituteDriveDownloadPath(
const base::FilePath& drive_path, const base::FilePath& drive_path,
content::DownloadItem* download, content::DownloadItem* download,
...@@ -223,12 +230,13 @@ void DownloadHandler::OnDownloadCreated(DownloadManager* manager, ...@@ -223,12 +230,13 @@ void DownloadHandler::OnDownloadCreated(DownloadManager* manager,
FROM_HERE, FROM_HERE,
base::Bind(&DownloadHandler::RemoveDownload, base::Bind(&DownloadHandler::RemoveDownload,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
static_cast<void*>(manager),
download->GetId())); download->GetId()));
} }
} }
void DownloadHandler::RemoveDownload(int id) { void DownloadHandler::RemoveDownload(void* manager_id, int id) {
DownloadManager* manager = notifier_->GetManager(); DownloadManager* manager = GetDownloadManager(manager_id);
if (!manager) if (!manager)
return; return;
DownloadItem* download = manager->GetDownload(id); DownloadItem* download = manager->GetDownload(id);
...@@ -253,7 +261,7 @@ void DownloadHandler::OnDownloadUpdated( ...@@ -253,7 +261,7 @@ void DownloadHandler::OnDownloadUpdated(
break; break;
case DownloadItem::COMPLETE: case DownloadItem::COMPLETE:
UploadDownloadItem(download); UploadDownloadItem(manager, download);
data->set_complete(); data->set_complete();
break; break;
...@@ -289,7 +297,8 @@ void DownloadHandler::OnCreateDirectory( ...@@ -289,7 +297,8 @@ void DownloadHandler::OnCreateDirectory(
} }
} }
void DownloadHandler::UploadDownloadItem(DownloadItem* download) { void DownloadHandler::UploadDownloadItem(DownloadManager* manager,
DownloadItem* download) {
DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); DCHECK_EQ(DownloadItem::COMPLETE, download->GetState());
base::FilePath* cache_file_path = new base::FilePath; base::FilePath* cache_file_path = new base::FilePath;
WriteOnCacheFileAndReply( WriteOnCacheFileAndReply(
...@@ -300,16 +309,18 @@ void DownloadHandler::UploadDownloadItem(DownloadItem* download) { ...@@ -300,16 +309,18 @@ void DownloadHandler::UploadDownloadItem(DownloadItem* download) {
cache_file_path), cache_file_path),
base::Bind(&DownloadHandler::SetCacheFilePath, base::Bind(&DownloadHandler::SetCacheFilePath,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
static_cast<void*>(manager),
download->GetId(), download->GetId(),
base::Owned(cache_file_path))); base::Owned(cache_file_path)));
} }
void DownloadHandler::SetCacheFilePath(int id, void DownloadHandler::SetCacheFilePath(void* manager_id,
int id,
const base::FilePath* cache_file_path, const base::FilePath* cache_file_path,
FileError error) { FileError error) {
if (error != FILE_ERROR_OK) if (error != FILE_ERROR_OK)
return; return;
DownloadManager* manager = notifier_->GetManager(); DownloadManager* manager = GetDownloadManager(manager_id);
if (!manager) if (!manager)
return; return;
DownloadItem* download = manager->GetDownload(id); DownloadItem* download = manager->GetDownload(id);
...@@ -321,5 +332,12 @@ void DownloadHandler::SetCacheFilePath(int id, ...@@ -321,5 +332,12 @@ void DownloadHandler::SetCacheFilePath(int id,
data->set_cache_file_path(*cache_file_path); data->set_cache_file_path(*cache_file_path);
} }
DownloadManager* DownloadHandler::GetDownloadManager(void* manager_id) {
if (manager_id == notifier_->GetManager())
return notifier_->GetManager();
if (notifier_incognito_ && manager_id == notifier_incognito_->GetManager())
return notifier_incognito_->GetManager();
return NULL;
}
} // namespace drive } // namespace drive
...@@ -37,6 +37,12 @@ class DownloadHandler : public AllDownloadItemNotifier::Observer { ...@@ -37,6 +37,12 @@ class DownloadHandler : public AllDownloadItemNotifier::Observer {
void Initialize(content::DownloadManager* download_manager, void Initialize(content::DownloadManager* download_manager,
const base::FilePath& drive_tmp_download_path); const base::FilePath& drive_tmp_download_path);
// In addition to the DownloadManager passed to Initialize(), observe another
// download manager. This should be called only for the DownloadManager of the
// incognito version of the profile where |file_system_| resides.
void ObserveIncognitoDownloadManager(
content::DownloadManager* download_manager);
// Callback used to return results from SubstituteDriveDownloadPath. // Callback used to return results from SubstituteDriveDownloadPath.
// TODO(hashimoto): Report error with a FileError. crbug.com/171345 // TODO(hashimoto): Report error with a FileError. crbug.com/171345
typedef base::Callback<void(const base::FilePath&)> typedef base::Callback<void(const base::FilePath&)>
...@@ -75,7 +81,7 @@ class DownloadHandler : public AllDownloadItemNotifier::Observer { ...@@ -75,7 +81,7 @@ class DownloadHandler : public AllDownloadItemNotifier::Observer {
content::DownloadItem* download) OVERRIDE; content::DownloadItem* download) OVERRIDE;
// Removes the download. // Removes the download.
void RemoveDownload(int id); void RemoveDownload(void* manager_id, int id);
// Callback for FileSystem::CreateDirectory(). // Callback for FileSystem::CreateDirectory().
// Used to implement SubstituteDriveDownloadPath(). // Used to implement SubstituteDriveDownloadPath().
...@@ -83,16 +89,24 @@ class DownloadHandler : public AllDownloadItemNotifier::Observer { ...@@ -83,16 +89,24 @@ class DownloadHandler : public AllDownloadItemNotifier::Observer {
FileError error); FileError error);
// Starts the upload of a downloaded/downloading file. // Starts the upload of a downloaded/downloading file.
void UploadDownloadItem(content::DownloadItem* download); void UploadDownloadItem(content::DownloadManager* manager,
content::DownloadItem* download);
// Sets |cache_file_path| as user data of the download item specified by |id|. // Sets |cache_file_path| as user data of the download item specified by |id|.
void SetCacheFilePath(int id, void SetCacheFilePath(void* manager_id,
int id,
const base::FilePath* cache_file_path, const base::FilePath* cache_file_path,
FileError error); FileError error);
// Gets a download manager, given a |manager_id| casted from the pointer to
// the manager. This is used to validate the manager that may be deleted while
// asynchronous task posting. Returns NULL if the manager is already gone.
content::DownloadManager* GetDownloadManager(void* manager_id);
FileSystemInterface* file_system_; // Owned by DriveIntegrationService. FileSystemInterface* file_system_; // Owned by DriveIntegrationService.
// Observe the DownloadManager for new downloads. // Observe the DownloadManager for new downloads.
scoped_ptr<AllDownloadItemNotifier> notifier_; scoped_ptr<AllDownloadItemNotifier> notifier_;
scoped_ptr<AllDownloadItemNotifier> notifier_incognito_;
// Temporary download location directory. // Temporary download location directory.
base::FilePath drive_tmp_download_path_; base::FilePath drive_tmp_download_path_;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/drive/debug_info_collector.h" #include "chrome/browser/chromeos/drive/debug_info_collector.h"
#include "chrome/browser/chromeos/drive/download_handler.h" #include "chrome/browser/chromeos/drive/download_handler.h"
#include "chrome/browser/chromeos/drive/file_cache.h" #include "chrome/browser/chromeos/drive/file_cache.h"
...@@ -30,6 +31,7 @@ ...@@ -30,6 +31,7 @@
#include "chrome/browser/drive/drive_notification_manager.h" #include "chrome/browser/drive/drive_notification_manager.h"
#include "chrome/browser/drive/drive_notification_manager_factory.h" #include "chrome/browser/drive/drive_notification_manager_factory.h"
#include "chrome/browser/drive/event_logger.h" #include "chrome/browser/drive/event_logger.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h" #include "chrome/browser/signin/signin_manager_factory.h"
...@@ -40,6 +42,7 @@ ...@@ -40,6 +42,7 @@
#include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/common/user_agent.h" #include "content/public/common/user_agent.h"
#include "google_apis/drive/auth_service.h" #include "google_apis/drive/auth_service.h"
#include "google_apis/drive/gdata_wapi_url_generator.h" #include "google_apis/drive/gdata_wapi_url_generator.h"
...@@ -221,6 +224,7 @@ DriveIntegrationService::DriveIntegrationService( ...@@ -221,6 +224,7 @@ DriveIntegrationService::DriveIntegrationService(
test_cache_root : util::GetCacheRootPath(profile)), test_cache_root : util::GetCacheRootPath(profile)),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile && !profile->IsOffTheRecord());
logger_.reset(new EventLogger); logger_.reset(new EventLogger);
base::SequencedWorkerPool* blocking_pool = BrowserThread::GetBlockingPool(); base::SequencedWorkerPool* blocking_pool = BrowserThread::GetBlockingPool();
...@@ -508,6 +512,7 @@ void DriveIntegrationService::InitializeAfterMetadataInitialized( ...@@ -508,6 +512,7 @@ void DriveIntegrationService::InitializeAfterMetadataInitialized(
return; return;
} }
// Initialize Download Handler for hooking downloads to the Drive folder.
content::DownloadManager* download_manager = content::DownloadManager* download_manager =
g_browser_process->download_status_updater() ? g_browser_process->download_status_updater() ?
BrowserContext::GetDownloadManager(profile_) : NULL; BrowserContext::GetDownloadManager(profile_) : NULL;
...@@ -515,6 +520,21 @@ void DriveIntegrationService::InitializeAfterMetadataInitialized( ...@@ -515,6 +520,21 @@ void DriveIntegrationService::InitializeAfterMetadataInitialized(
download_manager, download_manager,
cache_root_directory_.Append(kTemporaryFileDirectory)); cache_root_directory_.Append(kTemporaryFileDirectory));
// Install the handler also to incognito profile.
if (g_browser_process->download_status_updater()) {
if (profile_->HasOffTheRecordProfile()) {
download_handler_->ObserveIncognitoDownloadManager(
BrowserContext::GetDownloadManager(
profile_->GetOffTheRecordProfile()));
} else {
profile_notification_registrar_.reset(new content::NotificationRegistrar);
profile_notification_registrar_->Add(
this,
chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
}
}
// Register for Google Drive invalidation notifications. // Register for Google Drive invalidation notifications.
DriveNotificationManager* drive_notification_manager = DriveNotificationManager* drive_notification_manager =
DriveNotificationManagerFactory::GetForBrowserContext(profile_); DriveNotificationManagerFactory::GetForBrowserContext(profile_);
...@@ -548,6 +568,24 @@ void DriveIntegrationService::AvoidDriveAsDownloadDirecotryPreference() { ...@@ -548,6 +568,24 @@ void DriveIntegrationService::AvoidDriveAsDownloadDirecotryPreference() {
} }
} }
void DriveIntegrationService::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (type == chrome::NOTIFICATION_PROFILE_CREATED) {
Profile* created_profile = content::Source<Profile>(source).ptr();
if (created_profile->IsOffTheRecord() &&
created_profile->IsSameProfile(profile_)) {
download_handler_->ObserveIncognitoDownloadManager(
BrowserContext::GetDownloadManager(created_profile));
profile_notification_registrar_->Remove(
this,
chrome::NOTIFICATION_PROFILE_CREATED,
content::NotificationService::AllSources());
}
}
}
//===================== DriveIntegrationServiceFactory ======================= //===================== DriveIntegrationServiceFactory =======================
DriveIntegrationServiceFactory::FactoryCallback* DriveIntegrationServiceFactory::FactoryCallback*
...@@ -607,6 +645,11 @@ DriveIntegrationServiceFactory::DriveIntegrationServiceFactory() ...@@ -607,6 +645,11 @@ DriveIntegrationServiceFactory::DriveIntegrationServiceFactory()
DriveIntegrationServiceFactory::~DriveIntegrationServiceFactory() { DriveIntegrationServiceFactory::~DriveIntegrationServiceFactory() {
} }
content::BrowserContext* DriveIntegrationServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
KeyedService* DriveIntegrationServiceFactory::BuildServiceInstanceFor( KeyedService* DriveIntegrationServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const { content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context); Profile* profile = Profile::FromBrowserContext(context);
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#include "chrome/browser/drive/drive_notification_observer.h" #include "chrome/browser/drive/drive_notification_observer.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
namespace base { namespace base {
class FilePath; class FilePath;
...@@ -63,7 +65,8 @@ class DriveIntegrationServiceObserver { ...@@ -63,7 +65,8 @@ class DriveIntegrationServiceObserver {
// that are used to integrate Drive to Chrome. The object of this class is // that are used to integrate Drive to Chrome. The object of this class is
// created per-profile. // created per-profile.
class DriveIntegrationService : public KeyedService, class DriveIntegrationService : public KeyedService,
public DriveNotificationObserver { public DriveNotificationObserver,
public content::NotificationObserver {
public: public:
class PreferenceWatcher; class PreferenceWatcher;
...@@ -149,6 +152,11 @@ class DriveIntegrationService : public KeyedService, ...@@ -149,6 +152,11 @@ class DriveIntegrationService : public KeyedService,
// destination is set under Drive. This must be called when disabling Drive. // destination is set under Drive. This must be called when disabling Drive.
void AvoidDriveAsDownloadDirecotryPreference(); void AvoidDriveAsDownloadDirecotryPreference();
// content::NotificationObserver overrides.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
friend class DriveIntegrationServiceFactory; friend class DriveIntegrationServiceFactory;
Profile* profile_; Profile* profile_;
...@@ -174,6 +182,7 @@ class DriveIntegrationService : public KeyedService, ...@@ -174,6 +182,7 @@ class DriveIntegrationService : public KeyedService,
ObserverList<DriveIntegrationServiceObserver> observers_; ObserverList<DriveIntegrationServiceObserver> observers_;
scoped_ptr<PreferenceWatcher> preference_watcher_; scoped_ptr<PreferenceWatcher> preference_watcher_;
scoped_ptr<content::NotificationRegistrar> profile_notification_registrar_;
// Note: This should remain the last member so it'll be destroyed and // Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed. // invalidate its weak pointers before any other members are destroyed.
...@@ -223,7 +232,9 @@ class DriveIntegrationServiceFactory ...@@ -223,7 +232,9 @@ class DriveIntegrationServiceFactory
DriveIntegrationServiceFactory(); DriveIntegrationServiceFactory();
virtual ~DriveIntegrationServiceFactory(); virtual ~DriveIntegrationServiceFactory();
// BrowserContextKeyedServiceFactory: // BrowserContextKeyedServiceFactory overrides.
virtual content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const OVERRIDE;
virtual KeyedService* BuildServiceInstanceFor( virtual KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const OVERRIDE; content::BrowserContext* context) const OVERRIDE;
......
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