Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / privet_http_impl.cc
index 8cef172..a6dcab7 100644 (file)
@@ -13,7 +13,9 @@
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/stringprintf.h"
 #include "chrome/browser/local_discovery/privet_constants.h"
+#include "components/cloud_devices/printer_description.h"
 #include "net/base/url_util.h"
+#include "printing/units.h"
 #include "url/gurl.h"
 
 namespace local_discovery {
@@ -23,10 +25,12 @@ const char kUrlPlaceHolder[] = "http://host/";
 const char kPrivetRegisterActionArgName[] = "action";
 const char kPrivetRegisterUserArgName[] = "user";
 
-const char kPrivetURLKeyUser[] = "user";
-const char kPrivetURLKeyJobname[] = "jobname";
+const char kPrivetURLKeyUserName[] = "user_name";
+const char kPrivetURLKeyClientName[] = "client_name";
+const char kPrivetURLKeyJobname[] = "job_name";
 const char kPrivetURLKeyOffline[] = "offline";
 const char kPrivetURLValueOffline[] = "1";
+const char kPrivetURLValueClientName[] = "Chrome";
 
 const char kPrivetContentTypePDF[] = "application/pdf";
 const char kPrivetContentTypePWGRaster[] = "image/pwg-raster";
@@ -34,13 +38,9 @@ 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[] =
-    "printer.supported_content_type";
-
-const char kPrivetCDDKeyContentType[] = "content_type";
-
 const char kPrivetKeyJobID[] = "job_id";
 
 const int kPrivetCancelationTimeoutSeconds = 3;
@@ -378,12 +378,89 @@ 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)
-    : privet_client_(privet_client), delegate_(delegate),
-      use_pdf_(false), has_capabilities_(false), has_extended_workflow_(false),
-      started_(false), offline_(false), invalid_job_retries_(0),
+    : privet_client_(privet_client),
+      delegate_(delegate),
+      use_pdf_(false),
+      has_capabilities_(false),
+      has_extended_workflow_(false),
+      started_(false),
+      offline_(false),
+      dpi_(printing::kDefaultPdfDpi),
+      invalid_job_retries_(0),
       weak_factory_(this) {
 }
 
@@ -440,7 +517,7 @@ void PrivetLocalPrintOperationImpl::StartInitialRequest() {
   if (has_capabilities_) {
     GetCapabilities();
   } else {
-    // Since we have no capabiltties, the only reasonable format we can
+    // Since we have no capabilities, the only reasonable format we can
     // request is PWG Raster.
     use_pdf_ = false;
     StartConvertToPWG();
@@ -478,9 +555,13 @@ void PrivetLocalPrintOperationImpl::DoSubmitdoc() {
 
   GURL url = CreatePrivetURL(kPrivetSubmitdocPath);
 
+  url = net::AppendQueryParameter(url,
+                                  kPrivetURLKeyClientName,
+                                  kPrivetURLValueClientName);
+
   if (!user_.empty()) {
     url = net::AppendQueryParameter(url,
-                                    kPrivetURLKeyUser,
+                                    kPrivetURLKeyUserName,
                                     user_);
   }
 
@@ -528,9 +609,14 @@ void PrivetLocalPrintOperationImpl::StartPrinting() {
 void PrivetLocalPrintOperationImpl::StartConvertToPWG() {
   if (!pwg_raster_converter_)
     pwg_raster_converter_ = PWGRasterConverter::CreateDefault();
+  double scale = dpi_;
+  scale /= printing::kPointsPerInch;
+  // Make vertical rectangle to optimize streaming to printer. Fix orientation
+  // by autorotate.
+  gfx::Rect area(std::min(page_size_.width(), page_size_.height()) * scale,
+                 std::max(page_size_.width(), page_size_.height()) * scale);
   pwg_raster_converter_->Start(
-      data_,
-      conversion_settings_,
+      data_, printing::PdfRenderSettings(area, dpi_, true),
       base::Bind(&PrivetLocalPrintOperationImpl::OnPWGRasterConverted,
                  base::Unretained(this)));
 }
@@ -543,28 +629,26 @@ void PrivetLocalPrintOperationImpl::OnCapabilitiesResponse(
     return;
   }
 
-  const base::ListValue* supported_content_types;
-  use_pdf_ = false;
+  cloud_devices::CloudDeviceDescription description;
+  if (!description.InitFromDictionary(make_scoped_ptr(value->DeepCopy()))) {
+    delegate_->OnPrivetPrintingError(this, 200);
+    return;
+  }
 
-  if (value->GetList(kPrivetCDDKeySupportedContentTypes,
-                     &supported_content_types)) {
-    for (size_t i = 0; i < supported_content_types->GetSize(); i++) {
-      const base::DictionaryValue* content_type_value;
-      std::string content_type;
-
-      if (supported_content_types->GetDictionary(i, &content_type_value) &&
-          content_type_value->GetString(kPrivetCDDKeyContentType,
-                                        &content_type) &&
-          (content_type == kPrivetContentTypePDF ||
-           content_type == kPrivetContentTypeAny) ) {
-        use_pdf_ = true;
-      }
-    }
+  use_pdf_ = false;
+  cloud_devices::printer::ContentTypesCapability content_types;
+  if (content_types.LoadFrom(description)) {
+    use_pdf_ = content_types.Contains(kPrivetContentTypePDF) ||
+               content_types.Contains(kPrivetContentTypeAny);
   }
 
   if (use_pdf_) {
     StartPrinting();
   } else {
+    cloud_devices::printer::DpiCapability dpis;
+    if (dpis.LoadFrom(description)) {
+      dpi_ = std::max(dpis.GetDefault().horizontal, dpis.GetDefault().vertical);
+    }
     StartConvertToPWG();
   }
 }
@@ -689,10 +773,9 @@ void PrivetLocalPrintOperationImpl::SetOffline(bool offline) {
   offline_ = offline;
 }
 
-void PrivetLocalPrintOperationImpl::SetConversionSettings(
-    const printing::PdfRenderSettings& conversion_settings) {
+void PrivetLocalPrintOperationImpl::SetPageSize(const gfx::Size& page_size) {
   DCHECK(!started_);
-  conversion_settings_ = conversion_settings;
+  page_size_ = page_size;
 }
 
 void PrivetLocalPrintOperationImpl::SetPWGRasterConverterForTesting(
@@ -755,6 +838,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_;
 }