| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "cloud_print/service/win/service_state.h" | |
| 6 | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/string_util.h" | |
| 12 #include "base/utf_string_conversions.h" | |
| 13 #include "net/base/escape.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/url_request/url_request.h" | |
| 16 #include "net/url_request/url_request_context.h" | |
| 17 #include "net/url_request/url_request_context_builder.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const char kCloudPrintJsonName[] = "cloud_print"; | |
| 22 const char kEnabledOptionName[] = "enabled"; | |
| 23 | |
| 24 const char kEmailOptionName[] = "email"; | |
| 25 const char kPasswordOptionName[] = "password"; | |
| 26 const char kProxyIdOptionName[] = "proxy_id"; | |
| 27 const char kRobotEmailOptionName[] = "robot_email"; | |
| 28 const char kRobotTokenOptionName[] = "robot_refresh_token"; | |
| 29 const char kAuthTokenOptionName[] = "auth_token"; | |
| 30 const char kXmppAuthTokenOptionName[] = "xmpp_auth_token"; | |
| 31 | |
| 32 const char kClientLoginUrl[] = "https://www.google.com/accounts/ClientLogin"; | |
| 33 | |
| 34 const int64 kRequestTimeoutMs = 10 * 1000; | |
| 35 | |
| 36 class ServiceStateURLRequestDelegate : public net::URLRequest::Delegate { | |
| 37 public: | |
| 38 virtual void OnResponseStarted(net::URLRequest* request) { | |
| 39 if (request->GetResponseCode() == 200) { | |
| 40 Read(request); | |
| 41 if (request->status().is_io_pending()) | |
| 42 return; | |
| 43 } | |
| 44 request->Cancel(); | |
| 45 }; | |
| 46 | |
| 47 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) { | |
| 48 Read(request); | |
| 49 if (!request->status().is_io_pending()) | |
| 50 MessageLoop::current()->Quit(); | |
| 51 }; | |
| 52 | |
| 53 const std::string& data() const { | |
| 54 return data_; | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 void Read(net::URLRequest* request) { | |
| 59 // Read as many bytes as are available synchronously. | |
| 60 const int kBufSize = 100000; | |
| 61 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize)); | |
| 62 int num_bytes = 0; | |
| 63 while (request->Read(buf, kBufSize, &num_bytes)) { | |
| 64 data_.append(buf->data(), buf->data() + num_bytes); | |
| 65 } | |
| 66 } | |
| 67 std::string data_; | |
| 68 }; | |
| 69 | |
| 70 | |
| 71 void SetNotEmptyJsonString(base::DictionaryValue* dictionary, | |
| 72 const std::string& name, | |
| 73 const std::string& value) { | |
| 74 if (!value.empty()) | |
| 75 dictionary->SetString(name, value); | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 ServiceState::ServiceState() { | |
| 81 Reset(); | |
| 82 } | |
| 83 | |
| 84 ServiceState::~ServiceState() { | |
| 85 } | |
| 86 | |
| 87 void ServiceState::Reset() { | |
| 88 email_.clear(); | |
| 89 proxy_id_.clear(); | |
| 90 robot_email_.clear(); | |
| 91 robot_token_.clear(); | |
| 92 auth_token_.clear(); | |
| 93 xmpp_auth_token_.clear(); | |
| 94 } | |
| 95 | |
| 96 bool ServiceState::FromString(const std::string& json) { | |
| 97 Reset(); | |
| 98 scoped_ptr<base::Value> data(base::JSONReader::Read(json)); | |
| 99 if (!data.get()) | |
| 100 return false; | |
| 101 | |
| 102 const base::DictionaryValue* services = NULL; | |
| 103 if (!data->GetAsDictionary(&services)) | |
| 104 return false; | |
| 105 | |
| 106 base::DictionaryValue* cloud_print = NULL; | |
| 107 if (!services->GetDictionary(kCloudPrintJsonName, &cloud_print)) | |
| 108 return false; | |
| 109 | |
| 110 bool valid_file = true; | |
| 111 // Don't exit on fail. Collect all data for re-use by user later. | |
| 112 if (!cloud_print->GetBoolean(kEnabledOptionName, &valid_file)) | |
| 113 valid_file = false; | |
| 114 | |
| 115 cloud_print->GetString(kEmailOptionName, &email_); | |
| 116 cloud_print->GetString(kProxyIdOptionName, &proxy_id_); | |
| 117 cloud_print->GetString(kRobotEmailOptionName, &robot_email_); | |
| 118 cloud_print->GetString(kRobotTokenOptionName, &robot_token_); | |
| 119 cloud_print->GetString(kAuthTokenOptionName, &auth_token_); | |
| 120 cloud_print->GetString(kXmppAuthTokenOptionName, &xmpp_auth_token_); | |
| 121 | |
| 122 return valid_file && IsValid(); | |
| 123 } | |
| 124 | |
| 125 bool ServiceState::IsValid() const { | |
| 126 if (email_.empty() || proxy_id_.empty()) | |
| 127 return false; | |
| 128 bool valid_robot = !robot_email_.empty() && !robot_token_.empty(); | |
| 129 bool valid_auth = !auth_token_.empty() && !xmpp_auth_token_.empty(); | |
| 130 return valid_robot || valid_auth; | |
| 131 } | |
| 132 | |
| 133 std::string ServiceState::ToString() { | |
| 134 scoped_ptr<base::DictionaryValue> services(new DictionaryValue()); | |
| 135 | |
| 136 scoped_ptr<base::DictionaryValue> cloud_print(new DictionaryValue()); | |
| 137 cloud_print->SetBoolean(kEnabledOptionName, true); | |
| 138 | |
| 139 SetNotEmptyJsonString(cloud_print.get(), kEmailOptionName, email_); | |
| 140 SetNotEmptyJsonString(cloud_print.get(), kProxyIdOptionName, proxy_id_); | |
| 141 SetNotEmptyJsonString(cloud_print.get(), kRobotEmailOptionName, robot_email_); | |
| 142 SetNotEmptyJsonString(cloud_print.get(), kRobotTokenOptionName, robot_token_); | |
| 143 SetNotEmptyJsonString(cloud_print.get(), kAuthTokenOptionName, auth_token_); | |
| 144 SetNotEmptyJsonString(cloud_print.get(), kXmppAuthTokenOptionName, | |
| 145 xmpp_auth_token_); | |
| 146 | |
| 147 services->Set(kCloudPrintJsonName, cloud_print.release()); | |
| 148 | |
| 149 std::string json; | |
| 150 base::JSONWriter::WriteWithOptions(services.get(), | |
| 151 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
| 152 &json); | |
| 153 return json; | |
| 154 } | |
| 155 | |
| 156 std::string ServiceState::LoginToGoogle(const std::string& service, | |
| 157 const std::string& email, | |
| 158 const std::string& password) { | |
| 159 MessageLoop loop(MessageLoop::TYPE_IO); | |
| 160 | |
| 161 net::URLRequestContextBuilder builder; | |
| 162 scoped_ptr<net::URLRequestContext> context(builder.Build()); | |
| 163 | |
| 164 ServiceStateURLRequestDelegate fetcher_delegate; | |
| 165 GURL url(kClientLoginUrl); | |
| 166 | |
| 167 std::string post_body; | |
| 168 post_body += "accountType=GOOGLE"; | |
| 169 post_body += "&Email=" + net::EscapeUrlEncodedData(email, true); | |
| 170 post_body += "&Passwd=" + net::EscapeUrlEncodedData(password, true); | |
| 171 post_body += "&source=" + net::EscapeUrlEncodedData("CP-Service", true); | |
| 172 post_body += "&service=" + net::EscapeUrlEncodedData(service, true); | |
| 173 | |
| 174 net::URLRequest request(url, &fetcher_delegate); | |
| 175 | |
| 176 request.AppendBytesToUpload(post_body.c_str(), post_body.size()); | |
| 177 request.SetExtraRequestHeaderByName( | |
| 178 "Content-Type", "application/x-www-form-urlencoded", true); | |
| 179 request.set_context(context.get()); | |
| 180 request.set_method("POST"); | |
| 181 request.Start(); | |
| 182 | |
| 183 MessageLoop::current()->PostDelayedTask( | |
| 184 FROM_HERE, MessageLoop::QuitClosure(), kRequestTimeoutMs); | |
| 185 | |
| 186 MessageLoop::current()->Run(); | |
| 187 | |
| 188 const char kAuthStart[] = "Auth="; | |
| 189 std::vector<std::string> lines; | |
| 190 Tokenize(fetcher_delegate.data(), "\r\n", &lines); | |
| 191 for (size_t i = 0; i < lines.size(); ++i) { | |
| 192 std::vector<std::string> tokens; | |
| 193 if (StartsWithASCII(lines[i], kAuthStart, false)) | |
| 194 return lines[i].substr(arraysize(kAuthStart) - 1); | |
| 195 } | |
| 196 | |
| 197 return std::string(); | |
| 198 } | |
| 199 | |
| 200 bool ServiceState::Configure(const std::string& email, | |
| 201 const std::string& password, | |
| 202 const std::string& proxy_id) { | |
| 203 robot_token_.clear(); | |
| 204 robot_email_.clear(); | |
| 205 email_ = email; | |
| 206 proxy_id_ = proxy_id; | |
| 207 auth_token_ = LoginToGoogle("cloudprint", email_, password); | |
| 208 xmpp_auth_token_ = LoginToGoogle("chromiumsync", email_, password); | |
| 209 return IsValid(); | |
| 210 } | |
| 211 | |
| OLD | NEW |