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

Side by Side Diff: chrome/test/base/ui_test_utils.cc

Issue 8633004: Stop WebSocker server processes certainly (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: again Created 9 years, 1 month 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 | « chrome/test/base/ui_test_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/test/base/ui_test_utils.h" 5 #include "chrome/test/base/ui_test_utils.h"
6 6
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
10
7 #include <vector> 11 #include <vector>
8 12
9 #include "base/bind.h" 13 #include "base/bind.h"
10 #include "base/bind_helpers.h" 14 #include "base/bind_helpers.h"
11 #include "base/callback.h" 15 #include "base/callback.h"
12 #include "base/command_line.h" 16 #include "base/command_line.h"
13 #include "base/file_path.h" 17 #include "base/file_path.h"
14 #include "base/json/json_reader.h" 18 #include "base/json/json_reader.h"
15 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop.h" 20 #include "base/message_loop.h"
17 #include "base/path_service.h" 21 #include "base/path_service.h"
18 #include "base/process_util.h" 22 #include "base/process_util.h"
23 #include "base/string_number_conversions.h"
19 #include "base/utf_string_conversions.h" 24 #include "base/utf_string_conversions.h"
20 #include "base/values.h" 25 #include "base/values.h"
21 #include "chrome/browser/automation/ui_controls.h" 26 #include "chrome/browser/automation/ui_controls.h"
22 #include "chrome/browser/bookmarks/bookmark_model.h" 27 #include "chrome/browser/bookmarks/bookmark_model.h"
23 #include "chrome/browser/browser_process.h" 28 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/dom_operation_notification_details.h" 29 #include "chrome/browser/dom_operation_notification_details.h"
25 #include "chrome/browser/profiles/profile.h" 30 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/search_engines/template_url_service.h" 31 #include "chrome/browser/search_engines/template_url_service.h"
27 #include "chrome/browser/search_engines/template_url_service_test_util.h" 32 #include "chrome/browser/search_engines/template_url_service_test_util.h"
28 #include "chrome/browser/tab_contents/thumbnail_generator.h" 33 #include "chrome/browser/tab_contents/thumbnail_generator.h"
(...skipping 26 matching lines...) Expand all
55 #include "ui/gfx/size.h" 60 #include "ui/gfx/size.h"
56 61
57 #if defined(TOOLKIT_VIEWS) 62 #if defined(TOOLKIT_VIEWS)
58 #include "ui/views/focus/accelerator_handler.h" 63 #include "ui/views/focus/accelerator_handler.h"
59 #endif 64 #endif
60 65
61 #if defined(USE_AURA) 66 #if defined(USE_AURA)
62 #include "ui/aura/desktop.h" 67 #include "ui/aura/desktop.h"
63 #endif 68 #endif
64 69
70 static const int kDefaultWsPort = 8880;
71
65 namespace ui_test_utils { 72 namespace ui_test_utils {
66 73
67 namespace { 74 namespace {
68 75
69 class DOMOperationObserver : public content::NotificationObserver { 76 class DOMOperationObserver : public content::NotificationObserver {
70 public: 77 public:
71 explicit DOMOperationObserver(RenderViewHost* render_view_host) 78 explicit DOMOperationObserver(RenderViewHost* render_view_host)
72 : did_respond_(false) { 79 : did_respond_(false) {
73 registrar_.Add(this, chrome::NOTIFICATION_DOM_OPERATION_RESPONSE, 80 registrar_.Add(this, chrome::NOTIFICATION_DOM_OPERATION_RESPONSE,
74 content::Source<RenderViewHost>(render_view_host)); 81 content::Source<RenderViewHost>(render_view_host));
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 std::string newpath(oldpath); 727 std::string newpath(oldpath);
721 newpath.append(":"); 728 newpath.append(":");
722 newpath.append(dir.value()); 729 newpath.append(dir.value());
723 setenv(kPythonPath, newpath.c_str(), 1); 730 setenv(kPythonPath, newpath.c_str(), 1);
724 } 731 }
725 #endif 732 #endif
726 } 733 }
727 734
728 } // anonymous namespace 735 } // anonymous namespace
729 736
730 TestWebSocketServer::TestWebSocketServer() : started_(false) { 737 #if defined(OS_POSIX)
738 class OrphanedWebSocketServerFilter : public base::ProcessFilter {
Paweł Hajdan Jr. 2011/11/23 17:50:43 Could you remove it? It shouldn't be necessary now
Takashi Toyoshima 2011/11/23 23:29:43 OK, I remove OrphanedWebSocketServerFilter related
739 public:
740 OrphanedWebSocketServerFilter(
741 const std::string& script_name, const std::string& port)
742 : script_name_(script_name),
743 port_(port) {}
744
745 virtual bool Includes(const base::ProcessEntry& entry) const {
746 // The parent process of orphaned WebSocket servers must be root process.
747 if (entry.parent_pid() != 1)
748 return false;
749
750 // Python script name must be ended with specified script name.
751 const std::string &script_name = entry.cmd_line_args()[2];
752 if (script_name.find_last_of(script_name_) != script_name.size() - 1)
753 return false;
754
755 // Check if an argument sequence like "--port 8880" exists.
756 bool found = false;
757 for (std::vector<std::string>::const_iterator it =
758 entry.cmd_line_args().begin();
759 it != entry.cmd_line_args().end();
760 ++it) {
761 if (*it == "--port") {
762 ++it;
763 if (it == entry.cmd_line_args().end() && *it == port_) {
764 found = true;
765 break;
766 }
767 }
768 }
769 return found;
770 }
771
772 private:
773 std::string script_name_;
774 std::string port_;
775 DISALLOW_COPY_AND_ASSIGN(OrphanedWebSocketServerFilter);
776 };
777 #endif
778
779 TestWebSocketServer::TestWebSocketServer()
780 : started_(false) {
781 #if defined(OS_POSIX)
782 process_handle_ = base::kNullProcessHandle;
783 #endif
731 } 784 }
732 785
733 bool TestWebSocketServer::Start(const FilePath& root_directory) { 786 bool TestWebSocketServer::Start(const FilePath& root_directory) {
734 if (started_) 787 if (started_)
735 return true; 788 return true;
736 // Append CommandLine arguments after the server script, switches won't work. 789 // Append CommandLine arguments after the server script, switches won't work.
737 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine()); 790 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine());
738 cmd_line->AppendArg("--server=start"); 791 cmd_line->AppendArg("--server=start");
739 cmd_line->AppendArg("--chromium"); 792 cmd_line->AppendArg("--chromium");
740 cmd_line->AppendArg("--register_cygwin"); 793 cmd_line->AppendArg("--register_cygwin");
741 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--root=") + 794 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--root=") +
742 root_directory.value()); 795 root_directory.value());
796 cmd_line->AppendArg("--port=" + base::IntToString(kDefaultWsPort));
743 if (!temp_dir_.CreateUniqueTempDir()) { 797 if (!temp_dir_.CreateUniqueTempDir()) {
744 LOG(ERROR) << "Unable to create a temporary directory."; 798 LOG(ERROR) << "Unable to create a temporary directory.";
745 return false; 799 return false;
746 } 800 }
747 websocket_pid_file_ = temp_dir_.path().AppendASCII("websocket.pid"); 801 websocket_pid_file_ = temp_dir_.path().AppendASCII("websocket.pid");
748 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") + 802 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") +
749 websocket_pid_file_.value()); 803 websocket_pid_file_.value());
750 SetPythonPath(); 804 SetPythonPath();
805
751 base::LaunchOptions options; 806 base::LaunchOptions options;
807 base::ProcessHandle* process_handle = NULL;
808
809 #if defined(OS_POSIX)
810 // Try to kill any orphaned WebSocket server processes that may be running.
811 OrphanedWebSocketServerFilter filter("standalone.py",
812 base::IntToString(kDefaultWsPort));
813 if (!base::KillProcesses("python", -1, &filter)) {
814 LOG(WARNING) <<
815 "Failed to clean up older orphaned WebSocket server instance.";
816 }
817
818 options.new_process_group = true;
819 process_handle = &process_handle_;
820 #elif defined(OS_WIN)
821 job_handle_.Set(CreateJobObject(NULL, NULL));
822 if (!job_handle_.IsValid()) {
823 LOG(ERROR) << "Could not create JobObject.";
824 return false;
825 }
826
827 JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info = {0};
828 limit_info.BasicLimitInformation.LimitFlags =
829 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
830 if (0 == SetInformationJobObject(job_handle_.Get(),
831 JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info))) {
832 LOG(ERROR) << "Could not SetInformationJobObject.";
833 return false;
834 }
835
836 options.inherit_handles = true;
837 options.job_handle = job_handle_.Get();
838 #endif
839
840 // Launch a new WebSocket server process.
752 options.wait = true; 841 options.wait = true;
753 if (!base::LaunchProcess(*cmd_line.get(), options, NULL)) { 842 if (!base::LaunchProcess(*cmd_line.get(), options, process_handle)) {
754 LOG(ERROR) << "Unable to launch websocket server."; 843 LOG(ERROR) << "Unable to launch websocket server.";
755 return false; 844 return false;
756 } 845 }
757 started_ = true; 846 started_ = true;
758 return true; 847 return true;
759 } 848 }
760 849
761 CommandLine* TestWebSocketServer::CreatePythonCommandLine() { 850 CommandLine* TestWebSocketServer::CreatePythonCommandLine() {
762 // Note: Python's first argument must be the script; do not append CommandLine 851 // Note: Python's first argument must be the script; do not append CommandLine
763 // switches, as they would precede the script path and break this CommandLine. 852 // switches, as they would precede the script path and break this CommandLine.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 return; 887 return;
799 // Append CommandLine arguments after the server script, switches won't work. 888 // Append CommandLine arguments after the server script, switches won't work.
800 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine()); 889 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine());
801 cmd_line->AppendArg("--server=stop"); 890 cmd_line->AppendArg("--server=stop");
802 cmd_line->AppendArg("--chromium"); 891 cmd_line->AppendArg("--chromium");
803 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") + 892 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") +
804 websocket_pid_file_.value()); 893 websocket_pid_file_.value());
805 base::LaunchOptions options; 894 base::LaunchOptions options;
806 options.wait = true; 895 options.wait = true;
807 base::LaunchProcess(*cmd_line.get(), options, NULL); 896 base::LaunchProcess(*cmd_line.get(), options, NULL);
897
898 #if defined(PS_POSIX)
Paweł Hajdan Jr. 2011/11/23 17:50:43 Ooops, this should be OS_POSIX.
Takashi Toyoshima 2011/11/23 23:29:43 Done.
899 // Just to make sure that the server process terminates certainly.
900 base::KillProcessGroup(process_handle_);
901 #endif
808 } 902 }
809 903
810 TestNotificationObserver::TestNotificationObserver() 904 TestNotificationObserver::TestNotificationObserver()
811 : source_(content::NotificationService::AllSources()) { 905 : source_(content::NotificationService::AllSources()) {
812 } 906 }
813 907
814 TestNotificationObserver::~TestNotificationObserver() {} 908 TestNotificationObserver::~TestNotificationObserver() {}
815 909
816 void TestNotificationObserver::Observe( 910 void TestNotificationObserver::Observe(
817 int type, 911 int type,
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1119 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
1026 } 1120 }
1027 1121
1028 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1122 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
1029 DCHECK(bitmap); 1123 DCHECK(bitmap);
1030 SnapshotTaker taker; 1124 SnapshotTaker taker;
1031 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1125 return taker.TakeEntirePageSnapshot(rvh, bitmap);
1032 } 1126 }
1033 1127
1034 } // namespace ui_test_utils 1128 } // namespace ui_test_utils
OLDNEW
« no previous file with comments | « chrome/test/base/ui_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698