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 | |
9 #include "base/json/json_file_value_serializer.h" | |
10 #include "base/path_service.h" | |
11 #include "base/threading/sequenced_worker_pool.h" | |
8 #include "base/values.h" | 12 #include "base/values.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" | 14 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" |
11 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h" | 15 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h" |
12 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/chrome_constants.h" | |
18 #include "chrome/common/chrome_paths.h" | |
19 #include "chrome/common/pref_names.h" | |
20 #include "net/base/net_util.h" | |
21 #include "printing/backend/print_backend.h" | |
22 | |
23 namespace extensions { | |
13 | 24 |
14 namespace { | 25 namespace { |
15 | 26 |
16 bool test_mode = false; | 27 CloudPrintTestsDelegate* test_delegate = NULL; |
gene
2012/10/01 21:37:37
Make it a static member of CloudPrintTestsDelegate
Vitaly Buka (NO REVIEWS)
2012/10/01 22:20:13
Done.
| |
28 | |
29 void EnumeratePrinters(printing::PrinterList* printers) { | |
30 FilePath user_data_dir; | |
31 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
32 FilePath pref_path = user_data_dir.Append(chrome::kServiceStateFileName); | |
33 int error_code = 0; | |
34 std::string error_msg; | |
35 JSONFileValueSerializer serializer(pref_path); | |
36 scoped_ptr<base::Value> service_state(serializer.Deserialize(&error_code, | |
37 &error_msg)); | |
38 const base::DictionaryValue* dictionary = NULL; | |
39 const base::DictionaryValue* print_system_settings = NULL; | |
40 if (service_state->GetAsDictionary(&dictionary)) { | |
gene
2012/10/01 21:37:37
if 'Service State' file is missing or corrupted, s
Vitaly Buka (NO REVIEWS)
2012/10/01 22:20:13
Done.
| |
41 dictionary->GetDictionary(prefs::kCloudPrintPrintSystemSettings, | |
42 &print_system_settings); | |
43 } | |
44 | |
45 scoped_refptr<printing::PrintBackend> backend( | |
gene
2012/10/01 21:37:37
You need to use PrintSystem here (especially for C
Vitaly Buka (NO REVIEWS)
2012/10/01 22:20:13
Done.
| |
46 printing::PrintBackend::CreateInstance(print_system_settings)); | |
47 backend->EnumeratePrinters(printers); | |
48 } | |
17 | 49 |
18 } // namespace | 50 } // namespace |
19 | 51 |
20 namespace extensions { | 52 CloudPrintTestsDelegate::CloudPrintTestsDelegate() { |
21 | 53 test_delegate = this; |
22 CloudPrintSetCredentialsFunction::CloudPrintSetCredentialsFunction() { | |
23 } | 54 } |
24 | 55 |
25 CloudPrintSetCredentialsFunction::~CloudPrintSetCredentialsFunction() { | 56 CloudPrintTestsDelegate::~CloudPrintTestsDelegate() { |
57 test_delegate = NULL; | |
26 } | 58 } |
27 | 59 |
28 bool CloudPrintSetCredentialsFunction::RunImpl() { | 60 CloudPrintSetupConnectorFunction::CloudPrintSetupConnectorFunction() { |
61 } | |
62 | |
63 CloudPrintSetupConnectorFunction::~CloudPrintSetupConnectorFunction() { | |
64 } | |
65 | |
66 | |
67 bool CloudPrintSetupConnectorFunction::RunImpl() { | |
29 std::string user_email; | 68 std::string user_email; |
30 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &user_email)); | 69 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &user_email)); |
31 std::string robot_email; | 70 std::string robot_email; |
32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &robot_email)); | 71 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &robot_email)); |
33 std::string credentials; | 72 std::string credentials; |
34 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &credentials)); | 73 EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &credentials)); |
35 if (test_mode) { | 74 bool connect_new_printers = false; |
36 std::string test_response = user_email; | 75 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(3, &connect_new_printers)); |
37 test_response.append(robot_email); | 76 const ListValue* printer_blacklist = NULL; |
38 test_response.append(credentials); | 77 EXTENSION_FUNCTION_VALIDATE(args_->GetList(4, &printer_blacklist)); |
39 SetResult(Value::CreateStringValue(test_response)); | 78 if (test_delegate) { |
79 test_delegate->SetupConnector(user_email, robot_email, credentials, | |
80 connect_new_printers, printer_blacklist); | |
40 } else { | 81 } else { |
82 std::vector<std::string> printers; | |
83 if (printer_blacklist) { | |
84 printers.resize(printer_blacklist->GetSize()); | |
85 for (size_t i = 0; i < printer_blacklist->GetSize(); ++i) | |
86 printer_blacklist->GetString(i, &printers[i]); | |
87 } | |
41 CloudPrintProxyServiceFactory::GetForProfile(profile_)-> | 88 CloudPrintProxyServiceFactory::GetForProfile(profile_)-> |
42 EnableForUserWithRobot(credentials, robot_email, user_email); | 89 EnableForUserWithRobot(credentials, robot_email, user_email, |
90 connect_new_printers, printers); | |
43 } | 91 } |
44 SendResponse(true); | 92 SendResponse(true); |
45 return true; | 93 return true; |
46 } | 94 } |
47 | 95 |
48 // static | 96 CloudPrintGetHostNameFunction::CloudPrintGetHostNameFunction() { |
49 void CloudPrintSetCredentialsFunction::SetTestMode(bool test_mode_enabled) { | 97 } |
50 test_mode = test_mode_enabled; | 98 |
99 CloudPrintGetHostNameFunction::~CloudPrintGetHostNameFunction() { | |
100 } | |
101 | |
102 bool CloudPrintGetHostNameFunction::RunImpl() { | |
103 SetResult(Value::CreateStringValue( | |
104 test_delegate ? test_delegate->GetHostName() : net::GetHostName())); | |
105 SendResponse(true); | |
106 return true; | |
107 } | |
108 | |
109 CloudPrintGetPrintersFunction::CloudPrintGetPrintersFunction() { | |
110 } | |
111 | |
112 CloudPrintGetPrintersFunction::~CloudPrintGetPrintersFunction() { | |
113 } | |
114 | |
115 void CloudPrintGetPrintersFunction::ReturnResult( | |
116 const base::ListValue* printers) { | |
117 SetResult(printers->DeepCopy()); | |
118 SendResponse(true); | |
119 } | |
120 | |
121 void CloudPrintGetPrintersFunction::CollectPrinters() { | |
122 scoped_ptr<base::ListValue> result(new base::ListValue()); | |
123 if (test_delegate) { | |
124 std::vector<std::string> printers = test_delegate->GetPrinters(); | |
125 for (size_t i = 0; i < printers.size(); ++i) | |
126 result->Append(Value::CreateStringValue(printers[i])); | |
127 } else { | |
128 printing::PrinterList printers; | |
129 EnumeratePrinters(&printers); | |
130 for (size_t i = 0; i < printers.size(); ++i) | |
131 result->Append(Value::CreateStringValue(printers[i].printer_name)); | |
132 } | |
133 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
134 base::Bind(&CloudPrintGetPrintersFunction::ReturnResult, this, | |
135 base::Owned(result.release()))); | |
136 } | |
137 | |
138 | |
139 bool CloudPrintGetPrintersFunction::RunImpl() { | |
140 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, | |
141 base::Bind(&CloudPrintGetPrintersFunction::CollectPrinters, this)); | |
142 return true; | |
51 } | 143 } |
52 | 144 |
53 } // namespace extensions | 145 } // namespace extensions |
OLD | NEW |