Commit c4aa3874 authored by satorux@chromium.org's avatar satorux@chromium.org

gdata: Move GDataWapiFeedLoader to a set of new files

This change reduces ~800 lines from gdata_file_system.cc
Along the way move some functions to gdata_util.h, which
are needed for gdata_file_system.cc and gdata_wapi_feed_loader.cc

BUG=138268
TEST=none; just move code around. should not change the behaviors
TBR=ben@chromium.org
for chrome_browser.gypi

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150027 0039d316-1c4b-4281-b951-d872f2087c98
parent 2c148b72
......@@ -19,6 +19,7 @@
#include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h"
#include "chrome/browser/chromeos/gdata/gdata_errorcode.h"
#include "chrome/browser/chromeos/gdata/gdata_files.h"
#include "chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.h"
#include "chrome/browser/chromeos/gdata/gdata_wapi_feed_processor.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
#include "content/public/browser/notification_observer.h"
......@@ -34,167 +35,8 @@ namespace gdata {
class DocumentsServiceInterface;
class DriveWebAppsRegistryInterface;
class GDataWapiFeedLoader;
struct GetDocumentsParams;
struct GetDocumentsUiState;
struct UploadFileInfo;
// Callback run as a response to LoadFromServer.
//
// TODO(satorux): Move this to a new file: crbug.com/138268
typedef base::Callback<void(GetDocumentsParams* params,
GDataFileError error)>
LoadDocumentFeedCallback;
// GDataWapiFeedLoader is used to load feeds from WAPI (codename for
// Documents List API) and load the cached proto file.
//
// TODO(satorux): Move this to a new file: crbug.com/138268
class GDataWapiFeedLoader {
public:
// Used to notify events from the loader.
// All events are notified on UI thread.
class Observer {
public:
// Triggered when a content of a directory has been changed.
// |directory_path| is a virtual directory path representing the
// changed directory.
virtual void OnDirectoryChanged(const FilePath& directory_path) {}
// Triggered when a document feed is fetched. |num_accumulated_entries|
// tells the number of entries fetched so far.
virtual void OnDocumentFeedFetched(int num_accumulated_entries) {}
// Triggered when the feed from the server is loaded.
virtual void OnFeedFromServerLoaded() {}
protected:
virtual ~Observer() {}
};
GDataWapiFeedLoader(
GDataDirectoryService* directory_service,
DocumentsServiceInterface* documents_service,
DriveWebAppsRegistryInterface* webapps_registry,
GDataCache* cache,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_);
~GDataWapiFeedLoader();
// Adds and removes the observer.
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Starts root feed load from the cache. If successful, it will try to find
// the file upon retrieval completion. In addition to that, it will
// initiate retrieval of the root feed from the server unless
// |should_load_from_server| is set to false. |should_load_from_server| is
// false only for testing.
void LoadFromCache(bool should_load_from_server,
const FilePath& search_file_path,
const FindEntryCallback& callback);
// Starts root feed load from the server. Value of |start_changestamp|
// determines the type of feed to load - 0 means root feed, every other
// value would trigger delta feed.
// In the case of loading the root feed we use |root_feed_changestamp| as its
// initial changestamp value since it does not come with that info.
// When done |load_feed_callback| is invoked.
// |entry_found_callback| is used only when this is invoked while searching
// for file info, and is used in |load_feed_callback|. If successful, it will
// try to find the file upon retrieval completion.
// |should_fetch_multiple_feeds| is true iff don't want to stop feed loading
// after we retrieve first feed chunk.
// If invoked as a part of content search, query will be set in
// |search_query|.
// If |feed_to_load| is set, this is feed url that will be used to load feed.
void LoadFromServer(
ContentOrigin initial_origin,
int start_changestamp,
int root_feed_changestamp,
bool should_fetch_multiple_feeds,
const FilePath& search_file_path,
const std::string& search_query,
const GURL& feed_to_load,
const std::string& directory_resource_id,
const FindEntryCallback& entry_found_callback,
const LoadDocumentFeedCallback& feed_load_callback);
// Retrieves account metadata and determines from the last change timestamp
// if the feed content loading from the server needs to be initiated.
void ReloadFromServerIfNeeded(
ContentOrigin initial_origin,
int local_changestamp,
const FilePath& search_file_path,
const FindEntryCallback& callback);
// Updates whole directory structure feeds collected in |feed_list|.
// On success, returns PLATFORM_FILE_OK. Record file statistics as UMA
// histograms.
//
// See comments at GDataWapiFeedProcessor::ApplyFeeds() for
// |start_changestamp| and |root_feed_changestamp|.
GDataFileError UpdateFromFeed(
const std::vector<DocumentFeed*>& feed_list,
int start_changestamp,
int root_feed_changestamp);
private:
// Callback for handling root directory refresh from the cache.
void OnProtoLoaded(LoadRootFeedParams* params);
// Continues handling root directory refresh after the directory service
// is fully loaded.
void ContinueWithInitializedDirectoryService(LoadRootFeedParams* params,
GDataFileError error);
// Helper callback for handling results of metadata retrieval initiated from
// ReloadFeedFromServerIfNeeded(). This method makes a decision about fetching
// the content of the root feed during the root directory refresh process.
void OnGetAccountMetadata(
ContentOrigin initial_origin,
int local_changestamp,
const FilePath& search_file_path,
const FindEntryCallback& callback,
GDataErrorCode status,
scoped_ptr<base::Value> feed_data);
// Callback for handling feed content fetching while searching for file info.
// This callback is invoked after async feed fetch operation that was
// invoked by StartDirectoryRefresh() completes. This callback will update
// the content of the refreshed directory object and continue initially
// started FindEntryByPath() request.
void OnFeedFromServerLoaded(GetDocumentsParams* params,
GDataFileError error);
// Callback for handling response from |GDataDocumentsService::GetDocuments|.
// Invokes |callback| when done.
void OnGetDocuments(
ContentOrigin initial_origin,
const LoadDocumentFeedCallback& callback,
GetDocumentsParams* params,
base::TimeTicks start_time,
GDataErrorCode status,
scoped_ptr<base::Value> data);
// Save filesystem to disk.
void SaveFileSystem();
// Callback for handling UI updates caused by document fetching.
void OnNotifyDocumentFeedFetched(
base::WeakPtr<GetDocumentsUiState> ui_state);
GDataDirectoryService* directory_service_; // Not owned.
DocumentsServiceInterface* documents_service_; // Not owned.
DriveWebAppsRegistryInterface* webapps_registry_; // Not owned.
GDataCache* cache_; // Not owned.
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
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<GDataWapiFeedLoader> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(GDataWapiFeedLoader);
};
// The production implementation of GDataFileSystemInterface.
class GDataFileSystem : public GDataFileSystemInterface,
public GDataWapiFeedLoader::Observer,
......
......@@ -21,6 +21,7 @@
#include "base/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/time.h"
#include "base/tracked_objects.h"
#include "chrome/browser/chromeos/gdata/file_write_helper.h"
#include "chrome/browser/chromeos/gdata/gdata.pb.h"
#include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h"
......@@ -604,5 +605,47 @@ void PrepareWritableFileAndRun(Profile* profile,
}
}
GDataFileError GDataToGDataFileError(GDataErrorCode status) {
switch (status) {
case HTTP_SUCCESS:
case HTTP_CREATED:
return GDATA_FILE_OK;
case HTTP_UNAUTHORIZED:
case HTTP_FORBIDDEN:
return GDATA_FILE_ERROR_ACCESS_DENIED;
case HTTP_NOT_FOUND:
return GDATA_FILE_ERROR_NOT_FOUND;
case GDATA_PARSE_ERROR:
case GDATA_FILE_ERROR:
return GDATA_FILE_ERROR_ABORT;
case GDATA_NO_CONNECTION:
return GDATA_FILE_ERROR_NO_CONNECTION;
default:
return GDATA_FILE_ERROR_FAILED;
}
}
void PostBlockingPoolSequencedTask(
const tracked_objects::Location& from_here,
base::SequencedTaskRunner* blocking_task_runner,
const base::Closure& task) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const bool posted = blocking_task_runner->PostTask(from_here, task);
DCHECK(posted);
}
void PostBlockingPoolSequencedTaskAndReply(
const tracked_objects::Location& from_here,
base::SequencedTaskRunner* blocking_task_runner,
const base::Closure& request_task,
const base::Closure& reply_task) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const bool posted = blocking_task_runner->PostTaskAndReply(
from_here, request_task, reply_task);
DCHECK(posted);
}
} // namespace util
} // namespace gdata
......@@ -18,6 +18,14 @@
class FilePath;
class Profile;
namespace base {
class SequencedTaskRunner;
} // namespace base
namespace tracked_objects {
class Location;
} // tracked_objects
namespace gdata {
namespace util {
......@@ -112,7 +120,7 @@ std::string FormatTimeAsString(const base::Time& time);
// Callback type for PrepareWritableFilePathAndRun.
typedef base::Callback<void (GDataFileError, const FilePath& path)>
OpenFileCallback;
OpenFileCallback;
// Invokes |callback| on blocking thread pool, after converting virtual |path|
// string like "/special/drive/foo.txt" to the concrete local cache file path.
......@@ -126,6 +134,24 @@ void PrepareWritableFileAndRun(Profile* profile,
const FilePath& path,
const OpenFileCallback& callback);
// Converts gdata error code into file platform error code.
GDataFileError GDataToGDataFileError(GDataErrorCode status);
// Wrapper around BrowserThread::PostTask to post a task to the blocking
// pool with the given sequence token.
void PostBlockingPoolSequencedTask(
const tracked_objects::Location& from_here,
base::SequencedTaskRunner* blocking_task_runner,
const base::Closure& task);
// Similar to PostBlockingPoolSequencedTask() but this one takes a reply
// callback that runs on the calling thread.
void PostBlockingPoolSequencedTaskAndReply(
const tracked_objects::Location& from_here,
base::SequencedTaskRunner* blocking_task_runner,
const base::Closure& request_task,
const base::Closure& reply_task);
} // namespace util
} // namespace gdata
......
This diff is collapsed.
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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_WAPI_FEED_LOADER_H_
#define CHROME_BROWSER_CHROMEOS_GDATA_GDATA_WAPI_FEED_LOADER_H_
#include "base/callback.h"
#include "base/file_path.h"
#include "base/observer_list.h"
#include "chrome/browser/chromeos/gdata/gdata_errorcode.h"
#include "chrome/browser/chromeos/gdata/gdata_files.h"
namespace gdata {
class DriveWebAppsRegistryInterface;
class DocumentsServiceInterface;
class GDataCache;
struct GetDocumentsUiState;
// Set of parameters sent to LoadDocumentFeedCallback callback.
struct GetDocumentsParams {
GetDocumentsParams(int start_changestamp,
int root_feed_changestamp,
std::vector<DocumentFeed*>* feed_list,
bool should_fetch_multiple_feeds,
const FilePath& search_file_path,
const std::string& search_query,
const std::string& directory_resource_id,
const FindEntryCallback& callback,
GetDocumentsUiState* ui_state);
~GetDocumentsParams();
// Changestamps are positive numbers in increasing order. The difference
// between two changestamps is proportional equal to number of items in
// delta feed between them - bigger the difference, more likely bigger
// number of items in delta feeds.
int start_changestamp;
int root_feed_changestamp;
scoped_ptr<std::vector<DocumentFeed*> > feed_list;
// Should we stop after getting first feed chunk, even if there is more
// data.
bool should_fetch_multiple_feeds;
FilePath search_file_path;
std::string search_query;
std::string directory_resource_id;
FindEntryCallback callback;
scoped_ptr<GetDocumentsUiState> ui_state;
};
// Callback run as a response to LoadFromServer.
typedef base::Callback<void(GetDocumentsParams* params,
GDataFileError error)>
LoadDocumentFeedCallback;
// GDataWapiFeedLoader is used to load feeds from WAPI (codename for
// Documents List API) and load the cached proto file.
class GDataWapiFeedLoader {
public:
// Used to notify events from the loader.
// All events are notified on UI thread.
class Observer {
public:
// Triggered when a content of a directory has been changed.
// |directory_path| is a virtual directory path representing the
// changed directory.
virtual void OnDirectoryChanged(const FilePath& directory_path) {}
// Triggered when a document feed is fetched. |num_accumulated_entries|
// tells the number of entries fetched so far.
virtual void OnDocumentFeedFetched(int num_accumulated_entries) {}
// Triggered when the feed from the server is loaded.
virtual void OnFeedFromServerLoaded() {}
protected:
virtual ~Observer() {}
};
GDataWapiFeedLoader(
GDataDirectoryService* directory_service,
DocumentsServiceInterface* documents_service,
DriveWebAppsRegistryInterface* webapps_registry,
GDataCache* cache,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_);
~GDataWapiFeedLoader();
// Adds and removes the observer.
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Starts root feed load from the cache. If successful, it will try to find
// the file upon retrieval completion. In addition to that, it will
// initiate retrieval of the root feed from the server unless
// |should_load_from_server| is set to false. |should_load_from_server| is
// false only for testing.
void LoadFromCache(bool should_load_from_server,
const FilePath& search_file_path,
const FindEntryCallback& callback);
// Starts root feed load from the server. Value of |start_changestamp|
// determines the type of feed to load - 0 means root feed, every other
// value would trigger delta feed.
// In the case of loading the root feed we use |root_feed_changestamp| as its
// initial changestamp value since it does not come with that info.
// When done |load_feed_callback| is invoked.
// |entry_found_callback| is used only when this is invoked while searching
// for file info, and is used in |load_feed_callback|. If successful, it will
// try to find the file upon retrieval completion.
// |should_fetch_multiple_feeds| is true iff don't want to stop feed loading
// after we retrieve first feed chunk.
// If invoked as a part of content search, query will be set in
// |search_query|.
// If |feed_to_load| is set, this is feed url that will be used to load feed.
void LoadFromServer(
ContentOrigin initial_origin,
int start_changestamp,
int root_feed_changestamp,
bool should_fetch_multiple_feeds,
const FilePath& search_file_path,
const std::string& search_query,
const GURL& feed_to_load,
const std::string& directory_resource_id,
const FindEntryCallback& entry_found_callback,
const LoadDocumentFeedCallback& feed_load_callback);
// Retrieves account metadata and determines from the last change timestamp
// if the feed content loading from the server needs to be initiated.
void ReloadFromServerIfNeeded(
ContentOrigin initial_origin,
int local_changestamp,
const FilePath& search_file_path,
const FindEntryCallback& callback);
// Updates whole directory structure feeds collected in |feed_list|.
// On success, returns PLATFORM_FILE_OK. Record file statistics as UMA
// histograms.
//
// See comments at GDataWapiFeedProcessor::ApplyFeeds() for
// |start_changestamp| and |root_feed_changestamp|.
GDataFileError UpdateFromFeed(
const std::vector<DocumentFeed*>& feed_list,
int start_changestamp,
int root_feed_changestamp);
private:
// Callback for handling root directory refresh from the cache.
void OnProtoLoaded(LoadRootFeedParams* params);
// Continues handling root directory refresh after the directory service
// is fully loaded.
void ContinueWithInitializedDirectoryService(LoadRootFeedParams* params,
GDataFileError error);
// Helper callback for handling results of metadata retrieval initiated from
// ReloadFeedFromServerIfNeeded(). This method makes a decision about fetching
// the content of the root feed during the root directory refresh process.
void OnGetAccountMetadata(
ContentOrigin initial_origin,
int local_changestamp,
const FilePath& search_file_path,
const FindEntryCallback& callback,
GDataErrorCode status,
scoped_ptr<base::Value> feed_data);
// Callback for handling feed content fetching while searching for file info.
// This callback is invoked after async feed fetch operation that was
// invoked by StartDirectoryRefresh() completes. This callback will update
// the content of the refreshed directory object and continue initially
// started FindEntryByPath() request.
void OnFeedFromServerLoaded(GetDocumentsParams* params,
GDataFileError error);
// Callback for handling response from |GDataDocumentsService::GetDocuments|.
// Invokes |callback| when done.
void OnGetDocuments(
ContentOrigin initial_origin,
const LoadDocumentFeedCallback& callback,
GetDocumentsParams* params,
base::TimeTicks start_time,
GDataErrorCode status,
scoped_ptr<base::Value> data);
// Save filesystem to disk.
void SaveFileSystem();
// Callback for handling UI updates caused by document fetching.
void OnNotifyDocumentFeedFetched(
base::WeakPtr<GetDocumentsUiState> ui_state);
GDataDirectoryService* directory_service_; // Not owned.
DocumentsServiceInterface* documents_service_; // Not owned.
DriveWebAppsRegistryInterface* webapps_registry_; // Not owned.
GDataCache* cache_; // Not owned.
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
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<GDataWapiFeedLoader> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(GDataWapiFeedLoader);
};
} // namespace gdata
#endif // CHROME_BROWSER_CHROMEOS_GDATA_GDATA_WAPI_FEED_LOADER_H_
......@@ -593,6 +593,8 @@
'browser/chromeos/gdata/gdata_uploader.h',
'browser/chromeos/gdata/gdata_util.cc',
'browser/chromeos/gdata/gdata_util.h',
'browser/chromeos/gdata/gdata_wapi_feed_loader.cc',
'browser/chromeos/gdata/gdata_wapi_feed_loader.h',
'browser/chromeos/gdata/gdata_wapi_feed_processor.cc',
'browser/chromeos/gdata/gdata_wapi_feed_processor.h',
'browser/chromeos/gdata/gdata_wapi_parser.cc',
......
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