diff options
author | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-13 06:08:46 +0000 |
---|---|---|
committer | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-13 06:08:46 +0000 |
commit | b106ca53e5f9159acfd33498850a535dcf7cfbb7 (patch) | |
tree | 1b8bb9a3844cfebe8ef2737baf5673728763ebe6 /chrome/common/cloud_print | |
parent | 653007448cee9796264581fdd087d2353c3e03a6 (diff) | |
download | chromium_src-b106ca53e5f9159acfd33498850a535dcf7cfbb7.zip chromium_src-b106ca53e5f9159acfd33498850a535dcf7cfbb7.tar.gz chromium_src-b106ca53e5f9159acfd33498850a535dcf7cfbb7.tar.bz2 |
Add Chrome To Mobile Service and Views Page Action.
Implements the Chrome To Mobile extension in Chrome.
List the user's mobile devices via the Cloud Print server.
Add a page action icon when the service reports 1+ devices.
Add a bubble to send the current page URL / MHTML snapshot.
The bubble shows a radio group for multiple devices.
(or it shows a single device as part of the title label)
The bubble also shows a checkbox to send an offline copy.
Send URLFetcher requests to GET/POST the URL/Snapshot.
The bubble shows "Sending..."/"Sent"/ error request status.
BUG=102709
TEST=New Chrome To Mobile bubble works as expected :)
Review URL: http://codereview.chromium.org/9443007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126343 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/cloud_print')
-rwxr-xr-x | chrome/common/cloud_print/cloud_print_helpers.cc | 94 | ||||
-rwxr-xr-x | chrome/common/cloud_print/cloud_print_helpers.h | 56 |
2 files changed, 150 insertions, 0 deletions
diff --git a/chrome/common/cloud_print/cloud_print_helpers.cc b/chrome/common/cloud_print/cloud_print_helpers.cc new file mode 100755 index 0000000..cebe4a3 --- /dev/null +++ b/chrome/common/cloud_print/cloud_print_helpers.cc @@ -0,0 +1,94 @@ +// 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. + +#include "chrome/common/cloud_print/cloud_print_helpers.h" + +#include "base/json/json_reader.h" +#include "base/logging.h" +#include "base/memory/scoped_ptr.h" +#include "base/rand_util.h" +#include "base/stringprintf.h" +#include "base/values.h" +#include "chrome/common/guid.h" +#include "googleurl/src/gurl.h" + +namespace cloud_print { + +const char kPrinterListValue[] = "printers"; +const char kSuccessValue[] = "success"; + +// Certain cloud print requests require Chrome's X-CloudPrint-Proxy header. +const char kChromeCloudPrintProxyHeader[] = "X-CloudPrint-Proxy: Chrome"; + +std::string AppendPathToUrl(const GURL& url, const std::string& path) { + DCHECK_NE(path[0], '/'); + std::string ret = url.path(); + if (url.has_path() && (ret[ret.length() - 1] != '/')) + ret += '/'; + ret += path; + return ret; +} + +GURL GetUrlForSearch(const GURL& cloud_print_server_url) { + std::string path(AppendPathToUrl(cloud_print_server_url, "search")); + GURL::Replacements replacements; + replacements.SetPathStr(path); + return cloud_print_server_url.ReplaceComponents(replacements); +} + +GURL GetUrlForSubmit(const GURL& cloud_print_server_url) { + std::string path(AppendPathToUrl(cloud_print_server_url, "submit")); + GURL::Replacements replacements; + replacements.SetPathStr(path); + return cloud_print_server_url.ReplaceComponents(replacements); +} + +bool ParseResponseJSON(const std::string& response_data, + bool* succeeded, + DictionaryValue** response_dict) { + scoped_ptr<Value> message_value(base::JSONReader::Read(response_data, false)); + if (!message_value.get()) + return false; + + if (!message_value->IsType(Value::TYPE_DICTIONARY)) + return false; + + scoped_ptr<DictionaryValue> response_dict_local( + static_cast<DictionaryValue*>(message_value.release())); + if (succeeded && + !response_dict_local->GetBoolean(cloud_print::kSuccessValue, succeeded)) + *succeeded = false; + if (response_dict) + *response_dict = response_dict_local.release(); + return true; +} + +void AddMultipartValueForUpload(const std::string& value_name, + const std::string& value, + const std::string& mime_boundary, + const std::string& content_type, + std::string* post_data) { + DCHECK(post_data); + // First line is the boundary + post_data->append("--" + mime_boundary + "\r\n"); + // Next line is the Content-disposition + post_data->append(StringPrintf("Content-Disposition: form-data; " + "name=\"%s\"\r\n", value_name.c_str())); + if (!content_type.empty()) { + // If Content-type is specified, the next line is that + post_data->append(StringPrintf("Content-Type: %s\r\n", + content_type.c_str())); + } + // Leave an empty line and append the value. + post_data->append(StringPrintf("\r\n%s\r\n", value.c_str())); +} + +// Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). +void CreateMimeBoundaryForUpload(std::string* out) { + int r1 = base::RandInt(0, kint32max); + int r2 = base::RandInt(0, kint32max); + base::SStringPrintf(out, "---------------------------%08X%08X", r1, r2); +} + +} // namespace cloud_print diff --git a/chrome/common/cloud_print/cloud_print_helpers.h b/chrome/common/cloud_print/cloud_print_helpers.h new file mode 100755 index 0000000..1b4a15e --- /dev/null +++ b/chrome/common/cloud_print/cloud_print_helpers.h @@ -0,0 +1,56 @@ +// 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_COMMON_CLOUD_PRINT_CLOUD_PRINT_HELPERS_H_ +#define CHROME_COMMON_CLOUD_PRINT_CLOUD_PRINT_HELPERS_H_ + +#include <string> + +class GURL; + +namespace base { +class DictionaryValue; +} + +// Helper consts and methods for both cloud print and chrome browser. +namespace cloud_print { + +// Values in the respone JSON from the cloud print server +extern const char kPrinterListValue[]; +extern const char kSuccessValue[]; + +extern const char kChromeCloudPrintProxyHeader[]; + +// Appends a relative path to the url making sure to append a '/' if the +// URL's path does not end with a slash. It is assumed that |path| does not +// begin with a '/'. +// NOTE: Since we ALWAYS want to append here, we simply append the path string +// instead of calling url_utils::ResolveRelative. The input |url| may or may not +// contain a '/' at the end. +std::string AppendPathToUrl(const GURL& url, const std::string& path); + +GURL GetUrlForSearch(const GURL& cloud_print_server_url); +GURL GetUrlForSubmit(const GURL& cloud_print_server_url); + +// Parses the response data for any cloud print server request. The method +// returns false if there was an error in parsing the JSON. The succeeded +// value returns the value of the "success" value in the response JSON. +// Returns the response as a dictionary value. +bool ParseResponseJSON(const std::string& response_data, + bool* succeeded, + base::DictionaryValue** response_dict); + +// Prepares one value as part of a multi-part upload request. +void AddMultipartValueForUpload(const std::string& value_name, + const std::string& value, + const std::string& mime_boundary, + const std::string& content_type, + std::string* post_data); + +// Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). +void CreateMimeBoundaryForUpload(std::string *out); + +} // namespace cloud_print + +#endif // CHROME_COMMON_CLOUD_PRINT_CLOUD_PRINT_HELPERS_H_ |