Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / local / local_file_sync_service.cc
index 775f28b..e7501e7 100644 (file)
@@ -5,8 +5,7 @@
 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
 
 #include "base/stl_util.h"
-#include "chrome/browser/extensions/extension_service.h"
-#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/browser/extensions/extension_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/sync_file_system/file_change.h"
 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
@@ -19,6 +18,8 @@
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/site_instance.h"
 #include "content/public/browser/storage_partition.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/common/extension_set.h"
 #include "url/gurl.h"
 #include "webkit/browser/fileapi/file_system_context.h"
 #include "webkit/browser/fileapi/file_system_url.h"
@@ -96,16 +97,15 @@ void LocalFileSyncService::OriginChangeMap::SetOriginEnabled(
 
 // LocalFileSyncService -------------------------------------------------------
 
-LocalFileSyncService::LocalFileSyncService(Profile* profile)
-    : profile_(profile),
-      sync_context_(new LocalFileSyncContext(
-          profile_->GetPath(),
-          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
-          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)
-              .get())),
-      local_change_processor_(NULL) {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-  sync_context_->AddOriginChangeObserver(this);
+scoped_ptr<LocalFileSyncService> LocalFileSyncService::Create(
+    Profile* profile) {
+  return make_scoped_ptr(new LocalFileSyncService(profile, NULL));
+}
+
+scoped_ptr<LocalFileSyncService> LocalFileSyncService::CreateForTesting(
+    Profile* profile,
+    leveldb::Env* env) {
+  return make_scoped_ptr(new LocalFileSyncService(profile, env));
 }
 
 LocalFileSyncService::~LocalFileSyncService() {
@@ -141,7 +141,6 @@ void LocalFileSyncService::RegisterURLForWaitingSync(
 
 void LocalFileSyncService::ProcessLocalChange(
     const SyncFileCallback& callback) {
-  DCHECK(local_change_processor_);
   // Pick an origin to process next.
   GURL origin;
   if (!origin_change_map_.NextOriginToProcess(&origin)) {
@@ -163,8 +162,13 @@ void LocalFileSyncService::ProcessLocalChange(
 }
 
 void LocalFileSyncService::SetLocalChangeProcessor(
-    LocalChangeProcessor* processor) {
-  local_change_processor_ = processor;
+    LocalChangeProcessor* local_change_processor) {
+  local_change_processor_ = local_change_processor;
+}
+
+void LocalFileSyncService::SetLocalChangeProcessorCallback(
+    const GetLocalChangeProcessorCallback& get_local_change_processor) {
+  get_local_change_processor_ = get_local_change_processor;
 }
 
 void LocalFileSyncService::HasPendingLocalChanges(
@@ -180,6 +184,12 @@ void LocalFileSyncService::HasPendingLocalChanges(
       origin_to_contexts_[url.origin()], url, callback);
 }
 
+void LocalFileSyncService::PromoteDemotedChanges() {
+  for (OriginToContext::iterator iter = origin_to_contexts_.begin();
+       iter != origin_to_contexts_.end(); ++iter)
+    sync_context_->PromoteDemotedChanges(iter->first, iter->second);
+}
+
 void LocalFileSyncService::GetLocalFileMetadata(
     const FileSystemURL& url, const SyncFileMetadataCallback& callback) {
   DCHECK(ContainsKey(origin_to_contexts_, url.origin()));
@@ -197,10 +207,9 @@ void LocalFileSyncService::PrepareForProcessRemoteChange(
     // been initialized in this service.
     DCHECK(profile_);
     // The given url.origin() must be for valid installed app.
-    ExtensionService* extension_service =
-        extensions::ExtensionSystem::Get(profile_)->extension_service();
-    const extensions::Extension* extension = extension_service->GetInstalledApp(
-        url.origin());
+    const extensions::Extension* extension =
+        extensions::ExtensionRegistry::Get(profile_)
+            ->enabled_extensions().GetAppByURL(url.origin());
     if (!extension) {
       util::Log(
           logging::LOG_WARNING,
@@ -214,7 +223,8 @@ void LocalFileSyncService::PrepareForProcessRemoteChange(
                    SyncFileMetadata(), FileChangeList());
       return;
     }
-    GURL site_url = extension_service->GetSiteForExtensionId(extension->id());
+    GURL site_url =
+        extensions::util::GetSiteForExtensionId(extension->id(), profile_);
     DCHECK(!site_url.is_empty());
     scoped_refptr<fileapi::FileSystemContext> file_system_context =
         content::BrowserContext::GetStoragePartitionForSite(
@@ -243,9 +253,16 @@ void LocalFileSyncService::ApplyRemoteChange(
     const FileSystemURL& url,
     const SyncStatusCallback& callback) {
   DCHECK(ContainsKey(origin_to_contexts_, url.origin()));
+  util::Log(logging::LOG_VERBOSE, FROM_HERE,
+            "[Remote -> Local] ApplyRemoteChange: %s on %s",
+            change.DebugString().c_str(),
+            url.DebugString().c_str());
+
   sync_context_->ApplyRemoteChange(
       origin_to_contexts_[url.origin()],
-      change, local_path, url, callback);
+      change, local_path, url,
+      base::Bind(&LocalFileSyncService::DidApplyRemoteChange, AsWeakPtr(),
+                 callback));
 }
 
 void LocalFileSyncService::FinalizeRemoteSync(
@@ -303,6 +320,20 @@ void LocalFileSyncService::SetOriginEnabled(const GURL& origin, bool enabled) {
   origin_change_map_.SetOriginEnabled(origin, enabled);
 }
 
+LocalFileSyncService::LocalFileSyncService(Profile* profile,
+                                           leveldb::Env* env_override)
+    : profile_(profile),
+      sync_context_(new LocalFileSyncContext(
+          profile_->GetPath(),
+          env_override,
+          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
+          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)
+              .get())),
+      local_change_processor_(NULL) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  sync_context_->AddOriginChangeObserver(this);
+}
+
 void LocalFileSyncService::DidInitializeFileSystemContext(
     const GURL& app_origin,
     fileapi::FileSystemContext* file_system_context,
@@ -359,6 +390,15 @@ void LocalFileSyncService::RunLocalSyncCallback(
   callback.Run(status, url);
 }
 
+void LocalFileSyncService::DidApplyRemoteChange(
+    const SyncStatusCallback& callback,
+    SyncStatusCode status) {
+  util::Log(logging::LOG_VERBOSE, FROM_HERE,
+            "[Remote -> Local] ApplyRemoteChange finished --> %s",
+            SyncStatusCodeToString(status));
+  callback.Run(status);
+}
+
 void LocalFileSyncService::DidGetFileForLocalSync(
     SyncStatusCode status,
     const LocalFileSyncInfo& sync_file_info,
@@ -380,7 +420,7 @@ void LocalFileSyncService::DidGetFileForLocalSync(
   DVLOG(1) << "ProcessLocalChange: " << sync_file_info.url.DebugString()
            << " change:" << next_change.DebugString();
 
-  local_change_processor_->ApplyLocalChange(
+  GetLocalChangeProcessor(sync_file_info.url)->ApplyLocalChange(
       next_change,
       sync_file_info.local_file_path,
       sync_file_info.metadata,
@@ -402,7 +442,7 @@ void LocalFileSyncService::ProcessNextChangeForURL(
            << " status:" << status;
 
   if (status == SYNC_STATUS_RETRY) {
-    local_change_processor_->ApplyLocalChange(
+    GetLocalChangeProcessor(sync_file_info.url)->ApplyLocalChange(
         processed_change,
         sync_file_info.local_file_path,
         sync_file_info.metadata,
@@ -430,7 +470,7 @@ void LocalFileSyncService::ProcessNextChangeForURL(
   }
 
   FileChange next_change = changes.front();
-  local_change_processor_->ApplyLocalChange(
+  GetLocalChangeProcessor(url)->ApplyLocalChange(
       changes.front(),
       sync_file_info.local_file_path,
       sync_file_info.metadata,
@@ -440,4 +480,12 @@ void LocalFileSyncService::ProcessNextChangeForURL(
                  next_change, changes.PopAndGetNewList()));
 }
 
+LocalChangeProcessor* LocalFileSyncService::GetLocalChangeProcessor(
+    const FileSystemURL& url) {
+  if (!get_local_change_processor_.is_null())
+    return get_local_change_processor_.Run(url.origin());
+  DCHECK(local_change_processor_);
+  return local_change_processor_;
+}
+
 }  // namespace sync_file_system