Index: chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.cc |
diff --git a/chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.cc b/chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.cc |
index e5950de68e908ec67b3d611345ba67483f52e021..b404c06722bf0da6fc41682176f2b0a6dec4ad3a 100644 |
--- a/chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.cc |
+++ b/chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.cc |
@@ -5,49 +5,141 @@ |
#include "chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.h" |
#include <string> |
+ |
+#include "base/json/json_file_value_serializer.h" |
+#include "base/path_service.h" |
+#include "base/threading/sequenced_worker_pool.h" |
#include "base/values.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" |
#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h" |
#include "chrome/browser/profiles/profile.h" |
+#include "chrome/common/chrome_constants.h" |
+#include "chrome/common/chrome_paths.h" |
+#include "chrome/common/pref_names.h" |
+#include "net/base/net_util.h" |
+#include "printing/backend/print_backend.h" |
+ |
+namespace extensions { |
namespace { |
-bool test_mode = false; |
+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.
|
+ |
+void EnumeratePrinters(printing::PrinterList* printers) { |
+ FilePath user_data_dir; |
+ PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
+ FilePath pref_path = user_data_dir.Append(chrome::kServiceStateFileName); |
+ int error_code = 0; |
+ std::string error_msg; |
+ JSONFileValueSerializer serializer(pref_path); |
+ scoped_ptr<base::Value> service_state(serializer.Deserialize(&error_code, |
+ &error_msg)); |
+ const base::DictionaryValue* dictionary = NULL; |
+ const base::DictionaryValue* print_system_settings = NULL; |
+ 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.
|
+ dictionary->GetDictionary(prefs::kCloudPrintPrintSystemSettings, |
+ &print_system_settings); |
+ } |
+ |
+ 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.
|
+ printing::PrintBackend::CreateInstance(print_system_settings)); |
+ backend->EnumeratePrinters(printers); |
+} |
} // namespace |
-namespace extensions { |
+CloudPrintTestsDelegate::CloudPrintTestsDelegate() { |
+ test_delegate = this; |
+} |
-CloudPrintSetCredentialsFunction::CloudPrintSetCredentialsFunction() { |
+CloudPrintTestsDelegate::~CloudPrintTestsDelegate() { |
+ test_delegate = NULL; |
} |
-CloudPrintSetCredentialsFunction::~CloudPrintSetCredentialsFunction() { |
+CloudPrintSetupConnectorFunction::CloudPrintSetupConnectorFunction() { |
} |
-bool CloudPrintSetCredentialsFunction::RunImpl() { |
+CloudPrintSetupConnectorFunction::~CloudPrintSetupConnectorFunction() { |
+} |
+ |
+ |
+bool CloudPrintSetupConnectorFunction::RunImpl() { |
std::string user_email; |
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &user_email)); |
std::string robot_email; |
EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &robot_email)); |
std::string credentials; |
EXTENSION_FUNCTION_VALIDATE(args_->GetString(2, &credentials)); |
- if (test_mode) { |
- std::string test_response = user_email; |
- test_response.append(robot_email); |
- test_response.append(credentials); |
- SetResult(Value::CreateStringValue(test_response)); |
+ bool connect_new_printers = false; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(3, &connect_new_printers)); |
+ const ListValue* printer_blacklist = NULL; |
+ EXTENSION_FUNCTION_VALIDATE(args_->GetList(4, &printer_blacklist)); |
+ if (test_delegate) { |
+ test_delegate->SetupConnector(user_email, robot_email, credentials, |
+ connect_new_printers, printer_blacklist); |
} else { |
+ std::vector<std::string> printers; |
+ if (printer_blacklist) { |
+ printers.resize(printer_blacklist->GetSize()); |
+ for (size_t i = 0; i < printer_blacklist->GetSize(); ++i) |
+ printer_blacklist->GetString(i, &printers[i]); |
+ } |
CloudPrintProxyServiceFactory::GetForProfile(profile_)-> |
- EnableForUserWithRobot(credentials, robot_email, user_email); |
+ EnableForUserWithRobot(credentials, robot_email, user_email, |
+ connect_new_printers, printers); |
} |
SendResponse(true); |
return true; |
} |
-// static |
-void CloudPrintSetCredentialsFunction::SetTestMode(bool test_mode_enabled) { |
- test_mode = test_mode_enabled; |
+CloudPrintGetHostNameFunction::CloudPrintGetHostNameFunction() { |
+} |
+ |
+CloudPrintGetHostNameFunction::~CloudPrintGetHostNameFunction() { |
+} |
+ |
+bool CloudPrintGetHostNameFunction::RunImpl() { |
+ SetResult(Value::CreateStringValue( |
+ test_delegate ? test_delegate->GetHostName() : net::GetHostName())); |
+ SendResponse(true); |
+ return true; |
+} |
+ |
+CloudPrintGetPrintersFunction::CloudPrintGetPrintersFunction() { |
+} |
+ |
+CloudPrintGetPrintersFunction::~CloudPrintGetPrintersFunction() { |
+} |
+ |
+void CloudPrintGetPrintersFunction::ReturnResult( |
+ const base::ListValue* printers) { |
+ SetResult(printers->DeepCopy()); |
+ SendResponse(true); |
+} |
+ |
+void CloudPrintGetPrintersFunction::CollectPrinters() { |
+ scoped_ptr<base::ListValue> result(new base::ListValue()); |
+ if (test_delegate) { |
+ std::vector<std::string> printers = test_delegate->GetPrinters(); |
+ for (size_t i = 0; i < printers.size(); ++i) |
+ result->Append(Value::CreateStringValue(printers[i])); |
+ } else { |
+ printing::PrinterList printers; |
+ EnumeratePrinters(&printers); |
+ for (size_t i = 0; i < printers.size(); ++i) |
+ result->Append(Value::CreateStringValue(printers[i].printer_name)); |
+ } |
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&CloudPrintGetPrintersFunction::ReturnResult, this, |
+ base::Owned(result.release()))); |
+} |
+ |
+ |
+bool CloudPrintGetPrintersFunction::RunImpl() { |
+ content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, |
+ base::Bind(&CloudPrintGetPrintersFunction::CollectPrinters, this)); |
+ return true; |
} |
} // namespace extensions |