summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authornoamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-14 19:59:11 +0000
committernoamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-14 19:59:11 +0000
commit00173aa83e1e9330aef623c6bfed382814fd091f (patch)
treef5070905db9cfc49fcf68902cb292dbdd372cd1f /chrome
parent3afa0522a426b1f0ee5c58ae85312a1a598cb11b (diff)
downloadchromium_src-00173aa83e1e9330aef623c6bfed382814fd091f.zip
chromium_src-00173aa83e1e9330aef623c6bfed382814fd091f.tar.gz
chromium_src-00173aa83e1e9330aef623c6bfed382814fd091f.tar.bz2
Add privet HTTP operation for reading file contents
Add a privet HTTP operation for reading the file contents, including the ability to use range requests and save the contents to a file. BUG=332182 Review URL: https://codereview.chromium.org/135513005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251402 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/local_discovery/privet_http.h28
-rw-r--r--chrome/browser/local_discovery/privet_http_impl.cc84
-rw-r--r--chrome/browser/local_discovery/privet_http_impl.h49
3 files changed, 161 insertions, 0 deletions
diff --git a/chrome/browser/local_discovery/privet_http.h b/chrome/browser/local_discovery/privet_http.h
index 044a071..59a2cfad 100644
--- a/chrome/browser/local_discovery/privet_http.h
+++ b/chrome/browser/local_discovery/privet_http.h
@@ -42,6 +42,31 @@ class PrivetJSONOperation {
virtual PrivetHTTPClient* GetHTTPClient() = 0;
};
+class PrivetDataReadOperation {
+ public:
+ enum ResponseType {
+ RESPONSE_TYPE_ERROR,
+ RESPONSE_TYPE_STRING,
+ RESPONSE_TYPE_FILE
+ };
+
+ // If value is null, the operation failed.
+ typedef base::Callback<void(
+ ResponseType /*response_type*/,
+ const std::string& /*response_str*/,
+ const base::FilePath& /*response_file_path*/)> ResultCallback;
+
+ virtual ~PrivetDataReadOperation() {}
+
+ virtual void Start() = 0;
+
+ virtual void SetDataRange(int range_start, int range_end) = 0;
+
+ virtual void SaveDataToFile() = 0;
+
+ virtual PrivetHTTPClient* GetHTTPClient() = 0;
+};
+
// Represents a full registration flow (/privet/register), normally consisting
// of calling the start action, the getClaimToken action, and calling the
// complete action. Some intervention from the caller is required to display the
@@ -151,6 +176,9 @@ class PrivetHTTPClient {
virtual scoped_ptr<PrivetJSONOperation> CreateStorageListOperation(
const std::string& path,
const PrivetJSONOperation::ResultCallback& callback) = 0;
+ virtual scoped_ptr<PrivetDataReadOperation> CreateStorageReadOperation(
+ const std::string& path,
+ const PrivetDataReadOperation::ResultCallback& callback) = 0;
// A name for the HTTP client, e.g. the device name for the privet device.
virtual const std::string& GetName() = 0;
diff --git a/chrome/browser/local_discovery/privet_http_impl.cc b/chrome/browser/local_discovery/privet_http_impl.cc
index 8dd6c3d..734308b 100644
--- a/chrome/browser/local_discovery/privet_http_impl.cc
+++ b/chrome/browser/local_discovery/privet_http_impl.cc
@@ -36,6 +36,7 @@ const char kPrivetContentTypeAny[] = "*/*";
const char kPrivetContentTypeCJT[] = "application/json";
const char kPrivetStorageListPath[] = "/privet/storage/list";
+const char kPrivetStorageContentPath[] = "/privet/storage/content";
const char kPrivetStorageParamPathFormat[] = "path=%s";
const char kPrivetCDDKeySupportedContentTypes[] =
@@ -380,6 +381,77 @@ void PrivetJSONOperationImpl::OnNeedPrivetToken(
privet_client_->RefreshPrivetToken(callback);
}
+PrivetDataReadOperationImpl::PrivetDataReadOperationImpl(
+ PrivetHTTPClientImpl* privet_client,
+ const std::string& path,
+ const std::string& query_params,
+ const PrivetDataReadOperation::ResultCallback& callback)
+ : privet_client_(privet_client), path_(path), query_params_(query_params),
+ callback_(callback), has_range_(false), save_to_file_(false) {
+}
+
+PrivetDataReadOperationImpl::~PrivetDataReadOperationImpl() {
+}
+
+
+void PrivetDataReadOperationImpl::Start() {
+ url_fetcher_ = privet_client_->CreateURLFetcher(
+ CreatePrivetParamURL(path_, query_params_), net::URLFetcher::GET, this);
+ url_fetcher_->DoNotRetryOnTransientError();
+
+ if (has_range_) {
+ url_fetcher_->SetByteRange(range_start_, range_end_);
+ }
+
+ if (save_to_file_) {
+ url_fetcher_->SaveResponseToFile();
+ }
+
+ url_fetcher_->Start();
+}
+
+void PrivetDataReadOperationImpl::SetDataRange(int range_start, int range_end) {
+ has_range_ = true;
+ range_start_ = range_start;
+ range_end_ = range_end;
+}
+
+void PrivetDataReadOperationImpl::SaveDataToFile() {
+ save_to_file_ = false;
+}
+
+PrivetHTTPClient* PrivetDataReadOperationImpl::GetHTTPClient() {
+ return privet_client_;
+}
+
+void PrivetDataReadOperationImpl::OnError(
+ PrivetURLFetcher* fetcher,
+ PrivetURLFetcher::ErrorType error) {
+ callback_.Run(RESPONSE_TYPE_ERROR, std::string(), base::FilePath());
+}
+
+void PrivetDataReadOperationImpl::OnParsedJson(
+ PrivetURLFetcher* fetcher,
+ const base::DictionaryValue* value,
+ bool has_error) {
+ NOTREACHED();
+}
+
+void PrivetDataReadOperationImpl::OnNeedPrivetToken(
+ PrivetURLFetcher* fetcher,
+ const PrivetURLFetcher::TokenCallback& callback) {
+ privet_client_->RefreshPrivetToken(callback);
+}
+
+bool PrivetDataReadOperationImpl::OnRawData(PrivetURLFetcher* fetcher,
+ bool is_file,
+ const std::string& data_str,
+ const base::FilePath& file_path) {
+ ResponseType type = (is_file) ? RESPONSE_TYPE_FILE : RESPONSE_TYPE_STRING;
+ callback_.Run(type, data_str, file_path);
+ return true;
+}
+
PrivetLocalPrintOperationImpl::PrivetLocalPrintOperationImpl(
PrivetHTTPClientImpl* privet_client,
PrivetLocalPrintOperation::Delegate* delegate)
@@ -761,6 +833,18 @@ PrivetHTTPClientImpl::CreateStorageListOperation(
callback));
}
+
+scoped_ptr<PrivetDataReadOperation>
+PrivetHTTPClientImpl::CreateStorageReadOperation(
+ const std::string& path,
+ const PrivetDataReadOperation::ResultCallback& callback) {
+ std::string url_param = base::StringPrintf(kPrivetStorageParamPathFormat,
+ path.c_str());
+ return scoped_ptr<PrivetDataReadOperation>(
+ new PrivetDataReadOperationImpl(this, kPrivetStorageContentPath,
+ url_param, callback));
+}
+
const std::string& PrivetHTTPClientImpl::GetName() {
return name_;
}
diff --git a/chrome/browser/local_discovery/privet_http_impl.h b/chrome/browser/local_discovery/privet_http_impl.h
index 5c720e2..d7699c0 100644
--- a/chrome/browser/local_discovery/privet_http_impl.h
+++ b/chrome/browser/local_discovery/privet_http_impl.h
@@ -146,6 +146,51 @@ class PrivetJSONOperationImpl : public PrivetJSONOperation,
scoped_ptr<PrivetURLFetcher> url_fetcher_;
};
+class PrivetDataReadOperationImpl : public PrivetDataReadOperation,
+ public PrivetURLFetcher::Delegate {
+ public:
+ PrivetDataReadOperationImpl(
+ PrivetHTTPClientImpl* privet_client,
+ const std::string& path,
+ const std::string& query_params,
+ const PrivetDataReadOperation::ResultCallback& callback);
+ virtual ~PrivetDataReadOperationImpl();
+
+ virtual void Start() OVERRIDE;
+
+ virtual void SetDataRange(int range_start, int range_end) OVERRIDE;
+
+ virtual void SaveDataToFile() OVERRIDE;
+
+ virtual PrivetHTTPClient* GetHTTPClient() OVERRIDE;
+
+ virtual void OnError(PrivetURLFetcher* fetcher,
+ PrivetURLFetcher::ErrorType error) OVERRIDE;
+ virtual void OnParsedJson(PrivetURLFetcher* fetcher,
+ const base::DictionaryValue* value,
+ bool has_error) OVERRIDE;
+ virtual void OnNeedPrivetToken(
+ PrivetURLFetcher* fetcher,
+ const PrivetURLFetcher::TokenCallback& callback) OVERRIDE;
+ virtual bool OnRawData(PrivetURLFetcher* fetcher,
+ bool is_file,
+ const std::string& data_str,
+ const base::FilePath& file_path) OVERRIDE;
+
+ private:
+ PrivetHTTPClientImpl* privet_client_;
+ std::string path_;
+ std::string query_params_;
+ int range_start_;
+ int range_end_;
+ PrivetDataReadOperation::ResultCallback callback_;
+
+ bool has_range_;
+ bool save_to_file_;
+
+ scoped_ptr<PrivetURLFetcher> url_fetcher_;
+};
+
class PrivetLocalPrintOperationImpl
: public PrivetLocalPrintOperation,
public PrivetURLFetcher::Delegate {
@@ -262,6 +307,10 @@ class PrivetHTTPClientImpl : public PrivetHTTPClient {
const std::string& path,
const PrivetJSONOperation::ResultCallback& callback) OVERRIDE;
+ virtual scoped_ptr<PrivetDataReadOperation> CreateStorageReadOperation(
+ const std::string& path,
+ const PrivetDataReadOperation::ResultCallback& callback) OVERRIDE;
+
virtual const std::string& GetName() OVERRIDE;
scoped_ptr<PrivetURLFetcher> CreateURLFetcher(