/cef3/tests/cefclient/dialog_test.cpp

http://chromiumembedded.googlecode.com/ · C++ · 106 lines · 76 code · 20 blank · 10 comment · 12 complexity · d4eb841605a3b9ce3038532580f9e015 MD5 · raw file

  1. // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
  2. // reserved. Use of this source code is governed by a BSD-style license that
  3. // can be found in the LICENSE file.
  4. #include "cefclient/dialog_test.h"
  5. #include "include/cef_browser.h"
  6. #include <string>
  7. namespace dialog_test {
  8. namespace {
  9. const char* kTestUrl = "http://tests/dialogs";
  10. const char* kFileOpenMessageName = "DialogTest.FileOpen";
  11. const char* kFileOpenMultipleMessageName = "DialogTest.FileOpenMultiple";
  12. const char* kFileSaveMessageName = "DialogTest.FileSave";
  13. class RunFileDialogCallback : public CefRunFileDialogCallback {
  14. public:
  15. explicit RunFileDialogCallback(const std::string& message_name)
  16. : message_name_(message_name) {
  17. }
  18. virtual void OnFileDialogDismissed(
  19. CefRefPtr<CefBrowserHost> browser_host,
  20. const std::vector<CefString>& file_paths) OVERRIDE {
  21. // Send a message back to the render process with the same name and the list
  22. // of file paths.
  23. CefRefPtr<CefProcessMessage> message =
  24. CefProcessMessage::Create(message_name_);
  25. CefRefPtr<CefListValue> args = message->GetArgumentList();
  26. CefRefPtr<CefListValue> val = CefListValue::Create();
  27. for (size_t i = 0; i < file_paths.size(); ++i)
  28. val->SetString(i, file_paths[i]);
  29. args->SetList(0, val);
  30. // This will result in a call to the callback registered via JavaScript in
  31. // dialogs.html.
  32. browser_host->GetBrowser()->SendProcessMessage(PID_RENDERER, message);
  33. }
  34. private:
  35. std::string message_name_;
  36. IMPLEMENT_REFCOUNTING(RunFileDialogCallback);
  37. };
  38. // Handle messages in the browser process.
  39. class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
  40. public:
  41. ProcessMessageDelegate() {
  42. }
  43. // From ClientHandler::ProcessMessageDelegate.
  44. virtual bool OnProcessMessageReceived(
  45. CefRefPtr<ClientHandler> handler,
  46. CefRefPtr<CefBrowser> browser,
  47. CefProcessId source_process,
  48. CefRefPtr<CefProcessMessage> message) OVERRIDE {
  49. std::string url = browser->GetMainFrame()->GetURL();
  50. if (url.find(kTestUrl) != 0)
  51. return false;
  52. // Sample file type filter.
  53. std::vector<CefString> file_types;
  54. file_types.push_back("text/*");
  55. file_types.push_back(".log");
  56. file_types.push_back(".patch");
  57. std::string message_name = message->GetName();
  58. if (message_name == kFileOpenMessageName) {
  59. browser->GetHost()->RunFileDialog(FILE_DIALOG_OPEN, "My Open Dialog",
  60. "test.txt", file_types, new RunFileDialogCallback(message_name));
  61. return true;
  62. } else if (message_name == kFileOpenMultipleMessageName) {
  63. browser->GetHost()->RunFileDialog(FILE_DIALOG_OPEN_MULTIPLE,
  64. "My Open Multiple Dialog", CefString(), file_types,
  65. new RunFileDialogCallback(message_name));
  66. return true;
  67. } else if (message_name == kFileSaveMessageName) {
  68. browser->GetHost()->RunFileDialog(FILE_DIALOG_SAVE, "My Save Dialog",
  69. "test.txt", file_types, new RunFileDialogCallback(message_name));
  70. return true;
  71. } else {
  72. ASSERT(false); // Not reached.
  73. }
  74. return false;
  75. }
  76. IMPLEMENT_REFCOUNTING(ProcessMessageDelegate);
  77. };
  78. } // namespace
  79. void CreateProcessMessageDelegates(
  80. ClientHandler::ProcessMessageDelegateSet& delegates) {
  81. delegates.insert(new ProcessMessageDelegate);
  82. }
  83. void RunTest(CefRefPtr<CefBrowser> browser) {
  84. browser->GetMainFrame()->LoadURL(kTestUrl);
  85. }
  86. } // namespace dialog_test