diff options
author | kinaba@chromium.org <kinaba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-08 06:58:51 +0000 |
---|---|---|
committer | kinaba@chromium.org <kinaba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-08 06:58:51 +0000 |
commit | 9ef8d23fe50a1a126622b22ca4f9f9676c5b9e89 (patch) | |
tree | 15ed8bd294c79335bc8328c01361d7752aaad976 | |
parent | 7bdd3cd2f0aafed03963c2229232320037c58cb5 (diff) | |
download | chromium_src-9ef8d23fe50a1a126622b22ca4f9f9676c5b9e89.zip chromium_src-9ef8d23fe50a1a126622b22ca4f9f9676c5b9e89.tar.gz chromium_src-9ef8d23fe50a1a126622b22ca4f9f9676c5b9e89.tar.bz2 |
Remove direct reference to GetBlockingPool() in c/b/google_apis.
Instead, a task runner is injected from the upper layer.
This is for removing the dependency to content/ from c/b/google_apis/ and
moving the directory content to the top level google_apis/.
BUG=256112
Review URL: https://chromiumcodereview.appspot.com/18742002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@210352 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/chromeos/contacts/gdata_contacts_service.cc | 11 | ||||
-rw-r--r-- | chrome/browser/drive/drive_api_service.cc | 1 | ||||
-rw-r--r-- | chrome/browser/drive/gdata_wapi_service.cc | 1 | ||||
-rw-r--r-- | chrome/browser/google_apis/base_requests.cc | 24 | ||||
-rw-r--r-- | chrome/browser/google_apis/base_requests.h | 21 | ||||
-rw-r--r-- | chrome/browser/google_apis/base_requests_server_unittest.cc | 1 | ||||
-rw-r--r-- | chrome/browser/google_apis/base_requests_unittest.cc | 7 | ||||
-rw-r--r-- | chrome/browser/google_apis/drive_api_requests_unittest.cc | 1 | ||||
-rw-r--r-- | chrome/browser/google_apis/gdata_wapi_requests.cc | 29 | ||||
-rw-r--r-- | chrome/browser/google_apis/gdata_wapi_requests_unittest.cc | 1 | ||||
-rw-r--r-- | chrome/browser/google_apis/request_sender.cc | 2 | ||||
-rw-r--r-- | chrome/browser/google_apis/request_sender.h | 18 |
12 files changed, 81 insertions, 36 deletions
diff --git a/chrome/browser/chromeos/contacts/gdata_contacts_service.cc b/chrome/browser/chromeos/contacts/gdata_contacts_service.cc index 12e4161..d95f66d 100644 --- a/chrome/browser/chromeos/contacts/gdata_contacts_service.cc +++ b/chrome/browser/chromeos/contacts/gdata_contacts_service.cc @@ -16,6 +16,7 @@ #include "base/metrics/histogram.h" #include "base/stl_util.h" #include "base/strings/string_util.h" +#include "base/threading/sequenced_worker_pool.h" #include "base/time/time.h" #include "base/timer/timer.h" #include "base/values.h" @@ -856,10 +857,12 @@ GDataContactsService::GDataContactsService( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); std::vector<std::string> scopes; scopes.push_back(kContactsScope); - sender_.reset(new google_apis::RequestSender(profile, - url_request_context_getter, - scopes, - "" /* custom_user_agent */)); + sender_.reset(new google_apis::RequestSender( + profile, + url_request_context_getter, + content::BrowserThread::GetBlockingPool(), + scopes, + "" /* custom_user_agent */)); } GDataContactsService::~GDataContactsService() { diff --git a/chrome/browser/drive/drive_api_service.cc b/chrome/browser/drive/drive_api_service.cc index 6c6707f..ffd2498 100644 --- a/chrome/browser/drive/drive_api_service.cc +++ b/chrome/browser/drive/drive_api_service.cc @@ -287,6 +287,7 @@ void DriveAPIService::Initialize(Profile* profile) { scopes.push_back(kDriveAppsReadonlyScope); sender_.reset(new RequestSender(profile, url_request_context_getter_, + blocking_task_runner_.get(), scopes, custom_user_agent_)); sender_->Initialize(); diff --git a/chrome/browser/drive/gdata_wapi_service.cc b/chrome/browser/drive/gdata_wapi_service.cc index 054d79e..7db14f2 100644 --- a/chrome/browser/drive/gdata_wapi_service.cc +++ b/chrome/browser/drive/gdata_wapi_service.cc @@ -161,6 +161,7 @@ void GDataWapiService::Initialize(Profile* profile) { scopes.push_back(kDriveAppsScope); sender_.reset(new RequestSender(profile, url_request_context_getter_, + blocking_task_runner_.get(), scopes, custom_user_agent_)); sender_->Initialize(); diff --git a/chrome/browser/google_apis/base_requests.cc b/chrome/browser/google_apis/base_requests.cc index 49a5f07..8963eb9 100644 --- a/chrome/browser/google_apis/base_requests.cc +++ b/chrome/browser/google_apis/base_requests.cc @@ -5,13 +5,12 @@ #include "chrome/browser/google_apis/base_requests.h" #include "base/json/json_reader.h" +#include "base/location.h" #include "base/strings/string_number_conversions.h" #include "base/strings/stringprintf.h" #include "base/task_runner_util.h" -#include "base/threading/sequenced_worker_pool.h" #include "base/values.h" #include "chrome/browser/google_apis/request_sender.h" -#include "content/public/browser/browser_thread.h" #include "net/base/load_flags.h" #include "net/base/net_errors.h" #include "net/http/http_byte_range.h" @@ -20,7 +19,6 @@ #include "net/url_request/url_fetcher.h" #include "net/url_request/url_request_status.h" -using content::BrowserThread; using net::URLFetcher; namespace { @@ -80,9 +78,11 @@ std::string GetResponseHeadersAsString( namespace google_apis { -void ParseJson(const std::string& json, const ParseJsonCallback& callback) { +void ParseJson(base::TaskRunner* blocking_task_runner, + const std::string& json, + const ParseJsonCallback& callback) { base::PostTaskAndReplyWithResult( - BrowserThread::GetBlockingPool(), + blocking_task_runner, FROM_HERE, base::Bind(&ParseJsonOnBlockingPool, json), callback); @@ -131,7 +131,7 @@ void UrlFetchRequestBase::Start(const std::string& access_token, if (GetOutputFilePath(&output_file_path)) { url_fetcher_->SaveResponseToFileAtPath( output_file_path, - BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); + blocking_task_runner()); } // Add request headers. @@ -165,7 +165,7 @@ void UrlFetchRequestBase::Start(const std::string& access_token, local_file_path, range_offset, range_length, - BrowserThread::GetBlockingPool()); + blocking_task_runner()); } else { // Even if there is no content data, UrlFetcher requires to set empty // upload data string for POST, PUT and PATCH methods, explicitly. @@ -233,6 +233,10 @@ bool UrlFetchRequestBase::CalledOnValidThread() { return thread_checker_.CalledOnValidThread(); } +base::TaskRunner* UrlFetchRequestBase::blocking_task_runner() const { + return sender_->blocking_task_runner(); +} + void UrlFetchRequestBase::OnProcessURLFetchResultsComplete(bool result) { sender_->RequestFinished(this); } @@ -308,7 +312,8 @@ void GetDataRequest::ParseResponse(GDataErrorCode fetch_error_code, VLOG(1) << "JSON received from " << GetURL().spec() << ": " << data.size() << " bytes"; - ParseJson(data, + ParseJson(blocking_task_runner(), + data, base::Bind(&GetDataRequest::OnDataParsed, weak_ptr_factory_.GetWeakPtr(), fetch_error_code)); @@ -490,7 +495,8 @@ void UploadRangeRequestBase::ProcessURLFetchResults( std::string response_content; source->GetResponseAsString(&response_content); - ParseJson(response_content, + ParseJson(blocking_task_runner(), + response_content, base::Bind(&UploadRangeRequestBase::OnDataParsed, weak_ptr_factory_.GetWeakPtr(), code)); diff --git a/chrome/browser/google_apis/base_requests.h b/chrome/browser/google_apis/base_requests.h index d57a6c6..45cc358 100644 --- a/chrome/browser/google_apis/base_requests.h +++ b/chrome/browser/google_apis/base_requests.h @@ -35,10 +35,12 @@ typedef base::Callback<void(scoped_ptr<base::Value> value)> ParseJsonCallback; // Callback used for DownloadFileRequest and ResumeUploadRequestBase. typedef base::Callback<void(int64 progress, int64 total)> ProgressCallback; -// Parses JSON passed in |json| on blocking pool. Runs |callback| on the calling -// thread when finished with either success or failure. +// Parses JSON passed in |json| on |blocking_task_runner|. Runs |callback| on +// the calling thread when finished with either success or failure. // The callback must not be null. -void ParseJson(const std::string& json, const ParseJsonCallback& callback); +void ParseJson(base::TaskRunner* blocking_task_runner, + const std::string& json, + const ParseJsonCallback& callback); //======================= AuthenticatedRequestInterface ====================== @@ -146,6 +148,9 @@ class UrlFetchRequestBase : public AuthenticatedRequestInterface, // Returns true if called on the thread where the constructor was called. bool CalledOnValidThread(); + // Returns the task runner that should be used for blocking tasks. + base::TaskRunner* blocking_task_runner() const; + private: // URLFetcherDelegate overrides. virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; @@ -176,8 +181,8 @@ typedef base::Callback<void(GDataErrorCode error)> EntryActionCallback; // It is meant to be used for requests that return no JSON blobs. class EntryActionRequest : public UrlFetchRequestBase { public: - // |url_request_context_getter| is used to initialize URLFetcher. - // |callback| must not be null. + // |callback| is called when the request is finished either by success or by + // failure. It must not be null. EntryActionRequest(RequestSender* sender, const EntryActionCallback& callback); virtual ~EntryActionRequest(); @@ -205,7 +210,8 @@ typedef base::Callback<void(GDataErrorCode error, // content into a base::Value. class GetDataRequest : public UrlFetchRequestBase { public: - // |callback| must not be null. + // |callback| is called when the request finishes either by success or by + // failure. On success, a JSON Value object is passed. It must not be null. GetDataRequest(RequestSender* sender, const GetDataCallback& callback); virtual ~GetDataRequest(); @@ -258,8 +264,7 @@ typedef base::Callback<void(GDataErrorCode error, class InitiateUploadRequestBase : public UrlFetchRequestBase { protected: // |callback| will be called with the upload URL, where upload data is - // uploaded to with ResumeUploadRequestBase. - // |callback| must not be null. + // uploaded to with ResumeUploadRequestBase. It must not be null. // |content_type| and |content_length| should be the attributes of the // uploading file. InitiateUploadRequestBase(RequestSender* sender, diff --git a/chrome/browser/google_apis/base_requests_server_unittest.cc b/chrome/browser/google_apis/base_requests_server_unittest.cc index 0688273..63af4eb 100644 --- a/chrome/browser/google_apis/base_requests_server_unittest.cc +++ b/chrome/browser/google_apis/base_requests_server_unittest.cc @@ -43,6 +43,7 @@ class BaseRequestsServerTest : public testing::Test { request_sender_.reset(new RequestSender(profile_.get(), request_context_getter_.get(), + message_loop_.message_loop_proxy(), std::vector<std::string>(), kTestUserAgent)); request_sender_->auth_service()->set_access_token_for_testing( diff --git a/chrome/browser/google_apis/base_requests_unittest.cc b/chrome/browser/google_apis/base_requests_unittest.cc index 8e73ab3..306ec46 100644 --- a/chrome/browser/google_apis/base_requests_unittest.cc +++ b/chrome/browser/google_apis/base_requests_unittest.cc @@ -45,6 +45,7 @@ class BaseRequestsTest : public testing::Test { profile_.reset(new TestingProfile); sender_.reset(new RequestSender(profile_.get(), NULL /* url_request_context_getter */, + message_loop_.message_loop_proxy(), std::vector<std::string>() /* scopes */, std::string() /* custom user agent */)); sender_->Initialize(); @@ -57,7 +58,8 @@ class BaseRequestsTest : public testing::Test { TEST_F(BaseRequestsTest, ParseValidJson) { scoped_ptr<base::Value> json; - ParseJson(kValidJsonString, + ParseJson(message_loop_.message_loop_proxy(), + kValidJsonString, base::Bind(test_util::CreateCopyResultCallback(&json))); // Should wait for a blocking pool task, as the JSON parsing is done in the // blocking pool. @@ -75,7 +77,8 @@ TEST_F(BaseRequestsTest, ParseValidJson) { TEST_F(BaseRequestsTest, ParseInvalidJson) { // Initialize with a valid pointer to verify that null is indeed assigned. scoped_ptr<base::Value> json(base::Value::CreateNullValue()); - ParseJson(kInvalidJsonString, + ParseJson(message_loop_.message_loop_proxy(), + kInvalidJsonString, base::Bind(test_util::CreateCopyResultCallback(&json))); // Should wait for a blocking pool task, as the JSON parsing is done in the // blocking pool. diff --git a/chrome/browser/google_apis/drive_api_requests_unittest.cc b/chrome/browser/google_apis/drive_api_requests_unittest.cc index 9b7c30c..63d3824 100644 --- a/chrome/browser/google_apis/drive_api_requests_unittest.cc +++ b/chrome/browser/google_apis/drive_api_requests_unittest.cc @@ -59,6 +59,7 @@ class DriveApiRequestsTest : public testing::Test { request_sender_.reset(new RequestSender(profile_.get(), request_context_getter_.get(), + message_loop_.message_loop_proxy(), std::vector<std::string>(), kTestUserAgent)); request_sender_->auth_service()->set_access_token_for_testing( diff --git a/chrome/browser/google_apis/gdata_wapi_requests.cc b/chrome/browser/google_apis/gdata_wapi_requests.cc index c5cfdbef..0b22d34 100644 --- a/chrome/browser/google_apis/gdata_wapi_requests.cc +++ b/chrome/browser/google_apis/gdata_wapi_requests.cc @@ -4,21 +4,20 @@ #include "chrome/browser/google_apis/gdata_wapi_requests.h" +#include "base/location.h" #include "base/strings/string_number_conversions.h" #include "base/strings/stringprintf.h" #include "base/task_runner_util.h" -#include "base/threading/sequenced_worker_pool.h" #include "base/values.h" #include "chrome/browser/google_apis/gdata_wapi_parser.h" #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" +#include "chrome/browser/google_apis/request_sender.h" #include "chrome/browser/google_apis/request_util.h" #include "chrome/browser/google_apis/time_util.h" -#include "content/public/browser/browser_thread.h" #include "net/base/escape.h" #include "net/base/url_util.h" #include "third_party/libxml/chromium/libxml_utils.h" -using content::BrowserThread; using net::URLFetcher; namespace google_apis { @@ -57,9 +56,11 @@ void DidParseResourceListOnBlockingPool( // Parses the JSON value to ResourceList on the blocking pool and runs // |callback| on the UI thread once parsing is done. -void ParseResourceListAndRun(const GetResourceListCallback& callback, - GDataErrorCode error, - scoped_ptr<base::Value> value) { +void ParseResourceListAndRun( + scoped_refptr<base::TaskRunner> blocking_task_runner, + const GetResourceListCallback& callback, + GDataErrorCode error, + scoped_ptr<base::Value> value) { DCHECK(!callback.is_null()); if (!value) { @@ -68,7 +69,7 @@ void ParseResourceListAndRun(const GetResourceListCallback& callback, } base::PostTaskAndReplyWithResult( - BrowserThread::GetBlockingPool(), + blocking_task_runner, FROM_HERE, base::Bind(&ParseResourceListOnBlockingPool, base::Passed(&value)), base::Bind(&DidParseResourceListOnBlockingPool, callback, error)); @@ -162,8 +163,11 @@ GetResourceListRequest::GetResourceListRequest( const std::string& search_string, const std::string& directory_resource_id, const GetResourceListCallback& callback) - : GetDataRequest(sender, - base::Bind(&ParseResourceListAndRun, callback)), + : GetDataRequest( + sender, + base::Bind(&ParseResourceListAndRun, + make_scoped_refptr(sender->blocking_task_runner()), + callback)), url_generator_(url_generator), override_url_(override_url), start_changestamp_(start_changestamp), @@ -189,8 +193,11 @@ SearchByTitleRequest::SearchByTitleRequest( const std::string& title, const std::string& directory_resource_id, const GetResourceListCallback& callback) - : GetDataRequest(sender, - base::Bind(&ParseResourceListAndRun, callback)), + : GetDataRequest( + sender, + base::Bind(&ParseResourceListAndRun, + make_scoped_refptr(sender->blocking_task_runner()), + callback)), url_generator_(url_generator), title_(title), directory_resource_id_(directory_resource_id) { diff --git a/chrome/browser/google_apis/gdata_wapi_requests_unittest.cc b/chrome/browser/google_apis/gdata_wapi_requests_unittest.cc index 09db71d..3981a26 100644 --- a/chrome/browser/google_apis/gdata_wapi_requests_unittest.cc +++ b/chrome/browser/google_apis/gdata_wapi_requests_unittest.cc @@ -53,6 +53,7 @@ class GDataWapiRequestsTest : public testing::Test { request_sender_.reset(new RequestSender(profile_.get(), request_context_getter_.get(), + message_loop_.message_loop_proxy(), std::vector<std::string>(), kTestUserAgent)); request_sender_->auth_service()->set_access_token_for_testing( diff --git a/chrome/browser/google_apis/request_sender.cc b/chrome/browser/google_apis/request_sender.cc index b2709dc..7df6354 100644 --- a/chrome/browser/google_apis/request_sender.cc +++ b/chrome/browser/google_apis/request_sender.cc @@ -14,10 +14,12 @@ namespace google_apis { RequestSender::RequestSender( Profile* profile, net::URLRequestContextGetter* url_request_context_getter, + base::TaskRunner* blocking_task_runner, const std::vector<std::string>& scopes, const std::string& custom_user_agent) : profile_(profile), url_request_context_getter_(url_request_context_getter), + blocking_task_runner_(blocking_task_runner), auth_service_(new AuthService(url_request_context_getter, scopes)), custom_user_agent_(custom_user_agent), weak_ptr_factory_(this) { diff --git a/chrome/browser/google_apis/request_sender.h b/chrome/browser/google_apis/request_sender.h index f20c394..96b3fd1 100644 --- a/chrome/browser/google_apis/request_sender.h +++ b/chrome/browser/google_apis/request_sender.h @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "base/callback_forward.h" +#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "base/threading/thread_checker.h" @@ -18,6 +19,10 @@ class Profile; +namespace base { +class TaskRunner; +} + namespace net { class URLRequestContextGetter; } @@ -31,8 +36,11 @@ class AuthService; // AuthenticatedRequestInterface and handles retries and authentication. class RequestSender { public: - // |url_request_context_getter| is used to perform authentication with - // AuthService. + // |url_request_context_getter| is the context used to perform network + // requests from this RequestSender. + // + // |blocking_task_runner| is used for running blocking operation, e.g., + // parsing JSON response from the server. // // |scopes| specifies OAuth2 scopes. // @@ -40,6 +48,7 @@ class RequestSender { // requests issued through the request sender if the value is not empty. RequestSender(Profile* profile, net::URLRequestContextGetter* url_request_context_getter, + base::TaskRunner* blocking_task_runner, const std::vector<std::string>& scopes, const std::string& custom_user_agent); virtual ~RequestSender(); @@ -50,6 +59,10 @@ class RequestSender { return url_request_context_getter_; } + base::TaskRunner* blocking_task_runner() const { + return blocking_task_runner_.get(); + } + // Prepares the object for use. virtual void Initialize(); @@ -85,6 +98,7 @@ class RequestSender { Profile* profile_; // Not owned. net::URLRequestContextGetter* url_request_context_getter_; // Not owned. + scoped_refptr<base::TaskRunner> blocking_task_runner_; scoped_ptr<AuthService> auth_service_; std::set<AuthenticatedRequestInterface*> in_flight_requests_; |