Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / cloud_print_private / cloud_print_private_api.cc
blob74fdde849457a8e62bf750b0b81438537006c492
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/browser/extensions/api/cloud_print_private/cloud_print_private_api.h"
7 #include <string>
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
11 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
12 #include "chrome/common/extensions/api/cloud_print_private.h"
13 #include "google_apis/google_api_keys.h"
14 #include "net/base/net_util.h"
16 namespace extensions {
18 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance_ = NULL;
20 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance() {
21 return instance_;
24 CloudPrintTestsDelegate::CloudPrintTestsDelegate() {
25 instance_ = this;
28 CloudPrintTestsDelegate::~CloudPrintTestsDelegate() {
29 instance_ = NULL;
32 CloudPrintPrivateSetupConnectorFunction::
33 CloudPrintPrivateSetupConnectorFunction() {
36 CloudPrintPrivateSetupConnectorFunction::
37 ~CloudPrintPrivateSetupConnectorFunction() {
40 bool CloudPrintPrivateSetupConnectorFunction::RunAsync() {
41 #if defined(ENABLE_PRINT_PREVIEW)
42 using api::cloud_print_private::SetupConnector::Params;
43 scoped_ptr<Params> params(Params::Create(*args_));
44 if (CloudPrintTestsDelegate::instance()) {
45 CloudPrintTestsDelegate::instance()->SetupConnector(
46 params->user_email,
47 params->robot_email,
48 params->credentials,
49 params->user_settings);
50 } else {
51 CloudPrintProxyService* service =
52 CloudPrintProxyServiceFactory::GetForProfile(GetProfile());
53 if (!service)
54 return false;
55 scoped_ptr<base::DictionaryValue> user_setings(
56 params->user_settings.ToValue());
57 service->EnableForUserWithRobot(params->credentials,
58 params->robot_email,
59 params->user_email,
60 *user_setings);
62 SendResponse(true);
63 return true;
64 #else
65 return false;
66 #endif
69 CloudPrintPrivateGetHostNameFunction::CloudPrintPrivateGetHostNameFunction() {
72 CloudPrintPrivateGetHostNameFunction::~CloudPrintPrivateGetHostNameFunction() {
75 bool CloudPrintPrivateGetHostNameFunction::RunAsync() {
76 SetResult(new base::StringValue(
77 CloudPrintTestsDelegate::instance() ?
78 CloudPrintTestsDelegate::instance()->GetHostName() :
79 net::GetHostName()));
80 SendResponse(true);
81 return true;
84 CloudPrintPrivateGetPrintersFunction::CloudPrintPrivateGetPrintersFunction() {
87 CloudPrintPrivateGetPrintersFunction::~CloudPrintPrivateGetPrintersFunction() {
90 void CloudPrintPrivateGetPrintersFunction::SendResults(
91 const std::vector<std::string>& printers) {
92 results_ = api::cloud_print_private::GetPrinters::Results::Create(printers);
93 SendResponse(true);
96 bool CloudPrintPrivateGetPrintersFunction::RunAsync() {
97 #if defined(ENABLE_PRINT_PREVIEW)
98 std::vector<std::string> result;
99 if (CloudPrintTestsDelegate::instance()) {
100 SendResults(CloudPrintTestsDelegate::instance()->GetPrinters());
101 } else {
102 CloudPrintProxyService* service =
103 CloudPrintProxyServiceFactory::GetForProfile(GetProfile());
104 if (!service)
105 return false;
106 service->GetPrinters(
107 base::Bind(&CloudPrintPrivateGetPrintersFunction::SendResults, this));
109 return true;
110 #else
111 return false;
112 #endif
116 CloudPrintPrivateGetClientIdFunction::CloudPrintPrivateGetClientIdFunction() {
119 CloudPrintPrivateGetClientIdFunction::~CloudPrintPrivateGetClientIdFunction() {
122 bool CloudPrintPrivateGetClientIdFunction::RunAsync() {
123 SetResult(new base::StringValue(
124 CloudPrintTestsDelegate::instance() ?
125 CloudPrintTestsDelegate::instance()->GetClientId() :
126 google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT)));
127 SendResponse(true);
128 return true;
131 } // namespace extensions