1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/cloud_print/cloud_print_helpers.h"
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/rand_util.h"
11 #include "base/stringprintf.h"
12 #include "base/values.h"
13 #include "googleurl/src/gurl.h"
15 namespace cloud_print
{
17 const char kPrinterListValue
[] = "printers";
18 const char kSuccessValue
[] = "success";
20 // Certain cloud print requests require Chrome's X-CloudPrint-Proxy header.
21 const char kChromeCloudPrintProxyHeader
[] = "X-CloudPrint-Proxy: Chrome";
23 std::string
AppendPathToUrl(const GURL
& url
, const std::string
& path
) {
24 DCHECK_NE(path
[0], '/');
25 std::string ret
= url
.path();
26 if (url
.has_path() && (ret
[ret
.length() - 1] != '/'))
32 GURL
GetUrlForSearch(const GURL
& cloud_print_server_url
) {
33 std::string
path(AppendPathToUrl(cloud_print_server_url
, "search"));
34 GURL::Replacements replacements
;
35 replacements
.SetPathStr(path
);
36 return cloud_print_server_url
.ReplaceComponents(replacements
);
39 GURL
GetUrlForSubmit(const GURL
& cloud_print_server_url
) {
40 std::string
path(AppendPathToUrl(cloud_print_server_url
, "submit"));
41 GURL::Replacements replacements
;
42 replacements
.SetPathStr(path
);
43 return cloud_print_server_url
.ReplaceComponents(replacements
);
46 bool ParseResponseJSON(const std::string
& response_data
,
48 DictionaryValue
** response_dict
) {
49 scoped_ptr
<Value
> message_value(base::JSONReader::Read(response_data
));
50 if (!message_value
.get())
53 if (!message_value
->IsType(Value::TYPE_DICTIONARY
))
56 scoped_ptr
<DictionaryValue
> response_dict_local(
57 static_cast<DictionaryValue
*>(message_value
.release()));
59 !response_dict_local
->GetBoolean(cloud_print::kSuccessValue
, succeeded
))
62 *response_dict
= response_dict_local
.release();
66 void AddMultipartValueForUpload(const std::string
& value_name
,
67 const std::string
& value
,
68 const std::string
& mime_boundary
,
69 const std::string
& content_type
,
70 std::string
* post_data
) {
72 // First line is the boundary
73 post_data
->append("--" + mime_boundary
+ "\r\n");
74 // Next line is the Content-disposition
75 post_data
->append(StringPrintf("Content-Disposition: form-data; "
76 "name=\"%s\"\r\n", value_name
.c_str()));
77 if (!content_type
.empty()) {
78 // If Content-type is specified, the next line is that
79 post_data
->append(StringPrintf("Content-Type: %s\r\n",
80 content_type
.c_str()));
82 // Leave an empty line and append the value.
83 post_data
->append(StringPrintf("\r\n%s\r\n", value
.c_str()));
86 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits).
87 void CreateMimeBoundaryForUpload(std::string
* out
) {
88 int r1
= base::RandInt(0, kint32max
);
89 int r2
= base::RandInt(0, kint32max
);
90 base::SStringPrintf(out
, "---------------------------%08X%08X", r1
, r2
);
93 } // namespace cloud_print