Commit f078efc2 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Migrate ArcService to BrowserContextKeyedService part 7.

This CL migrates ArcDownloadsWatcherService.

BUG=672829
TEST=Ran try.

Change-Id: I1004dfa57616778463f36e86c3cc46295d8c9627
Reviewed-on: https://chromium-review.googlesource.com/571567Reviewed-by: default avatarLuis Hector Chavez <lhchavez@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487087}
parent bb01bd21
...@@ -94,8 +94,6 @@ void ArcServiceLauncher::Initialize() { ...@@ -94,8 +94,6 @@ void ArcServiceLauncher::Initialize() {
base::Bind(ArcSession::Create, arc_bridge_service))); base::Bind(ArcSession::Create, arc_bridge_service)));
// List in lexicographical order. // List in lexicographical order.
arc_service_manager_->AddService(
base::MakeUnique<ArcDownloadsWatcherService>(arc_bridge_service));
arc_service_manager_->AddService( arc_service_manager_->AddService(
base::MakeUnique<ArcImeService>(arc_bridge_service)); base::MakeUnique<ArcImeService>(arc_bridge_service));
arc_service_manager_->AddService( arc_service_manager_->AddService(
...@@ -181,6 +179,7 @@ void ArcServiceLauncher::OnPrimaryUserProfilePrepared(Profile* profile) { ...@@ -181,6 +179,7 @@ void ArcServiceLauncher::OnPrimaryUserProfilePrepared(Profile* profile) {
ArcBootErrorNotification::GetForBrowserContext(profile); ArcBootErrorNotification::GetForBrowserContext(profile);
ArcClipboardBridge::GetForBrowserContext(profile); ArcClipboardBridge::GetForBrowserContext(profile);
ArcCrashCollectorBridge::GetForBrowserContext(profile); ArcCrashCollectorBridge::GetForBrowserContext(profile);
ArcDownloadsWatcherService::GetForBrowserContext(profile);
ArcEnterpriseReportingService::GetForBrowserContext(profile); ArcEnterpriseReportingService::GetForBrowserContext(profile);
ArcFileSystemMounter::GetForBrowserContext(profile); ArcFileSystemMounter::GetForBrowserContext(profile);
ArcMetricsService::GetForBrowserContext(profile); ArcMetricsService::GetForBrowserContext(profile);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_path_watcher.h" #include "base/files/file_path_watcher.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/memory/singleton.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
...@@ -24,9 +25,10 @@ ...@@ -24,9 +25,10 @@
#include "base/threading/sequenced_task_runner_handle.h" #include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/download/download_prefs.h" #include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "components/arc/arc_bridge_service.h" #include "components/arc/arc_bridge_service.h"
#include "components/arc/arc_browser_context_keyed_service_factory_base.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
using content::BrowserThread; using content::BrowserThread;
...@@ -167,6 +169,25 @@ std::pair<base::TimeTicks, TimestampMap> BuildTimestampMapCallback( ...@@ -167,6 +169,25 @@ std::pair<base::TimeTicks, TimestampMap> BuildTimestampMapCallback(
return std::make_pair(snapshot_time, std::move(current_timestamp_map)); return std::make_pair(snapshot_time, std::move(current_timestamp_map));
} }
// Singleton factory for ArcDownloadsWatcherService.
class ArcDownloadsWatcherServiceFactory
: public internal::ArcBrowserContextKeyedServiceFactoryBase<
ArcDownloadsWatcherService,
ArcDownloadsWatcherServiceFactory> {
public:
// Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
static constexpr const char* kName = "ArcDownloadsWatcherServiceFactory";
static ArcDownloadsWatcherServiceFactory* GetInstance() {
return base::Singleton<ArcDownloadsWatcherServiceFactory>::get();
}
private:
friend base::DefaultSingletonTraits<ArcDownloadsWatcherServiceFactory>;
ArcDownloadsWatcherServiceFactory() = default;
~ArcDownloadsWatcherServiceFactory() override = default;
};
} // namespace } // namespace
bool HasAndroidSupportedMediaExtension(const base::FilePath& path) { bool HasAndroidSupportedMediaExtension(const base::FilePath& path) {
...@@ -190,7 +211,7 @@ class ArcDownloadsWatcherService::DownloadsWatcher { ...@@ -190,7 +211,7 @@ class ArcDownloadsWatcherService::DownloadsWatcher {
public: public:
using Callback = base::Callback<void(const std::vector<std::string>& paths)>; using Callback = base::Callback<void(const std::vector<std::string>& paths)>;
explicit DownloadsWatcher(const Callback& callback); DownloadsWatcher(content::BrowserContext* context, const Callback& callback);
~DownloadsWatcher(); ~DownloadsWatcher();
// Starts watching Downloads directory. // Starts watching Downloads directory.
...@@ -230,6 +251,7 @@ class ArcDownloadsWatcherService::DownloadsWatcher { ...@@ -230,6 +251,7 @@ class ArcDownloadsWatcherService::DownloadsWatcher {
}; };
ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher( ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher(
content::BrowserContext* context,
const Callback& callback) const Callback& callback)
: callback_(callback), : callback_(callback),
last_notify_time_(base::TimeTicks()), last_notify_time_(base::TimeTicks()),
...@@ -238,7 +260,7 @@ ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher( ...@@ -238,7 +260,7 @@ ArcDownloadsWatcherService::DownloadsWatcher::DownloadsWatcher(
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DETACH_FROM_SEQUENCE(sequence_checker_); DETACH_FROM_SEQUENCE(sequence_checker_);
downloads_dir_ = DownloadPrefs(ProfileManager::GetActiveUserProfile()) downloads_dir_ = DownloadPrefs(Profile::FromBrowserContext(context))
.GetDefaultDownloadDirectoryForProfile() .GetDefaultDownloadDirectoryForProfile()
.StripTrailingSeparators(); .StripTrailingSeparators();
} }
...@@ -314,21 +336,36 @@ void ArcDownloadsWatcherService::DownloadsWatcher::OnBuildTimestampMap( ...@@ -314,21 +336,36 @@ void ArcDownloadsWatcherService::DownloadsWatcher::OnBuildTimestampMap(
outstanding_task_ = false; outstanding_task_ = false;
} }
// static
ArcDownloadsWatcherService* ArcDownloadsWatcherService::GetForBrowserContext(
content::BrowserContext* context) {
return ArcDownloadsWatcherServiceFactory::GetForBrowserContext(context);
}
ArcDownloadsWatcherService::ArcDownloadsWatcherService( ArcDownloadsWatcherService::ArcDownloadsWatcherService(
content::BrowserContext* context,
ArcBridgeService* bridge_service) ArcBridgeService* bridge_service)
: ArcService(bridge_service), : context_(context),
arc_bridge_service_(bridge_service),
file_task_runner_( file_task_runner_(
base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})), base::CreateSequencedTaskRunnerWithTraits({base::MayBlock()})),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
arc_bridge_service()->file_system()->AddObserver(this); arc_bridge_service_->file_system()->AddObserver(this);
} }
ArcDownloadsWatcherService::~ArcDownloadsWatcherService() { ArcDownloadsWatcherService::~ArcDownloadsWatcherService() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
arc_bridge_service()->file_system()->RemoveObserver(this);
StopWatchingDownloads(); StopWatchingDownloads();
DCHECK(!watcher_); DCHECK(!watcher_);
// TODO(hidehiko): Currently, the lifetime of ArcBridgeService and
// BrowserContextKeyedService is not nested.
// If ArcServiceManager::Get() returns nullptr, it is already destructed,
// so do not touch it.
if (ArcServiceManager::Get())
arc_bridge_service_->file_system()->RemoveObserver(this);
} }
void ArcDownloadsWatcherService::OnInstanceReady() { void ArcDownloadsWatcherService::OnInstanceReady() {
...@@ -346,7 +383,7 @@ void ArcDownloadsWatcherService::StartWatchingDownloads() { ...@@ -346,7 +383,7 @@ void ArcDownloadsWatcherService::StartWatchingDownloads() {
StopWatchingDownloads(); StopWatchingDownloads();
DCHECK(!watcher_); DCHECK(!watcher_);
watcher_ = base::MakeUnique<DownloadsWatcher>( watcher_ = base::MakeUnique<DownloadsWatcher>(
base::Bind(&ArcDownloadsWatcherService::OnDownloadsChanged, context_, base::Bind(&ArcDownloadsWatcherService::OnDownloadsChanged,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
file_task_runner_->PostTask(FROM_HERE, file_task_runner_->PostTask(FROM_HERE,
base::BindOnce(&DownloadsWatcher::Start, base::BindOnce(&DownloadsWatcher::Start,
...@@ -364,7 +401,7 @@ void ArcDownloadsWatcherService::OnDownloadsChanged( ...@@ -364,7 +401,7 @@ void ArcDownloadsWatcherService::OnDownloadsChanged(
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto* instance = ARC_GET_INSTANCE_FOR_METHOD( auto* instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_bridge_service()->file_system(), RequestMediaScan); arc_bridge_service_->file_system(), RequestMediaScan);
if (!instance) if (!instance)
return; return;
instance->RequestMediaScan(paths); instance->RequestMediaScan(paths);
......
...@@ -12,15 +12,19 @@ ...@@ -12,15 +12,19 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "components/arc/arc_service.h"
#include "components/arc/common/file_system.mojom.h" #include "components/arc/common/file_system.mojom.h"
#include "components/arc/instance_holder.h" #include "components/arc/instance_holder.h"
#include "components/keyed_service/core/keyed_service.h"
namespace base { namespace base {
class FilePath; class FilePath;
class SequencedTaskRunner; class SequencedTaskRunner;
} // namespace base } // namespace base
namespace content {
class BrowserContext;
} // namespace content
namespace arc { namespace arc {
class ArcBridgeService; class ArcBridgeService;
...@@ -31,10 +35,16 @@ bool HasAndroidSupportedMediaExtension(const base::FilePath& path); ...@@ -31,10 +35,16 @@ bool HasAndroidSupportedMediaExtension(const base::FilePath& path);
// Watches Downloads directory and registers newly created media files to // Watches Downloads directory and registers newly created media files to
// Android MediaProvider. // Android MediaProvider.
class ArcDownloadsWatcherService class ArcDownloadsWatcherService
: public ArcService, : public KeyedService,
public InstanceHolder<mojom::FileSystemInstance>::Observer { public InstanceHolder<mojom::FileSystemInstance>::Observer {
public: public:
explicit ArcDownloadsWatcherService(ArcBridgeService* bridge_service); // Returns singleton instance for the given BrowserContext,
// or nullptr if the browser |context| is not allowed to use ARC.
static ArcDownloadsWatcherService* GetForBrowserContext(
content::BrowserContext* context);
ArcDownloadsWatcherService(content::BrowserContext* context,
ArcBridgeService* bridge_service);
~ArcDownloadsWatcherService() override; ~ArcDownloadsWatcherService() override;
// InstanceHolder<mojom::FileSystemInstance>::Observer // InstanceHolder<mojom::FileSystemInstance>::Observer
...@@ -49,6 +59,9 @@ class ArcDownloadsWatcherService ...@@ -49,6 +59,9 @@ class ArcDownloadsWatcherService
void OnDownloadsChanged(const std::vector<std::string>& paths); void OnDownloadsChanged(const std::vector<std::string>& paths);
content::BrowserContext* const context_;
ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager.
std::unique_ptr<DownloadsWatcher> watcher_; std::unique_ptr<DownloadsWatcher> watcher_;
scoped_refptr<base::SequencedTaskRunner> file_task_runner_; scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
......
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