scanning_handler.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_WEBUI_SCANNING_SCANNING_HANDLER_H_
  5. #define ASH_WEBUI_SCANNING_SCANNING_HANDLER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/files/file_path.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/values.h"
  13. #include "content/public/browser/web_ui_message_handler.h"
  14. #include "ui/shell_dialogs/select_file_dialog.h"
  15. namespace base {
  16. class SequencedTaskRunner;
  17. } // namespace base
  18. namespace content {
  19. class WebUI;
  20. } // namespace content
  21. namespace ash {
  22. class ScanningAppDelegate;
  23. // ChromeOS Scanning app UI handler.
  24. class ScanningHandler : public content::WebUIMessageHandler,
  25. public ui::SelectFileDialog::Listener {
  26. public:
  27. explicit ScanningHandler(
  28. std::unique_ptr<ScanningAppDelegate> scanning_app_delegate);
  29. ~ScanningHandler() override;
  30. ScanningHandler(const ScanningHandler&) = delete;
  31. ScanningHandler& operator=(const ScanningHandler&) = delete;
  32. // WebUIMessageHandler:
  33. void RegisterMessages() override;
  34. // SelectFileDialog::Listener:
  35. void FileSelected(const base::FilePath& path,
  36. int index,
  37. void* params) override;
  38. void FileSelectionCanceled(void* params) override;
  39. // Uses the full filepath and the base directory (lowest level directory in
  40. // the filepath, used to display in the UI) to create a Value object to return
  41. // to the Scanning UI.
  42. base::Value::Dict CreateSelectedPathValue(const base::FilePath& path);
  43. // Adds to map of string IDs for pluralization.
  44. void AddStringToPluralMap(const std::string& name, int id);
  45. void SetWebUIForTest(content::WebUI* web_ui);
  46. private:
  47. // Initializes Javascript.
  48. void HandleInitialize(const base::Value::List& args);
  49. // Opens the select dialog for the user to choose the directory to save
  50. // completed scans.
  51. void HandleRequestScanToLocation(const base::Value::List& args);
  52. // Opens the Media app with the completed scan files.
  53. void HandleOpenFilesInMediaApp(const base::Value::List& args);
  54. // Opens the Files app to the show the saved scan file.
  55. void HandleShowFileInLocation(const base::Value::List& args);
  56. // Callback for HandleShowFileInLocation().
  57. void OnShowFileInLocation(const std::string& callback, bool files_app_opened);
  58. // Returns a localized, pluralized string.
  59. void HandleGetPluralString(const base::Value::List& args);
  60. // Gets the MyFiles path for the current user.
  61. void HandleGetMyFilesPath(const base::Value::List& args);
  62. // Saves scan settings to Pref service.
  63. void HandleSaveScanSettings(const base::Value::List& args);
  64. // Fetches scan settings from Pref service.
  65. void HandleGetScanSettings(const base::Value::List& args);
  66. // Validates that a file path exists on the local filesystem and returns its
  67. // display name. If the file path doesn't exist, return an empty file path.
  68. void HandleEnsureValidFilePath(const base::Value::List& args);
  69. // Callback for HandleEnsureValidFilePath().
  70. void OnPathExists(const base::FilePath& file_path,
  71. const std::string& callback,
  72. bool file_path_exists);
  73. std::string scan_location_callback_id_;
  74. scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
  75. // Provides browser functionality from //chrome to the Scan app UI.
  76. std::unique_ptr<ScanningAppDelegate> scanning_app_delegate_;
  77. std::map<std::string, int> string_id_map_;
  78. // Task runner for the I/O function base::FilePath().
  79. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  80. base::WeakPtrFactory<ScanningHandler> weak_ptr_factory_{this};
  81. };
  82. } // namespace ash
  83. #endif // ASH_WEBUI_SCANNING_SCANNING_HANDLER_H_