diff options
Diffstat (limited to 'cloud_print/service/win')
-rw-r--r-- | cloud_print/service/win/DEPS | 3 | ||||
-rw-r--r-- | cloud_print/service/win/chrome_launcher.cc | 2 | ||||
-rw-r--r-- | cloud_print/service/win/cloud_print_service.cc | 4 | ||||
-rw-r--r-- | cloud_print/service/win/service.gyp | 66 | ||||
-rw-r--r-- | cloud_print/service/win/service_state.cc | 211 | ||||
-rw-r--r-- | cloud_print/service/win/service_state.h | 103 | ||||
-rw-r--r-- | cloud_print/service/win/service_state_unittest.cc | 89 | ||||
-rw-r--r-- | cloud_print/service/win/service_switches.cc | 16 | ||||
-rw-r--r-- | cloud_print/service/win/service_switches.h | 19 |
9 files changed, 510 insertions, 3 deletions
diff --git a/cloud_print/service/win/DEPS b/cloud_print/service/win/DEPS new file mode 100644 index 0000000..8fa9d48 --- /dev/null +++ b/cloud_print/service/win/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+net", +] diff --git a/cloud_print/service/win/chrome_launcher.cc b/cloud_print/service/win/chrome_launcher.cc index 5921fd0..8b984f0 100644 --- a/cloud_print/service/win/chrome_launcher.cc +++ b/cloud_print/service/win/chrome_launcher.cc @@ -12,7 +12,7 @@ #include "base/win/registry.h" #include "base/win/scoped_handle.h" #include "base/win/scoped_process_information.h" -#include "cloud_print/service/service_switches.h" +#include "cloud_print/service/win/service_switches.h" namespace { diff --git a/cloud_print/service/win/cloud_print_service.cc b/cloud_print/service/win/cloud_print_service.cc index f87c2888..c0948c4 100644 --- a/cloud_print/service/win/cloud_print_service.cc +++ b/cloud_print/service/win/cloud_print_service.cc @@ -15,10 +15,10 @@ #include "base/path_service.h" #include "base/string_util.h" #include "base/win/scoped_handle.h" -#include "cloud_print/service/service_state.h" -#include "cloud_print/service/service_switches.h" #include "cloud_print/service/win/chrome_launcher.h" #include "cloud_print/service/win/resource.h" +#include "cloud_print/service/win/service_state.h" +#include "cloud_print/service/win/service_switches.h" namespace { diff --git a/cloud_print/service/win/service.gyp b/cloud_print/service/win/service.gyp new file mode 100644 index 0000000..b365e83 --- /dev/null +++ b/cloud_print/service/win/service.gyp @@ -0,0 +1,66 @@ +# 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. +{ + 'target_defaults': { + 'variables': { + 'chromium_code': 1, + }, + 'include_dirs': [ + '../../..', + ], + }, + 'targets': [ + { + 'target_name': 'cloud_print_service_lib', + 'type': 'static_library', + 'dependencies': [ + '../../../base/base.gyp:base', + '../../../build/temp_gyp/googleurl.gyp:googleurl', + '../../../net/net.gyp:net', + ], + 'sources': [ + 'chrome_launcher.cc', + 'chrome_launcher.h', + 'service_state.cc', + 'service_state.h', + 'service_switches.cc', + 'service_switches.h', + ] + }, + { + 'target_name': 'cloud_print_service', + 'type': 'executable', + 'sources': [ + 'cloud_print_service.cc', + 'cloud_print_service.h', + 'cloud_print_service.rc', + 'resource.h', + ], + 'dependencies': [ + 'cloud_print_service_lib', + ], + 'msvs_settings': { + 'VCLinkerTool': { + 'SubSystem': '1', # Set /SUBSYSTEM:CONSOLE + 'UACExecutionLevel': '2', # /level='requireAdministrator' + }, + }, + }, + { + 'target_name': 'cloud_print_service_unittests', + 'type': 'executable', + 'sources': [ + 'service_state_unittest.cc', + ], + 'dependencies': [ + '../../../base/base.gyp:run_all_unittests', + '../../../base/base.gyp:base', + '../../../base/base.gyp:test_support_base', + '../../../testing/gmock.gyp:gmock', + '../../../testing/gtest.gyp:gtest', + 'cloud_print_service_lib', + ], + }, + ], +} diff --git a/cloud_print/service/win/service_state.cc b/cloud_print/service/win/service_state.cc new file mode 100644 index 0000000..8641000 --- /dev/null +++ b/cloud_print/service/win/service_state.cc @@ -0,0 +1,211 @@ +// 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 "cloud_print/service/win/service_state.h" + +#include "base/json/json_reader.h" +#include "base/json/json_writer.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/string_util.h" +#include "base/utf_string_conversions.h" +#include "net/base/escape.h" +#include "net/base/io_buffer.h" +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_context.h" +#include "net/url_request/url_request_context_builder.h" + +namespace { + +const char kCloudPrintJsonName[] = "cloud_print"; +const char kEnabledOptionName[] = "enabled"; + +const char kEmailOptionName[] = "email"; +const char kPasswordOptionName[] = "password"; +const char kProxyIdOptionName[] = "proxy_id"; +const char kRobotEmailOptionName[] = "robot_email"; +const char kRobotTokenOptionName[] = "robot_refresh_token"; +const char kAuthTokenOptionName[] = "auth_token"; +const char kXmppAuthTokenOptionName[] = "xmpp_auth_token"; + +const char kClientLoginUrl[] = "https://www.google.com/accounts/ClientLogin"; + +const int64 kRequestTimeoutMs = 10 * 1000; + +class ServiceStateURLRequestDelegate : public net::URLRequest::Delegate { + public: + virtual void OnResponseStarted(net::URLRequest* request) { + if (request->GetResponseCode() == 200) { + Read(request); + if (request->status().is_io_pending()) + return; + } + request->Cancel(); + }; + + virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) { + Read(request); + if (!request->status().is_io_pending()) + MessageLoop::current()->Quit(); + }; + + const std::string& data() const { + return data_; + } + + private: + void Read(net::URLRequest* request) { + // Read as many bytes as are available synchronously. + const int kBufSize = 100000; + scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufSize)); + int num_bytes = 0; + while (request->Read(buf, kBufSize, &num_bytes)) { + data_.append(buf->data(), buf->data() + num_bytes); + } + } + std::string data_; +}; + + +void SetNotEmptyJsonString(base::DictionaryValue* dictionary, + const std::string& name, + const std::string& value) { + if (!value.empty()) + dictionary->SetString(name, value); +} + +} // namespace + +ServiceState::ServiceState() { + Reset(); +} + +ServiceState::~ServiceState() { +} + +void ServiceState::Reset() { + email_.clear(); + proxy_id_.clear(); + robot_email_.clear(); + robot_token_.clear(); + auth_token_.clear(); + xmpp_auth_token_.clear(); +} + +bool ServiceState::FromString(const std::string& json) { + Reset(); + scoped_ptr<base::Value> data(base::JSONReader::Read(json)); + if (!data.get()) + return false; + + const base::DictionaryValue* services = NULL; + if (!data->GetAsDictionary(&services)) + return false; + + base::DictionaryValue* cloud_print = NULL; + if (!services->GetDictionary(kCloudPrintJsonName, &cloud_print)) + return false; + + bool valid_file = true; + // Don't exit on fail. Collect all data for re-use by user later. + if (!cloud_print->GetBoolean(kEnabledOptionName, &valid_file)) + valid_file = false; + + cloud_print->GetString(kEmailOptionName, &email_); + cloud_print->GetString(kProxyIdOptionName, &proxy_id_); + cloud_print->GetString(kRobotEmailOptionName, &robot_email_); + cloud_print->GetString(kRobotTokenOptionName, &robot_token_); + cloud_print->GetString(kAuthTokenOptionName, &auth_token_); + cloud_print->GetString(kXmppAuthTokenOptionName, &xmpp_auth_token_); + + return valid_file && IsValid(); +} + +bool ServiceState::IsValid() const { + if (email_.empty() || proxy_id_.empty()) + return false; + bool valid_robot = !robot_email_.empty() && !robot_token_.empty(); + bool valid_auth = !auth_token_.empty() && !xmpp_auth_token_.empty(); + return valid_robot || valid_auth; +} + +std::string ServiceState::ToString() { + scoped_ptr<base::DictionaryValue> services(new DictionaryValue()); + + scoped_ptr<base::DictionaryValue> cloud_print(new DictionaryValue()); + cloud_print->SetBoolean(kEnabledOptionName, true); + + SetNotEmptyJsonString(cloud_print.get(), kEmailOptionName, email_); + SetNotEmptyJsonString(cloud_print.get(), kProxyIdOptionName, proxy_id_); + SetNotEmptyJsonString(cloud_print.get(), kRobotEmailOptionName, robot_email_); + SetNotEmptyJsonString(cloud_print.get(), kRobotTokenOptionName, robot_token_); + SetNotEmptyJsonString(cloud_print.get(), kAuthTokenOptionName, auth_token_); + SetNotEmptyJsonString(cloud_print.get(), kXmppAuthTokenOptionName, + xmpp_auth_token_); + + services->Set(kCloudPrintJsonName, cloud_print.release()); + + std::string json; + base::JSONWriter::WriteWithOptions(services.get(), + base::JSONWriter::OPTIONS_PRETTY_PRINT, + &json); + return json; +} + +std::string ServiceState::LoginToGoogle(const std::string& service, + const std::string& email, + const std::string& password) { + MessageLoop loop(MessageLoop::TYPE_IO); + + net::URLRequestContextBuilder builder; + scoped_ptr<net::URLRequestContext> context(builder.Build()); + + ServiceStateURLRequestDelegate fetcher_delegate; + GURL url(kClientLoginUrl); + + std::string post_body; + post_body += "accountType=GOOGLE"; + post_body += "&Email=" + net::EscapeUrlEncodedData(email, true); + post_body += "&Passwd=" + net::EscapeUrlEncodedData(password, true); + post_body += "&source=" + net::EscapeUrlEncodedData("CP-Service", true); + post_body += "&service=" + net::EscapeUrlEncodedData(service, true); + + net::URLRequest request(url, &fetcher_delegate); + + request.AppendBytesToUpload(post_body.c_str(), post_body.size()); + request.SetExtraRequestHeaderByName( + "Content-Type", "application/x-www-form-urlencoded", true); + request.set_context(context.get()); + request.set_method("POST"); + request.Start(); + + MessageLoop::current()->PostDelayedTask( + FROM_HERE, MessageLoop::QuitClosure(), kRequestTimeoutMs); + + MessageLoop::current()->Run(); + + const char kAuthStart[] = "Auth="; + std::vector<std::string> lines; + Tokenize(fetcher_delegate.data(), "\r\n", &lines); + for (size_t i = 0; i < lines.size(); ++i) { + std::vector<std::string> tokens; + if (StartsWithASCII(lines[i], kAuthStart, false)) + return lines[i].substr(arraysize(kAuthStart) - 1); + } + + return std::string(); +} + +bool ServiceState::Configure(const std::string& email, + const std::string& password, + const std::string& proxy_id) { + robot_token_.clear(); + robot_email_.clear(); + email_ = email; + proxy_id_ = proxy_id; + auth_token_ = LoginToGoogle("cloudprint", email_, password); + xmpp_auth_token_ = LoginToGoogle("chromiumsync", email_, password); + return IsValid(); +} + diff --git a/cloud_print/service/win/service_state.h b/cloud_print/service/win/service_state.h new file mode 100644 index 0000000..ef037bb --- /dev/null +++ b/cloud_print/service/win/service_state.h @@ -0,0 +1,103 @@ +// 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 CLOUD_PRINT_SERVICE_SERVICE_STATE_H_ +#define CLOUD_PRINT_SERVICE_SERVICE_STATE_H_ + +#include <string> + +#include "base/file_path.h" +#include "base/memory/scoped_ptr.h" +#include "base/values.h" + +class FilePath; + +// Manages Cloud Print part of Service State. +class ServiceState { + public: + ServiceState(); + virtual ~ServiceState(); + + void Reset(); + + // Initialize object from json. + bool FromString(const std::string& json); + + // Returns object state as json. + std::string ToString(); + + // Setups object using data provided by delegate. + bool Configure(const std::string& email, + const std::string& password, + const std::string& proxy_id); + + // Returns authentication token provided by Google server. + virtual std::string LoginToGoogle(const std::string& service, + const std::string& email, + const std::string& password); + + // Returns true of object state is valid. + bool IsValid() const; + + std::string email() const { + return email_; + }; + + std::string proxy_id() const { + return proxy_id_; + }; + + std::string robot_email() const { + return robot_email_; + }; + + std::string robot_token() const { + return robot_token_; + }; + + std::string auth_token() const { + return auth_token_; + }; + + std::string xmpp_auth_token() const { + return xmpp_auth_token_; + }; + + void set_email(const std::string& value) { + email_ = value; + }; + + void set_proxy_id(const std::string& value) { + proxy_id_ = value; + }; + + void set_robot_email(const std::string& value) { + robot_email_ = value; + }; + + void set_robot_token(const std::string& value) { + robot_token_ = value; + }; + + void set_auth_token(const std::string& value) { + auth_token_ = value; + }; + + void set_xmpp_auth_token(const std::string& value) { + xmpp_auth_token_ = value; + }; + + private: + std::string email_; + std::string proxy_id_; + std::string robot_email_; + std::string robot_token_; + std::string auth_token_; + std::string xmpp_auth_token_; + + DISALLOW_COPY_AND_ASSIGN(ServiceState); +}; + +#endif // CLOUD_PRINT_SERVICE_SERVICE_STATE_H_ + diff --git a/cloud_print/service/win/service_state_unittest.cc b/cloud_print/service/win/service_state_unittest.cc new file mode 100644 index 0000000..00c651e --- /dev/null +++ b/cloud_print/service/win/service_state_unittest.cc @@ -0,0 +1,89 @@ +// 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 "cloud_print/service/win/service_state.h" + +#include "base/string_util.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +using ::testing::_; +using ::testing::Exactly; +using ::testing::Return; + +TEST(ServiceStateTest, Empty) { + ServiceState state; + EXPECT_FALSE(state.IsValid()); +} + +TEST(ServiceStateTest, ToString) { + ServiceState state; + EXPECT_STREQ("{\"cloud_print\": {\"enabled\": true}}", + CollapseWhitespaceASCII(state.ToString(), true).c_str()); + state.set_email("test@gmail.com"); + state.set_proxy_id("proxy"); + state.set_robot_email("robot@gmail.com"); + state.set_robot_token("abc"); + state.set_auth_token("token1"); + state.set_xmpp_auth_token("token2"); + EXPECT_TRUE(state.IsValid()); + EXPECT_STREQ("{\"cloud_print\": {\"auth_token\": \"token1\",\"email\": " + "\"test@gmail.com\",\"enabled\": true,\"proxy_id\": \"proxy\"," + "\"robot_email\": \"robot@gmail.com\",\"robot_refresh_token\": " + "\"abc\",\"xmpp_auth_token\": \"token2\"}}", + CollapseWhitespaceASCII(state.ToString(), true).c_str()); +} + +TEST(ServiceStateTest, FromString) { + ServiceState state; + // Syntax error. + EXPECT_FALSE(state.FromString("<\"cloud_print\": {\"enabled\": true}}")); + // No data. + EXPECT_FALSE(state.FromString("{\"cloud_print\": {\"enabled\": true}}")); + EXPECT_FALSE(state.FromString( + "{\"cloud_print\": {\"email\": \"test@gmail.com\"}}")); + EXPECT_STREQ("test@gmail.com", state.email().c_str()); + + // Good state. + EXPECT_TRUE(state.FromString( + "{\"cloud_print\": {\"email\": \"test2@gmail.com\",\"enabled\": true,\"" + "proxy_id\": \"proxy\",\"robot_email\": \"robot@gmail.com\",\"" + "robot_refresh_token\": \"abc\"}}")); + EXPECT_STREQ("test2@gmail.com", state.email().c_str()); + EXPECT_STREQ("proxy", state.proxy_id().c_str()); + EXPECT_STREQ("robot@gmail.com", state.robot_email().c_str()); + EXPECT_STREQ("abc", state.robot_token().c_str()); +} + +class ServiceStateMock : public ServiceState { + public: + ServiceStateMock() {} + + MOCK_METHOD3(LoginToGoogle, + std::string(const std::string& service, + const std::string& email, + const std::string& password)); + + private: + DISALLOW_COPY_AND_ASSIGN(ServiceStateMock); +}; + +TEST(ServiceStateTest, Configure) { + ServiceStateMock state; + state.set_email("test1@gmail.com"); + state.set_proxy_id("id1"); + + EXPECT_CALL(state, LoginToGoogle("cloudprint", "test2@gmail.com", "abc")) + .Times(Exactly(1)) + .WillOnce(Return("auth1")); + EXPECT_CALL(state, LoginToGoogle("chromiumsync", "test2@gmail.com", "abc")) + .Times(Exactly(1)) + .WillOnce(Return("auth2")); + + EXPECT_TRUE(state.Configure("test2@gmail.com", "abc", "id2")); + + EXPECT_STREQ("id2", state.proxy_id().c_str()); + EXPECT_STREQ("auth1", state.auth_token().c_str()); + EXPECT_STREQ("auth2", state.xmpp_auth_token().c_str()); +} diff --git a/cloud_print/service/win/service_switches.cc b/cloud_print/service/win/service_switches.cc new file mode 100644 index 0000000..296eae0 --- /dev/null +++ b/cloud_print/service/win/service_switches.cc @@ -0,0 +1,16 @@ +// 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 "cloud_print/service/win/service_switches.h" + +const char kChromeTypeSwitch[] = "type"; +const char kConsoleSwitch[] = "console"; +const char kInstallSwitch[] = "install"; +const char kQuietSwitch[] = "quiet"; +const char kServiceSwitch[] = "service"; +const char kStartSwitch[] = "start"; +const char kStopSwitch[] = "stop"; +const char kUninstallSwitch[] = "uninstall"; +const char kUserDataDirSwitch[] = "user-data-dir"; + diff --git a/cloud_print/service/win/service_switches.h b/cloud_print/service/win/service_switches.h new file mode 100644 index 0000000..17af981 --- /dev/null +++ b/cloud_print/service/win/service_switches.h @@ -0,0 +1,19 @@ +// 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 CLOUD_PRINT_SERVICE_SERVICE_SWITCHES_H_ +#define CLOUD_PRINT_SERVICE_SERVICE_SWITCHES_H_ + +extern const char kChromeTypeSwitch[]; +extern const char kConsoleSwitch[]; +extern const char kInstallSwitch[]; +extern const char kQuietSwitch[]; +extern const char kServiceSwitch[]; +extern const char kStartSwitch[]; +extern const char kStopSwitch[]; +extern const char kUninstallSwitch[]; +extern const char kUserDataDirSwitch[]; + +#endif // CLOUD_PRINT_SERVICE_SERVICE_SWITCHES_H_ + |