OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/cloud_print_private/cloud_print_private_
api.h" | 5 #include "chrome/browser/extensions/api/cloud_print_private/cloud_print_private_
api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include "base/values.h" | 8 |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
10 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.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" | 11 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.
h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/common/extensions/api/cloud_print_private.h" |
| 13 #include "net/base/net_util.h" |
| 14 #include "printing/backend/print_backend.h" |
13 | 15 |
14 namespace { | |
15 | 16 |
16 bool test_mode = false; | |
17 | |
18 } // namespace | |
19 | 17 |
20 namespace extensions { | 18 namespace extensions { |
21 | 19 |
22 CloudPrintSetCredentialsFunction::CloudPrintSetCredentialsFunction() { | 20 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance_ = NULL; |
| 21 |
| 22 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance() { |
| 23 return instance_; |
23 } | 24 } |
24 | 25 |
25 CloudPrintSetCredentialsFunction::~CloudPrintSetCredentialsFunction() { | 26 CloudPrintTestsDelegate::CloudPrintTestsDelegate() { |
| 27 instance_ = this; |
26 } | 28 } |
27 | 29 |
28 bool CloudPrintSetCredentialsFunction::RunImpl() { | 30 CloudPrintTestsDelegate::~CloudPrintTestsDelegate() { |
29 std::string user_email; | 31 instance_ = NULL; |
30 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &user_email)); | 32 } |
31 std::string robot_email; | 33 |
32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &robot_email)); | 34 CloudPrintSetupConnectorFunction::CloudPrintSetupConnectorFunction() { |
33 std::string credentials; | 35 } |
34 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &credentials)); | 36 |
35 if (test_mode) { | 37 CloudPrintSetupConnectorFunction::~CloudPrintSetupConnectorFunction() { |
36 std::string test_response = user_email; | 38 } |
37 test_response.append(robot_email); | 39 |
38 test_response.append(credentials); | 40 |
39 SetResult(Value::CreateStringValue(test_response)); | 41 bool CloudPrintSetupConnectorFunction::RunImpl() { |
| 42 using extensions::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->connect_new_printers, |
| 50 params->printer_blacklist); |
40 } else { | 51 } else { |
41 CloudPrintProxyServiceFactory::GetForProfile(profile_)-> | 52 CloudPrintProxyServiceFactory::GetForProfile(profile_)-> |
42 EnableForUserWithRobot(credentials, robot_email, user_email); | 53 EnableForUserWithRobot(params->credentials, |
| 54 params->robot_email, |
| 55 params->user_email, |
| 56 params->connect_new_printers, |
| 57 params->printer_blacklist); |
43 } | 58 } |
44 SendResponse(true); | 59 SendResponse(true); |
45 return true; | 60 return true; |
46 } | 61 } |
47 | 62 |
48 // static | 63 CloudPrintGetHostNameFunction::CloudPrintGetHostNameFunction() { |
49 void CloudPrintSetCredentialsFunction::SetTestMode(bool test_mode_enabled) { | 64 } |
50 test_mode = test_mode_enabled; | 65 |
| 66 CloudPrintGetHostNameFunction::~CloudPrintGetHostNameFunction() { |
| 67 } |
| 68 |
| 69 bool CloudPrintGetHostNameFunction::RunImpl() { |
| 70 SetResult(Value::CreateStringValue( |
| 71 CloudPrintTestsDelegate::instance() ? |
| 72 CloudPrintTestsDelegate::instance()->GetHostName() : |
| 73 net::GetHostName())); |
| 74 SendResponse(true); |
| 75 return true; |
| 76 } |
| 77 |
| 78 CloudPrintGetPrintersFunction::CloudPrintGetPrintersFunction() { |
| 79 } |
| 80 |
| 81 CloudPrintGetPrintersFunction::~CloudPrintGetPrintersFunction() { |
| 82 } |
| 83 |
| 84 void CloudPrintGetPrintersFunction::ReturnResult( |
| 85 const base::ListValue* printers) { |
| 86 SetResult(printers->DeepCopy()); |
| 87 SendResponse(true); |
| 88 } |
| 89 |
| 90 void CloudPrintGetPrintersFunction::CollectPrinters() { |
| 91 scoped_ptr<base::ListValue> result(new base::ListValue()); |
| 92 if (CloudPrintTestsDelegate::instance()) { |
| 93 std::vector<std::string> printers = |
| 94 CloudPrintTestsDelegate::instance()->GetPrinters(); |
| 95 for (size_t i = 0; i < printers.size(); ++i) |
| 96 result->Append(Value::CreateStringValue(printers[i])); |
| 97 } else { |
| 98 printing::PrinterList printers; |
| 99 scoped_refptr<printing::PrintBackend> backend( |
| 100 printing::PrintBackend::CreateInstance(NULL)); |
| 101 if (backend) |
| 102 backend->EnumeratePrinters(&printers); |
| 103 for (size_t i = 0; i < printers.size(); ++i) |
| 104 result->Append(Value::CreateStringValue(printers[i].printer_name)); |
| 105 } |
| 106 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 107 base::Bind(&CloudPrintGetPrintersFunction::ReturnResult, this, |
| 108 base::Owned(result.release()))); |
| 109 } |
| 110 |
| 111 |
| 112 bool CloudPrintGetPrintersFunction::RunImpl() { |
| 113 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, |
| 114 base::Bind(&CloudPrintGetPrintersFunction::CollectPrinters, this)); |
| 115 return true; |
51 } | 116 } |
52 | 117 |
53 } // namespace extensions | 118 } // namespace extensions |
OLD | NEW |