Print Preview: Populate the printer list.

BUG=57892
TEST=none
Review URL: http://codereview.chromium.org/4150007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64664 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd
index ba7c986..a1c0553 100644
--- a/chrome/app/generated_resources.grd
+++ b/chrome/app/generated_resources.grd
@@ -5277,6 +5277,9 @@
       <message name="IDS_PRINT_PREVIEW_TITLE" desc="Title for print preview page ">
         Print Preview
       </message>
+      <message name="IDS_PRINT_PREVIEW_NO_PRINTER" desc="Select box entry when there is no printer.">
+        No printers found
+      </message>
 
       <!-- Load State -->
       <message name="IDS_LOAD_STATE_IDLE"></message>
diff --git a/chrome/browser/dom_ui/print_preview_handler.cc b/chrome/browser/dom_ui/print_preview_handler.cc
new file mode 100644
index 0000000..9248a8ad
--- /dev/null
+++ b/chrome/browser/dom_ui/print_preview_handler.cc
@@ -0,0 +1,33 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/dom_ui/print_preview_handler.h"
+
+#include "base/values.h"
+#include "printing/backend/print_backend.h"
+
+PrintPreviewHandler::PrintPreviewHandler()
+    : print_backend_(printing::PrintBackend::CreateInstance(NULL)) {
+}
+
+PrintPreviewHandler::~PrintPreviewHandler() {
+}
+
+void PrintPreviewHandler::RegisterMessages() {
+  dom_ui_->RegisterMessageCallback("getPrinters",
+      NewCallback(this, &PrintPreviewHandler::HandleGetPrinters));
+}
+
+void PrintPreviewHandler::HandleGetPrinters(const ListValue*) {
+  ListValue printers;
+
+  printing::PrinterList printer_list;
+  print_backend_->EnumeratePrinters(&printer_list);
+  for (printing::PrinterList::iterator index = printer_list.begin();
+       index != printer_list.end(); ++index) {
+    printers.Append(new StringValue(index->printer_name));
+  }
+
+  dom_ui_->CallJavascriptFunction(L"setPrinters", printers);
+}
diff --git a/chrome/browser/dom_ui/print_preview_handler.h b/chrome/browser/dom_ui/print_preview_handler.h
new file mode 100644
index 0000000..edcb8e60
--- /dev/null
+++ b/chrome/browser/dom_ui/print_preview_handler.h
@@ -0,0 +1,37 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_DOM_UI_PRINT_PREVIEW_HANDLER_H_
+#define CHROME_BROWSER_DOM_UI_PRINT_PREVIEW_HANDLER_H_
+#pragma once
+
+#include "base/scoped_ptr.h"
+#include "base/weak_ptr.h"
+#include "chrome/browser/dom_ui/dom_ui.h"
+
+namespace printing {
+class PrintBackend;
+}  // namespace printing
+
+// The handler for Javascript messages related to the "print preview" dialog.
+class PrintPreviewHandler : public DOMMessageHandler,
+                            public base::SupportsWeakPtr<PrintPreviewHandler> {
+ public:
+  PrintPreviewHandler();
+  virtual ~PrintPreviewHandler();
+
+  // DOMMessageHandler implementation.
+  virtual void RegisterMessages();
+
+ private:
+  // Get the list of printers and send it to the DOM UI. |args| is unused.
+  void HandleGetPrinters(const ListValue* args);
+
+  // Pointer to current print system.
+  scoped_refptr<printing::PrintBackend> print_backend_;
+
+  DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler);
+};
+
+#endif  // CHROME_BROWSER_DOM_UI_PRINT_PREVIEW_HANDLER_H_
diff --git a/chrome/browser/dom_ui/print_preview_ui.cc b/chrome/browser/dom_ui/print_preview_ui.cc
index 7c34dfd..a1535187 100644
--- a/chrome/browser/dom_ui/print_preview_ui.cc
+++ b/chrome/browser/dom_ui/print_preview_ui.cc
@@ -13,7 +13,9 @@
 #include "base/string_piece.h"
 #include "base/values.h"
 #include "chrome/browser/browser_thread.h"
+#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
 #include "chrome/browser/dom_ui/dom_ui_theme_source.h"
+#include "chrome/browser/dom_ui/print_preview_handler.h"
 #include "chrome/browser/tab_contents/tab_contents.h"
 #include "chrome/common/jstemplate_builder.h"
 #include "chrome/common/url_constants.h"
@@ -27,6 +29,8 @@
 void SetLocalizedStrings(DictionaryValue* localized_strings) {
   localized_strings->SetString(std::string("title"),
       l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE));
+  localized_strings->SetString(std::string("no-printer"),
+      l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PRINTER));
 }
 
 }  // namespace
@@ -37,6 +41,22 @@
 //
 ////////////////////////////////////////////////////////////////////////////////
 
+class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource {
+ public:
+  PrintPreviewUIHTMLSource();
+  virtual ~PrintPreviewUIHTMLSource();
+
+  // Called when the network layer has requested a resource underneath
+  // the path we registered.
+  virtual void StartDataRequest(const std::string& path,
+                                bool is_off_the_record,
+                                int request_id);
+  virtual std::string GetMimeType(const std::string&) const;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource);
+};
+
 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource()
     : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) {
 }
@@ -74,6 +94,10 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 PrintPreviewUI::PrintPreviewUI(TabContents* contents) : DOMUI(contents) {
+  // PrintPreviewUI owns |handler|.
+  PrintPreviewHandler* handler = new PrintPreviewHandler();
+  AddMessageHandler(handler->Attach(this));
+
   // Set up the chrome://print/ source.
   BrowserThread::PostTask(
       BrowserThread::IO, FROM_HERE,
diff --git a/chrome/browser/dom_ui/print_preview_ui.h b/chrome/browser/dom_ui/print_preview_ui.h
index 58844637f..57458562 100644
--- a/chrome/browser/dom_ui/print_preview_ui.h
+++ b/chrome/browser/dom_ui/print_preview_ui.h
@@ -8,27 +8,8 @@
 
 #include <string>
 
-#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
 #include "chrome/browser/dom_ui/dom_ui.h"
 
-struct UserMetricsAction;
-
-class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource {
- public:
-  explicit PrintPreviewUIHTMLSource();
-  virtual ~PrintPreviewUIHTMLSource();
-
-  // Called when the network layer has requested a resource underneath
-  // the path we registered.
-  virtual void StartDataRequest(const std::string& path,
-                                bool is_off_the_record,
-                                int request_id);
-  virtual std::string GetMimeType(const std::string&) const;
-
- private:
-  DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource);
-};
-
 class PrintPreviewUI : public DOMUI {
  public:
   explicit PrintPreviewUI(TabContents* contents);
diff --git a/chrome/browser/resources/print_preview.html b/chrome/browser/resources/print_preview.html
index 9ce2efb..d1eaeb24 100644
--- a/chrome/browser/resources/print_preview.html
+++ b/chrome/browser/resources/print_preview.html
@@ -9,18 +9,16 @@
 
 <script src="chrome://resources/js/local_strings.js"></script>
 <script src="chrome://resources/js/util.js"></script>
-
+<script src="print_preview.js"></script>
 </head>
 <body i18n-values=".style.fontFamily:fontfamily;.style.fontSize:fontsize">
 <div id="navbar-container">
   <div id="destination-container">
     <h1>Destination</h1>
-    <div id="destination-list">
-      LIST GOES HERE
-    </div>
-    <div id="print-buttons">
-      <button id="printButton">Print</button>
-      <button id="cancelButton">Cancel</button>
+    <select id="printer-list"></select>
+    <div id="buttons">
+      <button id="print-button">Print</button>
+      <button id="cancel-button">Cancel</button>
     </div>
   </div>
   <div id="options-container">
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js
new file mode 100644
index 0000000..4fcf953
--- /dev/null
+++ b/chrome/browser/resources/print_preview.js
@@ -0,0 +1,34 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+var localStrings = new LocalStrings();
+
+/**
+ * Window onload handler, sets up the page.
+ */
+function load() {
+  chrome.send('getPrinters');
+};
+
+/**
+ * Fill the printer list drop down.
+ */
+function setPrinters(printers) {
+  if (printers.length > 0) {
+    for (var i = 0; i < printers.length; ++i) {
+      var option = document.createElement('option');
+      option.textContent = printers[i];
+      $('printer-list').add(option);
+    }
+  } else {
+    var option = document.createElement('option');
+    option.textContent = localStrings.getString('no-printer');
+    $('printer-list').add(option);
+    $('printer-list').disabled = true;
+    $('print-button').disabled = true;
+  }
+}
+
+window.addEventListener('DOMContentLoaded', load);
+
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index f28e655..e749f9e 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -1335,6 +1335,8 @@
         'browser/dom_ui/ntp_resource_cache.h',
         'browser/dom_ui/plugins_ui.cc',
         'browser/dom_ui/plugins_ui.h',
+        'browser/dom_ui/print_preview_handler.cc',
+        'browser/dom_ui/print_preview_handler.h',
         'browser/dom_ui/print_preview_ui.cc',
         'browser/dom_ui/print_preview_ui.h',
         'browser/dom_ui/remoting_ui.cc',