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

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: revised Created 9 years 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 {
739 public:
740 OrphanedWebSocketServerFilter(std::string script_name, std::string port)
Yuta Kitamura 2011/11/23 06:06:09 Arguments should be typed as const std::string&, b
Takashi Toyoshima 2011/11/23 08:53:32 Done.
741 : script_name_(script_name),
742 port_(port) {}
743
744 virtual bool Includes(const base::ProcessEntry& entry) const {
745 // The parent process of orphaned WebSocket servers must be root process.
746 if (entry.parent_pid() != 1)
747 return false;
748
749 // Python script name must be ended with specified script name.
750 const std::string &script_name = entry.cmd_line_args()[2];
751 if (script_name.find_last_of(script_name_) != script_name.size() - 1)
752 return false;
753
754 // Check if an argument sequence like "--port 8880" exist.
Yuta Kitamura 2011/11/23 06:06:09 nit: exist -> exists.
Takashi Toyoshima 2011/11/23 08:53:32 Done.
755 bool found = false;
756 for (std::vector<std::string>::const_iterator it =
757 entry.cmd_line_args().begin();
758 it != entry.cmd_line_args().end();
759 ++it) {
760 if (*it == "--port") {
761 ++it;
762 if (it == entry.cmd_line_args().end() && *it == port_) {
763 found = true;
764 break;
765 }
766 }
767 }
768 return found;
769 }
770
771 private:
772 std::string script_name_;
773 std::string port_;
774 DISALLOW_COPY_AND_ASSIGN(OrphanedWebSocketServerFilter);
775 };
776 #endif
777
778 TestWebSocketServer::TestWebSocketServer()
779 : started_(false),
780 process_handle_(base::kNullProcessHandle) {
Yuta Kitamura 2011/11/23 06:06:09 Don't you have to guard process_handle_ with #if d
Takashi Toyoshima 2011/11/23 08:53:32 Oh, right! Nice catch.
731 } 781 }
732 782
733 bool TestWebSocketServer::Start(const FilePath& root_directory) { 783 bool TestWebSocketServer::Start(const FilePath& root_directory) {
734 if (started_) 784 if (started_)
735 return true; 785 return true;
736 // Append CommandLine arguments after the server script, switches won't work. 786 // Append CommandLine arguments after the server script, switches won't work.
737 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine()); 787 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine());
738 cmd_line->AppendArg("--server=start"); 788 cmd_line->AppendArg("--server=start");
739 cmd_line->AppendArg("--chromium"); 789 cmd_line->AppendArg("--chromium");
740 cmd_line->AppendArg("--register_cygwin"); 790 cmd_line->AppendArg("--register_cygwin");
741 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--root=") + 791 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--root=") +
742 root_directory.value()); 792 root_directory.value());
793 cmd_line->AppendArg("--port=" + base::IntToString(kDefaultWsPort));
743 if (!temp_dir_.CreateUniqueTempDir()) { 794 if (!temp_dir_.CreateUniqueTempDir()) {
744 LOG(ERROR) << "Unable to create a temporary directory."; 795 LOG(ERROR) << "Unable to create a temporary directory.";
745 return false; 796 return false;
746 } 797 }
747 websocket_pid_file_ = temp_dir_.path().AppendASCII("websocket.pid"); 798 websocket_pid_file_ = temp_dir_.path().AppendASCII("websocket.pid");
748 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") + 799 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") +
749 websocket_pid_file_.value()); 800 websocket_pid_file_.value());
750 SetPythonPath(); 801 SetPythonPath();
802
751 base::LaunchOptions options; 803 base::LaunchOptions options;
804
805 #if defined(OS_POSIX)
806 // Try to kill any orphaned WebSocket server processes that may be running.
807 OrphanedWebSocketServerFilter filter("standalone.py",
808 base::IntToString(kDefaultWsPort));
809 if (!base::KillProcesses("python", -1, &filter))
Yuta Kitamura 2011/11/23 06:06:09 nit: Add curly braces if the content of a block sp
Takashi Toyoshima 2011/11/23 08:53:32 Done.
810 LOG(WARNING) <<
811 "Failed to clean up older orphaned WebSocket server instance.";
812
813 options.new_process_group = true;
814 #elif defined(OS_WIN)
815 job_handle_.Set(CreateJobObject(NULL, NULL));
816 if (!job_handle_.IsValid()) {
817 LOG(ERROR) << "Could not create JobObject.";
818 return false;
819 }
820
821 JOBOBJECT_EXTENDED_LIMIT_INFORMATION limit_info = {0};
822 limit_info.BasicLimitInformation.LimitFlags =
823 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
824 if (0 == SetInformationJobObject(job_handle_.Get(),
Yuta Kitamura 2011/11/23 06:06:09 nit: Prefer (x == 0) over (0 == x). Maybe it's bet
Takashi Toyoshima 2011/11/23 08:53:32 For now, other four place use this exactly same id
825 JobObjectExtendedLimitInformation, &limit_info, sizeof(limit_info))) {
826 LOG(ERROR) << "Could not SetInformationJobObject.";
827 return false;
828 }
829
830 options.inherit_handles = true;
831 options.job_handle = job_handle_.Get();
832 #endif
833
834 // Launch a new WebSocket server process.
752 options.wait = true; 835 options.wait = true;
753 if (!base::LaunchProcess(*cmd_line.get(), options, NULL)) { 836 if (!base::LaunchProcess(*cmd_line.get(), options, &process_handle_)) {
Yuta Kitamura 2011/11/23 06:06:09 Same here for #if defined(OS_POSIX) for process_ha
Takashi Toyoshima 2011/11/23 08:53:32 Done.
754 LOG(ERROR) << "Unable to launch websocket server."; 837 LOG(ERROR) << "Unable to launch websocket server.";
755 return false; 838 return false;
756 } 839 }
757 started_ = true; 840 started_ = true;
758 return true; 841 return true;
759 } 842 }
760 843
761 CommandLine* TestWebSocketServer::CreatePythonCommandLine() { 844 CommandLine* TestWebSocketServer::CreatePythonCommandLine() {
762 // Note: Python's first argument must be the script; do not append CommandLine 845 // 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. 846 // 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; 881 return;
799 // Append CommandLine arguments after the server script, switches won't work. 882 // Append CommandLine arguments after the server script, switches won't work.
800 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine()); 883 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine());
801 cmd_line->AppendArg("--server=stop"); 884 cmd_line->AppendArg("--server=stop");
802 cmd_line->AppendArg("--chromium"); 885 cmd_line->AppendArg("--chromium");
803 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") + 886 cmd_line->AppendArgNative(FILE_PATH_LITERAL("--pidfile=") +
804 websocket_pid_file_.value()); 887 websocket_pid_file_.value());
805 base::LaunchOptions options; 888 base::LaunchOptions options;
806 options.wait = true; 889 options.wait = true;
807 base::LaunchProcess(*cmd_line.get(), options, NULL); 890 base::LaunchProcess(*cmd_line.get(), options, NULL);
891
892 // Just to make sure that the server process terminates certainly.
893 base::KillProcessGroup(process_handle_);
Yuta Kitamura 2011/11/23 06:06:09 Ditto.
Takashi Toyoshima 2011/11/23 08:53:32 Done.
808 } 894 }
809 895
810 TestNotificationObserver::TestNotificationObserver() 896 TestNotificationObserver::TestNotificationObserver()
811 : source_(content::NotificationService::AllSources()) { 897 : source_(content::NotificationService::AllSources()) {
812 } 898 }
813 899
814 TestNotificationObserver::~TestNotificationObserver() {} 900 TestNotificationObserver::~TestNotificationObserver() {}
815 901
816 void TestNotificationObserver::Observe( 902 void TestNotificationObserver::Observe(
817 int type, 903 int type,
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1111 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
1026 } 1112 }
1027 1113
1028 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1114 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
1029 DCHECK(bitmap); 1115 DCHECK(bitmap);
1030 SnapshotTaker taker; 1116 SnapshotTaker taker;
1031 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1117 return taker.TakeEntirePageSnapshot(rvh, bitmap);
1032 } 1118 }
1033 1119
1034 } // namespace ui_test_utils 1120 } // 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