Revert 149576 - gdata: Make WeakPtrFactory the last parameter

Broke RemoteFileSystemExtensionApiTest.RemoteMountPoint

Remove WeakPtr members in favor of WeakPtrFactory::GetWeakPtr()
Along the way, copy derat's comment from ContactDatabase on this matter.

Wez on [chromium-dev] Weak ptrs for derived types?

You need to be similarly careful to have WeakPtrFactory appear after any
fields that its WeakPtrs may be used to access if you follow that pattern
instead.

BUG=none
TEST=none

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

TBR=satorux@chromium.org
Review URL: https://chromiumcodereview.appspot.com/10836069

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@149582 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/chromeos/gdata/gdata_auth_service.cc b/chrome/browser/chromeos/gdata/gdata_auth_service.cc
index cb5673a..2b335a9 100644
--- a/chrome/browser/chromeos/gdata/gdata_auth_service.cc
+++ b/chrome/browser/chromeos/gdata/gdata_auth_service.cc
@@ -41,7 +41,8 @@
 
 GDataAuthService::GDataAuthService()
     : profile_(NULL),
-      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
+      weak_ptr_bound_to_ui_thread_(weak_ptr_factory_.GetWeakPtr()) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
 }
 
@@ -62,11 +63,11 @@
         BrowserThread::UI,
         FROM_HERE,
         base::Bind(&GDataAuthService::StartAuthenticationOnUIThread,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   weak_ptr_bound_to_ui_thread_,
                    registry,
                    relay_proxy,
                    base::Bind(&GDataAuthService::OnAuthCompleted,
-                              weak_ptr_factory_.GetWeakPtr(),
+                              weak_ptr_bound_to_ui_thread_,
                               relay_proxy,
                               callback)));
   } else {
diff --git a/chrome/browser/chromeos/gdata/gdata_auth_service.h b/chrome/browser/chromeos/gdata/gdata_auth_service.h
index a79fa236..1acfac2 100644
--- a/chrome/browser/chromeos/gdata/gdata_auth_service.h
+++ b/chrome/browser/chromeos/gdata/gdata_auth_service.h
@@ -98,10 +98,8 @@
   ObserverList<Observer> observers_;
 
   content::NotificationRegistrar registrar_;
-
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
   base::WeakPtrFactory<GDataAuthService> weak_ptr_factory_;
+  base::WeakPtr<GDataAuthService> weak_ptr_bound_to_ui_thread_;
 
   DISALLOW_COPY_AND_ASSIGN(GDataAuthService);
 };
diff --git a/chrome/browser/chromeos/gdata/gdata_cache.cc b/chrome/browser/chromeos/gdata/gdata_cache.cc
index 4c9ae4c..889f772 100644
--- a/chrome/browser/chromeos/gdata/gdata_cache.cc
+++ b/chrome/browser/chromeos/gdata/gdata_cache.cc
@@ -324,7 +324,8 @@
     : cache_root_path_(cache_root_path),
       cache_paths_(GetCachePaths(cache_root_path_)),
       blocking_task_runner_(blocking_task_runner),
-      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+      ui_weak_ptr_factory_(this),
+      ui_weak_ptr_(ui_weak_ptr_factory_.GetWeakPtr()) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
 }
 
@@ -541,7 +542,7 @@
                  GDataCache::FILE_OPERATION_MOVE,
                  error),
       base::Bind(&GDataCache::OnPinned,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  base::Owned(error),
                  resource_id,
                  md5,
@@ -563,7 +564,7 @@
                  GDataCache::FILE_OPERATION_MOVE,
                  error),
       base::Bind(&GDataCache::OnUnpinned,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  base::Owned(error),
                  resource_id,
                  md5,
@@ -633,7 +634,7 @@
                  GDataCache::FILE_OPERATION_MOVE,
                  error),
       base::Bind(&GDataCache::OnCommitDirty,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  base::Owned(error),
                  resource_id,
                  md5,
@@ -718,7 +719,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
 
   // Invalidate the weak pointer.
-  weak_ptr_factory_.InvalidateWeakPtrs();
+  ui_weak_ptr_factory_.InvalidateWeakPtrs();
 
   // Destroy myself on the blocking pool.
   blocking_task_runner_->PostTask(
diff --git a/chrome/browser/chromeos/gdata/gdata_cache.h b/chrome/browser/chromeos/gdata/gdata_cache.h
index 125d2ac..3ee7388 100644
--- a/chrome/browser/chromeos/gdata/gdata_cache.h
+++ b/chrome/browser/chromeos/gdata/gdata_cache.h
@@ -430,12 +430,13 @@
   // The cache state data. This member must be access only on the blocking pool.
   scoped_ptr<GDataCacheMetadata> metadata_;
 
+  // WeakPtrFactory and WeakPtr bound to the UI thread.
+  base::WeakPtrFactory<GDataCache> ui_weak_ptr_factory_;
+  base::WeakPtr<GDataCache> ui_weak_ptr_;
+
   // List of observers, this member must be accessed on UI thread.
   ObserverList<Observer> observers_;
 
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
-  base::WeakPtrFactory<GDataCache> weak_ptr_factory_;
   DISALLOW_COPY_AND_ASSIGN(GDataCache);
 };
 
diff --git a/chrome/browser/chromeos/gdata/gdata_download_observer.h b/chrome/browser/chromeos/gdata/gdata_download_observer.h
index 51bb00b..ece654d 100644
--- a/chrome/browser/chromeos/gdata/gdata_download_observer.h
+++ b/chrome/browser/chromeos/gdata/gdata_download_observer.h
@@ -2,8 +2,8 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DOWNLOAD_OBSERVER_H_
-#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DOWNLOAD_OBSERVER_H_
+#ifndef CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DOWNLOAD_OBSERVER_H__
+#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DOWNLOAD_OBSERVER_H__
 
 #include <map>
 
@@ -155,8 +155,6 @@
   typedef std::map<int32, content::DownloadItem*> DownloadMap;
   DownloadMap pending_downloads_;
 
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
   base::WeakPtrFactory<GDataDownloadObserver> weak_ptr_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(GDataDownloadObserver);
@@ -164,4 +162,4 @@
 
 }  // namespace gdata
 
-#endif  // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DOWNLOAD_OBSERVER_H_
+#endif  // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_DOWNLOAD_OBSERVER_H__
diff --git a/chrome/browser/chromeos/gdata/gdata_file_system.cc b/chrome/browser/chromeos/gdata/gdata_file_system.cc
index 5170b57..f45ae0a 100644
--- a/chrome/browser/chromeos/gdata/gdata_file_system.cc
+++ b/chrome/browser/chromeos/gdata/gdata_file_system.cc
@@ -735,8 +735,9 @@
       webapps_registry_(webapps_registry),
       update_timer_(true /* retain_user_task */, true /* is_repeating */),
       hide_hosted_docs_(false),
-      blocking_task_runner_(blocking_task_runner),
-      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+      ui_weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
+      ui_weak_ptr_(ui_weak_ptr_factory_.GetWeakPtr()),
+      blocking_task_runner_(blocking_task_runner) {
   // Should be created from the file browser extension API on UI thread.
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
 }
@@ -764,7 +765,7 @@
         directory_service_->largest_changestamp(),
         directory_service_->root()->GetFilePath(),
         base::Bind(&GDataFileSystem::OnUpdateChecked,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   ui_weak_ptr_,
                    initial_origin));
   }
 }
@@ -806,7 +807,7 @@
                       base::TimeDelta::FromSeconds(
                           kGDataUpdateCheckIntervalInSec),
                       base::Bind(&GDataFileSystem::CheckForUpdates,
-                                 weak_ptr_factory_.GetWeakPtr()));
+                          ui_weak_ptr_));
 }
 
 void GDataFileSystem::StopUpdates() {
@@ -822,7 +823,7 @@
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(
       base::Bind(&GDataFileSystem::GetEntryInfoByResourceIdOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  resource_id,
                  CreateRelayCallback(callback)));
 }
@@ -833,7 +834,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   directory_service_->GetEntryByResourceIdAsync(resource_id,
       base::Bind(&GDataFileSystem::GetEntryInfoByEntryOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -868,7 +869,7 @@
     AddObserver(new InitialLoadObserver(
         this,
         base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   ui_weak_ptr_,
                    search_file_path,
                    callback)));
     return;
@@ -881,7 +882,7 @@
         search_file_path,
         // This is the initial load, hence we'll notify when it's done.
         base::Bind(&GDataFileSystem::RunAndNotifyInitialLoadFinished,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   ui_weak_ptr_,
                    callback));
     return;
   }
@@ -891,7 +892,7 @@
   base::MessageLoopProxy::current()->PostTask(
       FROM_HERE,
       base::Bind(&GDataFileSystem::FindEntryByPathSyncOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  search_file_path,
                  callback));
 }
@@ -917,7 +918,7 @@
   // there at all.
   documents_service_->GetAccountMetadata(
       base::Bind(&GDataFileSystem::OnGetAccountMetadata,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  initial_origin,
                  local_changestamp,
                  search_file_path,
@@ -944,7 +945,7 @@
                        std::string() /* no directory resource ID */,
                        callback,
                        base::Bind(&GDataFileSystem::OnFeedFromServerLoaded,
-                                  weak_ptr_factory_.GetWeakPtr()));
+                                  ui_weak_ptr_));
     return;
   }
 
@@ -973,7 +974,7 @@
                        std::string() /* no directory resource ID */,
                        callback,
                        base::Bind(&GDataFileSystem::OnFeedFromServerLoaded,
-                                  weak_ptr_factory_.GetWeakPtr()));
+                                  ui_weak_ptr_));
     return;
   }
 
@@ -1011,7 +1012,7 @@
                      std::string() /* no directory resource ID */,
                      callback,
                      base::Bind(&GDataFileSystem::OnFeedFromServerLoaded,
-                                weak_ptr_factory_.GetWeakPtr()));
+                                ui_weak_ptr_));
 }
 
 void GDataFileSystem::LoadFeedFromServer(
@@ -1037,7 +1038,7 @@
       search_query,
       directory_resource_id,
       base::Bind(&GDataFileSystem::OnGetDocuments,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  initial_origin,
                  feed_load_callback,
                  base::Owned(new GetDocumentsParams(start_changestamp,
@@ -1091,7 +1092,7 @@
 
   GetFileByPath(remote_src_file_path,
       base::Bind(&GDataFileSystem::OnGetFileCompleteForTransferFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  local_dest_file_path,
                  callback),
       GetDownloadDataCallback());
@@ -1108,7 +1109,7 @@
       remote_dest_file_path.DirName(),
       base::Bind(
           &GDataFileSystem::TransferFileFromLocalToRemoteAfterGetEntryInfo,
-          weak_ptr_factory_.GetWeakPtr(),
+          ui_weak_ptr_,
           local_src_file_path,
           remote_dest_file_path,
           callback));
@@ -1148,7 +1149,7 @@
                  local_src_file_path,
                  resource_id),
       base::Bind(&GDataFileSystem::TransferFileForResourceId,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  local_src_file_path,
                  remote_dest_file_path,
                  callback,
@@ -1199,7 +1200,7 @@
                  file_size,
                  content_type),
       base::Bind(&GDataFileSystem::StartFileUploadOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  StartFileUploadParams(local_file_path,
                                        remote_dest_file_path,
                                        callback),
@@ -1232,7 +1233,7 @@
       params.remote_file_path.DirName(),
       base::Bind(
           &GDataFileSystem::StartFileUploadOnUIThreadAfterGetEntryInfo,
-          weak_ptr_factory_.GetWeakPtr(),
+          ui_weak_ptr_,
           params,
           *file_size,
           *content_type));
@@ -1270,7 +1271,7 @@
 
   upload_file_info->completion_callback =
       base::Bind(&GDataFileSystem::OnTransferCompleted,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  params.callback);
 
   uploader_->UploadNewFile(upload_file_info.Pass());
@@ -1301,7 +1302,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::CopyOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                src_file_path,
                                dest_file_path,
                                CreateRelayCallback(callback)));
@@ -1357,7 +1358,7 @@
   // copying of regular files directly on the server side.
   GetFileByPath(src_file_path,
                 base::Bind(&GDataFileSystem::OnGetFileCompleteForCopy,
-                           weak_ptr_factory_.GetWeakPtr(),
+                           ui_weak_ptr_,
                            dest_file_path,
                            callback),
                 GetDownloadDataCallback());
@@ -1390,7 +1391,7 @@
       BrowserThread::UI,
       FROM_HERE,
       base::Bind(&GDataFileSystem::TransferRegularFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  local_file_path, remote_dest_file_path,
                  base::Bind(OnTransferRegularFileCompleteForCopy,
                             callback,
@@ -1439,7 +1440,7 @@
 
   documents_service_->CopyDocument(resource_id, new_name,
       base::Bind(&GDataFileSystem::OnCopyDocumentCompleted,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  dir_path,
                  callback));
 }
@@ -1462,7 +1463,7 @@
   GetEntryInfoByPath(file_path,
                      base::Bind(
                          &GDataFileSystem::RenameAfterGetEntryInfo,
-                         weak_ptr_factory_.GetWeakPtr(),
+                         ui_weak_ptr_,
                          file_path,
                          new_name,
                          callback));
@@ -1500,7 +1501,7 @@
       GURL(entry_proto->edit_url()),
       file_name,
       base::Bind(&GDataFileSystem::OnRenameResourceCompleted,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  file_name,
                  callback));
@@ -1512,7 +1513,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::MoveOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                src_file_path,
                                dest_file_path,
                                CreateRelayCallback(callback)));
@@ -1548,7 +1549,7 @@
   if (src_file_path.DirName() == dest_parent_path) {
     FilePathUpdateCallback final_file_path_update_callback =
         base::Bind(&GDataFileSystem::OnFilePathUpdated,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   ui_weak_ptr_,
                    callback);
 
     Rename(src_file_path, dest_file_path.BaseName().value(),
@@ -1567,13 +1568,13 @@
   //    directory of |dest_file_path|.
   FilePathUpdateCallback add_file_to_directory_callback =
       base::Bind(&GDataFileSystem::AddEntryToDirectory,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  dest_file_path.DirName(),
                  callback);
 
   FilePathUpdateCallback remove_file_from_directory_callback =
       base::Bind(&GDataFileSystem::RemoveEntryFromDirectory,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  src_file_path.DirName(),
                  add_file_to_directory_callback);
 
@@ -1612,7 +1613,7 @@
       dir_entry->content_url(),
       entry->edit_url(),
       base::Bind(&GDataFileSystem::OnAddEntryToDirectoryCompleted,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback,
                  file_path,
                  dir_path));
@@ -1651,7 +1652,7 @@
       entry->edit_url(),
       entry->resource_id(),
       base::Bind(&GDataFileSystem::OnRemoveEntryFromDirectoryCompleted,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback,
                  file_path,
                  dir_path));
@@ -1663,7 +1664,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::RemoveOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                file_path,
                                is_recursive,
                                CreateRelayCallback(callback)));
@@ -1679,7 +1680,7 @@
   GetEntryInfoByPath(file_path,
                      base::Bind(
                          &GDataFileSystem::RemoveOnUIThreadAfterGetEntryInfo,
-                         weak_ptr_factory_.GetWeakPtr(),
+                         ui_weak_ptr_,
                          file_path,
                          is_recursive,
                          callback));
@@ -1705,7 +1706,7 @@
   documents_service_->DeleteDocument(
       GURL(entry_proto->edit_url()),
       base::Bind(&GDataFileSystem::OnRemovedDocument,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback,
                  file_path));
 }
@@ -1718,7 +1719,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::CreateDirectoryOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                directory_path,
                                is_exclusive,
                                is_recursive,
@@ -1783,7 +1784,7 @@
       last_parent_dir_url,
       first_missing_path.BaseName().value(),
       base::Bind(&GDataFileSystem::OnCreateDirectoryCompleted,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  CreateDirectoryParams(
                      first_missing_path,
                      directory_path,
@@ -1798,7 +1799,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::CreateFileOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                file_path,
                                is_exclusive,
                                CreateRelayCallback(callback)));
@@ -1814,7 +1815,7 @@
   FindEntryByPathAsyncOnUIThread(
       file_path,
       base::Bind(&GDataFileSystem::OnGetEntryInfoForCreateFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  is_exclusive,
                  callback));
@@ -1868,7 +1869,7 @@
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(
       base::Bind(&GDataFileSystem::GetFileByPathOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  CreateRelayCallback(get_file_callback),
                  CreateRelayCallback(get_download_data_callback)));
@@ -1883,7 +1884,7 @@
   GetEntryInfoByPath(
       file_path,
       base::Bind(&GDataFileSystem::OnGetEntryInfoCompleteForGetFileByPath,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  CreateRelayCallback(get_file_callback),
                  CreateRelayCallback(get_download_data_callback)));
@@ -1973,7 +1974,7 @@
       entry_proto->file_specific_info().file_md5(),
       base::Bind(
           &GDataFileSystem::OnGetFileFromCache,
-          weak_ptr_factory_.GetWeakPtr(),
+          ui_weak_ptr_,
           GetFileFromCacheParams(
               file_path,
               local_tmp_path,
@@ -1993,7 +1994,7 @@
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(
       base::Bind(&GDataFileSystem::GetFileByResourceIdOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  resource_id,
                  CreateRelayCallback(get_file_callback),
                  CreateRelayCallback(get_download_data_callback)));
@@ -2007,7 +2008,7 @@
 
   directory_service_->GetEntryByResourceIdAsync(resource_id,
       base::Bind(&GDataFileSystem::GetFileByEntryOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  get_file_callback,
                  get_download_data_callback));
 }
@@ -2075,7 +2076,7 @@
   documents_service_->GetDocumentEntry(
       resource_id,
       base::Bind(&GDataFileSystem::OnGetDocumentEntry,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  cache_file_path,
                  GetFileFromCacheParams(params.virtual_file_path,
                                         params.local_tmp_path,
@@ -2136,7 +2137,7 @@
                  file_size,
                  has_enough_space),
       base::Bind(&GDataFileSystem::StartDownloadFileIfEnoughSpace,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  params,
                  content_url,
                  cache_file_path,
@@ -2167,7 +2168,7 @@
       params.local_tmp_path,
       content_url,
       base::Bind(&GDataFileSystem::OnFileDownloaded,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  params),
       params.get_download_data_callback);
 }
@@ -2178,7 +2179,7 @@
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(
       base::Bind(&GDataFileSystem::GetEntryInfoByPathAsyncOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  CreateRelayCallback(callback)));
 }
@@ -2191,7 +2192,7 @@
   FindEntryByPathAsyncOnUIThread(
       file_path,
       base::Bind(&GDataFileSystem::OnGetEntryInfo,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -2220,7 +2221,7 @@
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(
       base::Bind(&GDataFileSystem::ReadDirectoryByPathAsyncOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  CreateRelayCallback(callback)));
 }
@@ -2233,7 +2234,7 @@
   FindEntryByPathAsyncOnUIThread(
       file_path,
       base::Bind(&GDataFileSystem::OnReadDirectory,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -2285,7 +2286,7 @@
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(
       base::Bind(&GDataFileSystem::RequestDirectoryRefreshOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path));
 }
 
@@ -2298,7 +2299,7 @@
       file_path,
       base::Bind(
           &GDataFileSystem::RequestDirectoryRefreshOnUIThreadAfterGetEntryInfo,
-          weak_ptr_factory_.GetWeakPtr(),
+          ui_weak_ptr_,
           file_path));
 }
 
@@ -2323,7 +2324,7 @@
                      entry_proto->resource_id(),
                      FindEntryCallback(),  // Not used.
                      base::Bind(&GDataFileSystem::OnRequestDirectoryRefresh,
-                                weak_ptr_factory_.GetWeakPtr()));
+                                ui_weak_ptr_));
 }
 
 void GDataFileSystem::OnRequestDirectoryRefresh(
@@ -2356,7 +2357,7 @@
 
   directory_service_->GetEntryByResourceIdAsync(params->directory_resource_id,
       base::Bind(&GDataFileSystem::RequestDirectoryRefreshByEntry,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  directory_path,
                  params->directory_resource_id,
                  file_map));
@@ -2402,7 +2403,7 @@
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(
       base::Bind(&GDataFileSystem::UpdateFileByResourceIdOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  resource_id,
                  CreateRelayCallback(callback)));
 }
@@ -2414,7 +2415,7 @@
 
   directory_service_->GetEntryByResourceIdAsync(resource_id,
       base::Bind(&GDataFileSystem::UpdateFileByEntryOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -2436,7 +2437,7 @@
       file->resource_id(),
       file->file_md5(),
       base::Bind(&GDataFileSystem::OnGetFileCompleteForUpdateFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -2466,7 +2467,7 @@
                  get_size_error,
                  file_size),
       base::Bind(&GDataFileSystem::OnGetFileSizeCompleteForUpdateFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback,
                  resource_id,
                  md5,
@@ -2492,7 +2493,7 @@
 
   directory_service_->GetEntryByResourceIdAsync(resource_id,
       base::Bind(&GDataFileSystem::OnGetFileCompleteForUpdateFileByEntry,
-          weak_ptr_factory_.GetWeakPtr(),
+          ui_weak_ptr_,
           callback,
           md5,
           *file_size,
@@ -2521,7 +2522,7 @@
       file_size,
       file->content_mime_type(),
       base::Bind(&GDataFileSystem::OnUpdatedFileUploaded,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -2551,7 +2552,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::GetAvailableSpaceOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                CreateRelayCallback(callback)));
 }
 
@@ -2561,7 +2562,7 @@
 
   documents_service_->GetAccountMetadata(
       base::Bind(&GDataFileSystem::OnGetAvailableSpace,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -2691,8 +2692,7 @@
         base::Bind(&AddEntryToSearchResults,
                    results,
                    callback,
-                   base::Bind(&GDataFileSystem::CheckForUpdates,
-                              weak_ptr_factory_.GetWeakPtr()),
+                   base::Bind(&GDataFileSystem::CheckForUpdates, ui_weak_ptr_),
                    error,
                    i+1 == feed->entries().size()));
   }
@@ -2703,7 +2703,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::SearchAsyncOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                search_query,
                                CreateRelayCallback(callback)));
 }
@@ -2727,8 +2727,7 @@
                      std::string(),  // No directory resource ID.
                      FindEntryCallback(),  // Not used.
                      base::Bind(&GDataFileSystem::OnSearch,
-                                weak_ptr_factory_.GetWeakPtr(),
-                                callback));
+                                ui_weak_ptr_, callback));
 }
 
 void GDataFileSystem::OnGetDocuments(ContentOrigin initial_origin,
@@ -2806,7 +2805,7 @@
         params->search_query,
         params->directory_resource_id,
         base::Bind(&GDataFileSystem::OnGetDocuments,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   ui_weak_ptr_,
                    initial_origin,
                    callback,
                    base::Owned(
@@ -2845,7 +2844,7 @@
   BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE,
       base::Bind(&LoadProtoOnBlockingPool, path, params),
       base::Bind(&GDataFileSystem::OnProtoLoaded,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  base::Owned(params)));
 }
 
@@ -3075,7 +3074,7 @@
         params.resource_id,
         params.md5,
         base::Bind(&GDataFileSystem::UnpinIfPinned,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   ui_weak_ptr_,
                    params.resource_id,
                    params.md5));
   }
@@ -3097,7 +3096,7 @@
                  0,
                  has_enough_space),
       base::Bind(&GDataFileSystem::OnFileDownloadedAndSpaceChecked,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  params,
                  status,
                  content_url,
@@ -3138,7 +3137,7 @@
           downloaded_file_path,
           GDataCache::FILE_OPERATION_MOVE,
           base::Bind(&GDataFileSystem::OnDownloadStoredToCache,
-                     weak_ptr_factory_.GetWeakPtr()));
+                     ui_weak_ptr_));
     } else {
       // If we don't have enough space, remove the downloaded file, and
       // report "no space" error.
@@ -3431,7 +3430,7 @@
   base::MessageLoopProxy::current()->PostTask(
       FROM_HERE,
       base::Bind(&GDataFileSystem::AddUploadedFileOnUIThread,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  upload_mode,
                  virtual_dir_path,
                  base::Passed(&entry),
@@ -3557,7 +3556,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::OpenFileOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                file_path,
                                CreateRelayCallback(callback)));
 }
@@ -3582,10 +3581,10 @@
   GetEntryInfoByPath(
       file_path,
       base::Bind(&GDataFileSystem::OnGetEntryInfoCompleteForOpenFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  base::Bind(&GDataFileSystem::OnOpenFileFinished,
-                            weak_ptr_factory_.GetWeakPtr(),
+                            ui_weak_ptr_,
                             file_path,
                             callback)));
 }
@@ -3618,7 +3617,7 @@
   GetResolvedFileByPath(
       file_path,
       base::Bind(&GDataFileSystem::OnGetFileCompleteForOpenFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback,
                  GetFileCompleteForOpenParams(
                      entry_proto->resource_id(),
@@ -3650,7 +3649,7 @@
       entry_proto.resource_id,
       entry_proto.md5,
       base::Bind(&GDataFileSystem::OnMarkDirtyInCacheCompleteForOpenFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -3687,7 +3686,7 @@
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
          BrowserThread::CurrentlyOn(BrowserThread::IO));
   RunTaskOnUIThread(base::Bind(&GDataFileSystem::CloseFileOnUIThread,
-                               weak_ptr_factory_.GetWeakPtr(),
+                               ui_weak_ptr_,
                                file_path,
                                CreateRelayCallback(callback)));
 }
@@ -3709,10 +3708,10 @@
   GetEntryInfoByPathAsyncOnUIThread(
       file_path,
       base::Bind(&GDataFileSystem::OnGetEntryInfoCompleteForCloseFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  base::Bind(&GDataFileSystem::OnCloseFileFinished,
-                            weak_ptr_factory_.GetWeakPtr(),
+                            ui_weak_ptr_,
                             file_path,
                             callback)));
 }
@@ -3737,7 +3736,7 @@
       entry_proto->resource_id(),
       entry_proto->file_specific_info().file_md5(),
       base::Bind(&GDataFileSystem::OnGetCacheFilePathCompleteForCloseFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  callback));
 }
@@ -3769,7 +3768,7 @@
                  base::Unretained(file_info),
                  base::Unretained(get_file_info_result)),
       base::Bind(&GDataFileSystem::OnGetModifiedFileInfoCompleteForCloseFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  base::Owned(file_info),
                  base::Owned(get_file_info_result),
@@ -3794,7 +3793,7 @@
   FindEntryByPathAsyncOnUIThread(
       file_path,
       base::Bind(&GDataFileSystem::OnGetEntryCompleteForCloseFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  file_path,
                  *file_info,
                  callback));
@@ -3841,7 +3840,7 @@
       file->resource_id(),
       file->file_md5(),
       base::Bind(&GDataFileSystem::OnCommitDirtyInCacheCompleteForCloseFile,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  callback));
 }
 
@@ -3895,9 +3894,7 @@
       md5,
       base::Bind(
           &GDataFileSystem::CheckLocalModificationAndRunAfterGetCacheEntry,
-          weak_ptr_factory_.GetWeakPtr(),
-          base::Passed(&entry_proto),
-          callback));
+          ui_weak_ptr_, base::Passed(&entry_proto), callback));
 }
 
 void GDataFileSystem::CheckLocalModificationAndRunAfterGetCacheEntry(
@@ -3922,9 +3919,7 @@
       md5,
       base::Bind(
           &GDataFileSystem::CheckLocalModificationAndRunAfterGetCacheFile,
-          weak_ptr_factory_.GetWeakPtr(),
-          base::Passed(&entry_proto),
-          callback));
+          ui_weak_ptr_, base::Passed(&entry_proto), callback));
 }
 
 void GDataFileSystem::CheckLocalModificationAndRunAfterGetCacheFile(
@@ -3954,7 +3949,7 @@
                  base::Unretained(file_info),
                  base::Unretained(get_file_info_result)),
       base::Bind(&GDataFileSystem::CheckLocalModificationAndRunAfterGetFileInfo,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 ui_weak_ptr_,
                  base::Passed(&entry_proto),
                  callback,
                  base::Owned(file_info),
diff --git a/chrome/browser/chromeos/gdata/gdata_file_system.h b/chrome/browser/chromeos/gdata/gdata_file_system.h
index 726fefa..caca8b5 100644
--- a/chrome/browser/chromeos/gdata/gdata_file_system.h
+++ b/chrome/browser/chromeos/gdata/gdata_file_system.h
@@ -874,13 +874,13 @@
 
   scoped_ptr<PrefChangeRegistrar> pref_registrar_;
 
+  // WeakPtrFactory and WeakPtr bound to the UI thread.
+  base::WeakPtrFactory<GDataFileSystem> ui_weak_ptr_factory_;
+  base::WeakPtr<GDataFileSystem> ui_weak_ptr_;
+
   ObserverList<Observer> observers_;
 
   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
-
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
-  base::WeakPtrFactory<GDataFileSystem> weak_ptr_factory_;
 };
 
 }  // namespace gdata
diff --git a/chrome/browser/chromeos/gdata/gdata_operation_runner.cc b/chrome/browser/chromeos/gdata/gdata_operation_runner.cc
index 2bd8151..0705c38 100644
--- a/chrome/browser/chromeos/gdata/gdata_operation_runner.cc
+++ b/chrome/browser/chromeos/gdata/gdata_operation_runner.cc
@@ -17,7 +17,8 @@
     : profile_(profile),
       auth_service_(new GDataAuthService()),
       operation_registry_(new GDataOperationRegistry()),
-      weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+      weak_ptr_factory_(this),
+      weak_ptr_bound_to_ui_thread_(weak_ptr_factory_.GetWeakPtr()) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   auth_service_->AddObserver(this);
 }
@@ -49,7 +50,7 @@
   // The re-authenticatation callback will run on UI thread.
   operation->SetReAuthenticateCallback(
       base::Bind(&GDataOperationRunner::RetryOperation,
-                 weak_ptr_factory_.GetWeakPtr()));
+                 weak_ptr_bound_to_ui_thread_));
   StartOperation(operation);
 }
 
@@ -61,7 +62,7 @@
     auth_service_->StartAuthentication(
         operation_registry_.get(),
         base::Bind(&GDataOperationRunner::OnOperationAuthRefresh,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   weak_ptr_bound_to_ui_thread_,
                    operation));
     return;
   }
diff --git a/chrome/browser/chromeos/gdata/gdata_operation_runner.h b/chrome/browser/chromeos/gdata/gdata_operation_runner.h
index a5bbc46..e58c47d 100644
--- a/chrome/browser/chromeos/gdata/gdata_operation_runner.h
+++ b/chrome/browser/chromeos/gdata/gdata_operation_runner.h
@@ -69,10 +69,8 @@
 
   scoped_ptr<GDataAuthService> auth_service_;
   scoped_ptr<GDataOperationRegistry> operation_registry_;
-
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
   base::WeakPtrFactory<GDataOperationRunner> weak_ptr_factory_;
+  base::WeakPtr<GDataOperationRunner> weak_ptr_bound_to_ui_thread_;
 
   DISALLOW_COPY_AND_ASSIGN(GDataOperationRunner);
 };
diff --git a/chrome/browser/chromeos/gdata/gdata_sync_client.h b/chrome/browser/chromeos/gdata/gdata_sync_client.h
index ba9010f4..23324c750 100644
--- a/chrome/browser/chromeos/gdata/gdata_sync_client.h
+++ b/chrome/browser/chromeos/gdata/gdata_sync_client.h
@@ -196,8 +196,6 @@
   // True if the sync loop is running.
   bool sync_loop_is_running_;
 
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
   base::WeakPtrFactory<GDataSyncClient> weak_ptr_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(GDataSyncClient);
diff --git a/chrome/browser/chromeos/gdata/gdata_uploader.cc b/chrome/browser/chromeos/gdata/gdata_uploader.cc
index 806a86a..0451eda 100644
--- a/chrome/browser/chromeos/gdata/gdata_uploader.cc
+++ b/chrome/browser/chromeos/gdata/gdata_uploader.cc
@@ -33,7 +33,7 @@
 GDataUploader::GDataUploader(DocumentsServiceInterface* documents_service)
   : documents_service_(documents_service),
     next_upload_id_(0),
-    ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
+    ALLOW_THIS_IN_INITIALIZER_LIST(uploader_factory_(this)) {
 }
 
 GDataUploader::~GDataUploader() {
@@ -194,7 +194,7 @@
       base::PLATFORM_FILE_READ |
       base::PLATFORM_FILE_ASYNC,
       base::Bind(&GDataUploader::OpenCompletionCallback,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 uploader_factory_.GetWeakPtr(),
                  upload_file_info->upload_id));
   DCHECK_EQ(net::ERR_IO_PENDING, rv);
 }
@@ -247,7 +247,7 @@
                            upload_file_info->initial_upload_location,
                            upload_file_info->gdata_path),
       base::Bind(&GDataUploader::OnUploadLocationReceived,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 uploader_factory_.GetWeakPtr(),
                  upload_file_info->upload_id));
 }
 
@@ -321,7 +321,7 @@
     base::MessageLoopProxy::current()->PostTask(
         FROM_HERE,
         base::Bind(&GDataUploader::ResumeUpload,
-                   weak_ptr_factory_.GetWeakPtr(),
+                   uploader_factory_.GetWeakPtr(),
                    upload_file_info->upload_id));
     return;
   }
@@ -330,7 +330,7 @@
       upload_file_info->buf,
       bytes_to_read,
       base::Bind(&GDataUploader::ReadCompletionCallback,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 uploader_factory_.GetWeakPtr(),
                  upload_file_info->upload_id,
                  bytes_to_read));
 }
@@ -375,7 +375,7 @@
                          upload_file_info->upload_location,
                          upload_file_info->gdata_path),
       base::Bind(&GDataUploader::OnResumeUploadResponseReceived,
-                 weak_ptr_factory_.GetWeakPtr(),
+                 uploader_factory_.GetWeakPtr(),
                  upload_file_info->upload_id));
 }
 
diff --git a/chrome/browser/chromeos/gdata/gdata_uploader.h b/chrome/browser/chromeos/gdata/gdata_uploader.h
index 232d1e0..970bede 100644
--- a/chrome/browser/chromeos/gdata/gdata_uploader.h
+++ b/chrome/browser/chromeos/gdata/gdata_uploader.h
@@ -134,9 +134,8 @@
   typedef std::map<int, UploadFileInfo*> UploadFileInfoMap;
   UploadFileInfoMap pending_uploads_;
 
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
-  base::WeakPtrFactory<GDataUploader> weak_ptr_factory_;
+  // Factory for various callbacks.
+  base::WeakPtrFactory<GDataUploader> uploader_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(GDataUploader);
 };
diff --git a/chrome/browser/chromeos/gdata/operations_base.h b/chrome/browser/chromeos/gdata/operations_base.h
index 45fedf2..4724fca 100644
--- a/chrome/browser/chromeos/gdata/operations_base.h
+++ b/chrome/browser/chromeos/gdata/operations_base.h
@@ -206,9 +206,6 @@
                     scoped_ptr<base::Value>* value);
 
   GetDataCallback callback_;
-
-  // Note: This should remain the last member so it'll be destroyed and
-  // invalidate its weak pointers before any other members are destroyed.
   base::WeakPtrFactory<GetDataOperation> weak_ptr_factory_;
   DISALLOW_COPY_AND_ASSIGN(GetDataOperation);
 };