Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: chrome/browser/extensions/api/cloud_print_private/cloud_print_private_api.cc

Issue 12769009: Pass list of printers to chrome using file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/chrome_switches.h » ('j') | chrome/common/chrome_switches.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 8
9 #include "base/command_line.h"
10 #include "base/file_util.h"
11 #include "base/json/json_reader.h"
9 #include "base/threading/sequenced_worker_pool.h" 12 #include "base/threading/sequenced_worker_pool.h"
10 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" 13 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
11 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h" 14 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h"
15 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/extensions/api/cloud_print_private.h" 16 #include "chrome/common/extensions/api/cloud_print_private.h"
13 #include "google_apis/google_api_keys.h" 17 #include "google_apis/google_api_keys.h"
14 #include "net/base/net_util.h" 18 #include "net/base/net_util.h"
15 #include "printing/backend/print_backend.h" 19 #include "printing/backend/print_backend.h"
16 20
17 21
18 22
19 namespace extensions { 23 namespace extensions {
20 24
21 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance_ = NULL; 25 CloudPrintTestsDelegate* CloudPrintTestsDelegate::instance_ = NULL;
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 CloudPrintPrivateGetPrintersFunction::~CloudPrintPrivateGetPrintersFunction() { 88 CloudPrintPrivateGetPrintersFunction::~CloudPrintPrivateGetPrintersFunction() {
85 } 89 }
86 90
87 void CloudPrintPrivateGetPrintersFunction::ReturnResult( 91 void CloudPrintPrivateGetPrintersFunction::ReturnResult(
88 const base::ListValue* printers) { 92 const base::ListValue* printers) {
89 SetResult(printers->DeepCopy()); 93 SetResult(printers->DeepCopy());
90 SendResponse(true); 94 SendResponse(true);
91 } 95 }
92 96
93 void CloudPrintPrivateGetPrintersFunction::CollectPrinters() { 97 void CloudPrintPrivateGetPrintersFunction::CollectPrinters() {
94 scoped_ptr<base::ListValue> result(new base::ListValue()); 98 scoped_ptr<base::ListValue> result;
95 if (CloudPrintTestsDelegate::instance()) { 99
96 std::vector<std::string> printers = 100 CommandLine* command_line = CommandLine::ForCurrentProcess();
97 CloudPrintTestsDelegate::instance()->GetPrinters(); 101 base::FilePath list_path(
98 for (size_t i = 0; i < printers.size(); ++i) 102 command_line->GetSwitchValuePath(switches::kCloudPrintAddPrinters));
99 result->Append(Value::CreateStringValue(printers[i])); 103 if (!list_path.empty()) {
104 std::string printers_json;
105 file_util::ReadFileToString(list_path, &printers_json);
106 scoped_ptr<Value> value(base::JSONReader::Read(printers_json));
107 base::ListValue* list = NULL;
108 if (value)
109 value->GetAsList(&list);
110 if (list) {
111 result.reset(list);
112 value.release(); // Holds the same object as result.
113 }
100 } else { 114 } else {
101 printing::PrinterList printers; 115 result.reset(new base::ListValue());
102 scoped_refptr<printing::PrintBackend> backend( 116 if (CloudPrintTestsDelegate::instance()) {
103 printing::PrintBackend::CreateInstance(NULL)); 117 std::vector<std::string> printers =
104 if (backend) 118 CloudPrintTestsDelegate::instance()->GetPrinters();
105 backend->EnumeratePrinters(&printers); 119 for (size_t i = 0; i < printers.size(); ++i)
106 for (size_t i = 0; i < printers.size(); ++i) 120 result->Append(Value::CreateStringValue(printers[i]));
107 result->Append(Value::CreateStringValue(printers[i].printer_name)); 121 } else {
122 printing::PrinterList printers;
123 scoped_refptr<printing::PrintBackend> backend(
124 printing::PrintBackend::CreateInstance(NULL));
125 if (backend)
126 backend->EnumeratePrinters(&printers);
127 for (size_t i = 0; i < printers.size(); ++i)
128 result->Append(Value::CreateStringValue(printers[i].printer_name));
129 }
108 } 130 }
109 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, 131 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
110 base::Bind(&CloudPrintPrivateGetPrintersFunction::ReturnResult, this, 132 base::Bind(&CloudPrintPrivateGetPrintersFunction::ReturnResult, this,
111 base::Owned(result.release()))); 133 base::Owned(result.release())));
112 } 134 }
113 135
114 136
115 bool CloudPrintPrivateGetPrintersFunction::RunImpl() { 137 bool CloudPrintPrivateGetPrintersFunction::RunImpl() {
116 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, 138 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE,
117 base::Bind(&CloudPrintPrivateGetPrintersFunction::CollectPrinters, this)); 139 base::Bind(&CloudPrintPrivateGetPrintersFunction::CollectPrinters, this));
(...skipping 10 matching lines...) Expand all
128 bool CloudPrintPrivateGetClientIdFunction::RunImpl() { 150 bool CloudPrintPrivateGetClientIdFunction::RunImpl() {
129 SetResult(Value::CreateStringValue( 151 SetResult(Value::CreateStringValue(
130 CloudPrintTestsDelegate::instance() ? 152 CloudPrintTestsDelegate::instance() ?
131 CloudPrintTestsDelegate::instance()->GetClientId() : 153 CloudPrintTestsDelegate::instance()->GetClientId() :
132 google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT))); 154 google_apis::GetOAuth2ClientID(google_apis::CLIENT_CLOUD_PRINT)));
133 SendResponse(true); 155 SendResponse(true);
134 return true; 156 return true;
135 } 157 }
136 158
137 } // namespace extensions 159 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/common/chrome_switches.h » ('j') | chrome/common/chrome_switches.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698