Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync_file_system/sync_file_system_service.h" | 5 #include "chrome/browser/sync_file_system/sync_file_system_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/stl_util.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_dependency_manager.h" | 12 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 12 #include "chrome/browser/sync_file_system/local_file_sync_service.h" | 13 #include "chrome/browser/sync_file_system/local_file_sync_service.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 15 #include "webkit/fileapi/file_system_context.h" | 16 #include "webkit/fileapi/file_system_context.h" |
| 17 #include "webkit/fileapi/file_system_url.h" | |
| 16 #include "webkit/fileapi/syncable/sync_status_code.h" | 18 #include "webkit/fileapi/syncable/sync_status_code.h" |
| 17 | 19 |
| 18 using content::BrowserThread; | 20 using content::BrowserThread; |
| 19 | 21 |
| 20 namespace sync_file_system { | 22 namespace sync_file_system { |
| 21 | 23 |
| 22 void SyncFileSystemService::Shutdown() { | 24 void SyncFileSystemService::Shutdown() { |
| 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 24 | 26 |
| 27 pending_providers_.clear(); | |
| 28 STLDeleteValues(&remote_file_providers_); | |
| 29 | |
| 25 local_file_service_->Shutdown(); | 30 local_file_service_->Shutdown(); |
| 26 local_file_service_.reset(); | 31 local_file_service_.reset(); |
| 27 | 32 |
| 28 profile_ = NULL; | 33 profile_ = NULL; |
| 29 } | 34 } |
| 30 | 35 |
| 31 SyncFileSystemService::~SyncFileSystemService() { | 36 SyncFileSystemService::~SyncFileSystemService() { |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 33 DCHECK(!profile_); | 38 DCHECK(!profile_); |
| 39 DCHECK(remote_file_providers_.empty()); | |
| 40 } | |
| 41 | |
| 42 void SyncFileSystemService::OnRemoteChangeAvailable( | |
| 43 RemoteFileProvider* remote_file_provider) { | |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 45 DCHECK(remote_file_provider); | |
| 46 | |
| 47 pending_providers_.insert(remote_file_provider); | |
| 48 MayStartRemoteSync(); | |
| 49 } | |
| 50 | |
| 51 void SyncFileSystemService::RegisterRemoteFileProvider( | |
| 52 const std::string& service_name, | |
| 53 scoped_ptr<RemoteFileProvider> remote_file_provider) { | |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 55 DCHECK(remote_file_provider); | |
| 56 | |
| 57 std::pair<RemoteFileProviderMap::iterator, bool> inserted | |
| 58 = remote_file_providers_.insert( | |
| 59 std::make_pair(service_name, remote_file_provider.get())); | |
| 60 if (!inserted.second) { | |
| 61 NOTREACHED(); | |
| 62 pending_providers_.erase(inserted.first->second); | |
| 63 delete inserted.first->second; | |
| 64 inserted.first->second = remote_file_provider.get(); | |
| 65 } | |
| 66 | |
| 67 remote_file_provider->SetObserver(this); | |
| 68 ignore_result(remote_file_provider.release()); | |
| 34 } | 69 } |
| 35 | 70 |
| 36 void SyncFileSystemService::InitializeForApp( | 71 void SyncFileSystemService::InitializeForApp( |
| 37 fileapi::FileSystemContext* file_system_context, | 72 fileapi::FileSystemContext* file_system_context, |
| 38 const std::string& service_name, | 73 const std::string& service_name, |
| 39 const GURL& app_url, | 74 const GURL& app_url, |
| 40 const StatusCallback& callback) { | 75 const StatusCallback& callback) { |
| 41 DCHECK(local_file_service_.get()); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 77 DCHECK(local_file_service_); | |
| 42 | 78 |
| 43 // TODO(kinuko,tzik): Instantiate the remote_file_service for the given | 79 // TODO(kinuko,tzik): Instantiate the remote_file_service for the given |
| 44 // |service_name| if it hasn't been initialized. | 80 // |service_name| if it hasn't been initialized. |
| 45 | 81 |
| 46 local_file_service_->MaybeInitializeFileSystemContext( | 82 local_file_service_->MaybeInitializeFileSystemContext( |
| 47 app_url, file_system_context, callback); | 83 app_url, file_system_context, callback); |
| 48 | 84 |
| 49 // TODO(tzik): Hook up remote service initialization code. | 85 DCHECK(ContainsKey(remote_file_providers_, service_name) && |
| 86 remote_file_providers_[service_name]); | |
| 87 remote_file_providers_[service_name]->RegisterOrigin(app_url.GetOrigin()); | |
| 50 } | 88 } |
| 51 | 89 |
| 52 SyncFileSystemService::SyncFileSystemService(Profile* profile) | 90 SyncFileSystemService::SyncFileSystemService(Profile* profile) |
| 53 : profile_(profile) {} | 91 : profile_(profile), |
| 92 sync_is_running_(false) {} | |
| 54 | 93 |
| 55 void SyncFileSystemService::Initialize( | 94 void SyncFileSystemService::Initialize( |
| 56 scoped_ptr<LocalFileSyncService> local_file_service) { | 95 scoped_ptr<LocalFileSyncService> local_file_service) { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 DCHECK(local_file_service.get()); | 97 DCHECK(local_file_service); |
| 59 DCHECK(profile_); | 98 DCHECK(profile_); |
| 60 | 99 |
| 61 local_file_service_ = local_file_service.Pass(); | 100 local_file_service_ = local_file_service.Pass(); |
| 62 } | 101 } |
| 63 | 102 |
| 103 void SyncFileSystemService::MayStartRemoteSync() { | |
|
kinuko
2012/10/17 09:22:57
May -> Maybe ?
tzik
2012/10/18 07:18:08
Done.
| |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 105 | |
| 106 if (sync_is_running_) | |
| 107 return; | |
| 108 | |
| 109 RemoteFileProvider* provider = NULL; | |
| 110 while (true) { | |
| 111 if (pending_providers_.empty()) | |
| 112 return; | |
| 113 provider = *pending_providers_.begin(); | |
| 114 if (provider->HasRemoteChange()) | |
| 115 break; | |
| 116 else | |
| 117 pending_providers_.erase(pending_providers_.begin()); | |
| 118 } | |
| 119 DCHECK(provider); | |
| 120 | |
| 121 sync_is_running_ = true; | |
| 122 | |
| 123 scoped_ptr<RemoteChange> change = provider->GetRemoteChange(); | |
| 124 | |
| 125 // TODO(tzik): Disable writing for |url|. | |
| 126 fileapi::FileSystemURL url = change->url(); | |
| 127 if (change->deleted()) { | |
| 128 provider->DeleteFile( | |
| 129 url, base::Bind(&SyncFileSystemService::DidDeleteFile, AsWeakPtr(), | |
| 130 url)); | |
| 131 } else { | |
| 132 provider->DownloadFile( | |
| 133 url, | |
| 134 base::Bind(&SyncFileSystemService::DidDownloadFile, AsWeakPtr(), | |
| 135 base::Passed(&change))); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 void SyncFileSystemService::DidDownloadFile( | |
| 140 scoped_ptr<RemoteChange> change, | |
| 141 fileapi::SyncStatusCode status, | |
| 142 const FilePath& file_path) { | |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 144 NOTIMPLEMENTED(); | |
| 145 | |
| 146 // TODO(tzik): Do follows on correct thread. | |
| 147 // On FILE thread: | |
| 148 // CopyInForeightFile(file_path, change->url()); | |
| 149 // DeleteFile(file_path); | |
| 150 // | |
| 151 // On UI thread: | |
| 152 // remote_file_provider->ChangeApplied(*change); | |
| 153 // EnableWriting(change->url); | |
| 154 // sync_is_running_ = false; | |
| 155 // MayStartRemoteSync(); | |
| 156 } | |
| 157 | |
| 158 void SyncFileSystemService::DidDeleteFile(const fileapi::FileSystemURL& url, | |
| 159 fileapi::SyncStatusCode status) { | |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 161 sync_is_running_ = false; | |
| 162 // Enable writing for |url|. | |
| 163 MayStartRemoteSync(); | |
| 164 } | |
| 165 | |
| 64 // SyncFileSystemServiceFactory ----------------------------------------------- | 166 // SyncFileSystemServiceFactory ----------------------------------------------- |
| 65 | 167 |
| 66 // static | 168 // static |
| 67 SyncFileSystemService* SyncFileSystemServiceFactory::GetForProfile( | 169 SyncFileSystemService* SyncFileSystemServiceFactory::GetForProfile( |
| 68 Profile* profile) { | 170 Profile* profile) { |
| 69 return static_cast<SyncFileSystemService*>( | 171 return static_cast<SyncFileSystemService*>( |
| 70 GetInstance()->GetServiceForProfile(profile, true)); | 172 GetInstance()->GetServiceForProfile(profile, true)); |
| 71 } | 173 } |
| 72 | 174 |
| 73 // static | 175 // static |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 85 ProfileKeyedService* SyncFileSystemServiceFactory::BuildServiceInstanceFor( | 187 ProfileKeyedService* SyncFileSystemServiceFactory::BuildServiceInstanceFor( |
| 86 Profile* profile) const { | 188 Profile* profile) const { |
| 87 SyncFileSystemService* service = new SyncFileSystemService(profile); | 189 SyncFileSystemService* service = new SyncFileSystemService(profile); |
| 88 | 190 |
| 89 // TODO(kinuko): Set up mock services if it is called for testing. | 191 // TODO(kinuko): Set up mock services if it is called for testing. |
| 90 | 192 |
| 91 scoped_ptr<LocalFileSyncService> local_file_service( | 193 scoped_ptr<LocalFileSyncService> local_file_service( |
| 92 new LocalFileSyncService); | 194 new LocalFileSyncService); |
| 93 | 195 |
| 94 service->Initialize(local_file_service.Pass()); | 196 service->Initialize(local_file_service.Pass()); |
| 197 | |
| 198 // TODO(tzik): Instantiate DriveFileProvider and register it to |service|. | |
| 199 | |
| 95 return service; | 200 return service; |
| 96 } | 201 } |
| 97 | 202 |
| 98 } // namespace chrome | 203 } // namespace chrome |
| OLD | NEW |