Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "apps/app_shim/app_shim_host_manager_mac.h" | 5 #include "apps/app_shim/app_shim_host_manager_mac.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include "apps/app_shim/app_shim_messages.h" | 9 #include "apps/app_shim/app_shim_messages.h" |
| 10 #include "apps/app_shim/test/app_shim_host_manager_test_api_mac.h" | 10 #include "apps/app_shim/test/app_shim_host_manager_test_api_mac.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "ipc/ipc_listener.h" | 22 #include "ipc/ipc_listener.h" |
| 23 #include "ipc/ipc_message.h" | 23 #include "ipc/ipc_message.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kTestAppMode[] = "test_app"; | 27 const char kTestAppMode[] = "test_app"; |
| 28 | 28 |
| 29 // A test version of the AppShimController IPC client in chrome_main_app_mode. | 29 // A test version of the AppShimController IPC client in chrome_main_app_mode. |
| 30 class TestShimClient : public IPC::Listener { | 30 class TestShimClient : public IPC::Listener { |
| 31 public: | 31 public: |
| 32 TestShimClient(const base::FilePath& socket_path); | 32 TestShimClient(); |
| 33 virtual ~TestShimClient(); | 33 virtual ~TestShimClient(); |
| 34 | 34 |
| 35 template <class T> | 35 template <class T> |
| 36 void Send(const T& message) { | 36 void Send(const T& message) { |
| 37 channel_->Send(message); | 37 channel_->Send(message); |
| 38 } | 38 } |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 // IPC::Listener overrides: | 41 // IPC::Listener overrides: |
| 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 42 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 43 virtual void OnChannelError() OVERRIDE; | 43 virtual void OnChannelError() OVERRIDE; |
| 44 | 44 |
| 45 base::Thread io_thread_; | 45 base::Thread io_thread_; |
| 46 scoped_ptr<IPC::ChannelProxy> channel_; | 46 scoped_ptr<IPC::ChannelProxy> channel_; |
| 47 | 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(TestShimClient); | 48 DISALLOW_COPY_AND_ASSIGN(TestShimClient); |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 TestShimClient::TestShimClient(const base::FilePath& socket_path) | 51 TestShimClient::TestShimClient() |
| 52 : io_thread_("TestShimClientIO") { | 52 : io_thread_("TestShimClientIO") { |
| 53 base::Thread::Options io_thread_options; | 53 base::Thread::Options io_thread_options; |
| 54 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 54 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 55 io_thread_.StartWithOptions(io_thread_options); | 55 io_thread_.StartWithOptions(io_thread_options); |
| 56 | 56 |
| 57 base::FilePath socket_path; | |
| 58 PathService::Get(chrome::DIR_USER_DATA, &socket_path); | |
|
tapted
2013/11/21 11:12:05
nit: check return value
jackhou1
2013/11/22 00:20:40
Done.
| |
| 59 socket_path = socket_path.Append(app_mode::kAppShimSocketName); | |
|
tapted
2013/11/21 11:12:05
should this be part of GetShortSocketPath?
Then i
jackhou1
2013/11/22 00:20:40
Changed it so that GetShortSocketPath takes the us
| |
| 60 socket_path = app_mode::GetShortSocketPath(socket_path); | |
| 61 | |
| 57 IPC::ChannelHandle handle(socket_path.value()); | 62 IPC::ChannelHandle handle(socket_path.value()); |
| 58 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, | 63 channel_.reset(new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, |
| 59 this, io_thread_.message_loop_proxy().get())); | 64 this, io_thread_.message_loop_proxy().get())); |
| 60 } | 65 } |
| 61 | 66 |
| 62 TestShimClient::~TestShimClient() {} | 67 TestShimClient::~TestShimClient() {} |
| 63 | 68 |
| 64 bool TestShimClient::OnMessageReceived(const IPC::Message& message) { | 69 bool TestShimClient::OnMessageReceived(const IPC::Message& message) { |
| 65 return true; | 70 return true; |
| 66 } | 71 } |
| 67 | 72 |
| 68 void TestShimClient::OnChannelError() { | 73 void TestShimClient::OnChannelError() { |
| 69 // Client should not get any channel errors for the current set of tests. | 74 // Client should not get any channel errors for the current set of tests. |
| 70 PLOG(FATAL) << "ChannelError"; | 75 PLOG(FATAL) << "ChannelError"; |
| 71 } | 76 } |
| 72 | 77 |
| 73 // Browser Test for AppShimHostManager to test IPC interactions across the | 78 // Browser Test for AppShimHostManager to test IPC interactions across the |
| 74 // UNIX domain socket. | 79 // UNIX domain socket. |
| 75 class AppShimHostManagerBrowserTest : public InProcessBrowserTest, | 80 class AppShimHostManagerBrowserTest : public InProcessBrowserTest, |
| 76 public apps::AppShimHandler { | 81 public apps::AppShimHandler { |
| 77 public: | 82 public: |
| 78 AppShimHostManagerBrowserTest(); | 83 AppShimHostManagerBrowserTest(); |
| 79 virtual ~AppShimHostManagerBrowserTest(); | 84 virtual ~AppShimHostManagerBrowserTest(); |
| 80 | 85 |
| 81 protected: | 86 protected: |
| 82 // Wait for OnShimLaunch, then send a quit, and wait for the response. Used to | 87 // Wait for OnShimLaunch, then send a quit, and wait for the response. Used to |
| 83 // test launch behavior. | 88 // test launch behavior. |
| 84 void RunAndExitGracefully(); | 89 void RunAndExitGracefully(); |
| 85 | 90 |
| 86 // InProcessBrowserTest overrides: | |
| 87 virtual bool SetUpUserDataDirectory() OVERRIDE; | |
| 88 | |
| 89 // AppShimHandler overrides: | 91 // AppShimHandler overrides: |
| 90 virtual void OnShimLaunch(apps::AppShimHandler::Host* host, | 92 virtual void OnShimLaunch(apps::AppShimHandler::Host* host, |
| 91 apps::AppShimLaunchType launch_type, | 93 apps::AppShimLaunchType launch_type, |
| 92 const std::vector<base::FilePath>& files) OVERRIDE; | 94 const std::vector<base::FilePath>& files) OVERRIDE; |
| 93 virtual void OnShimClose(apps::AppShimHandler::Host* host) OVERRIDE {} | 95 virtual void OnShimClose(apps::AppShimHandler::Host* host) OVERRIDE {} |
| 94 virtual void OnShimFocus(apps::AppShimHandler::Host* host, | 96 virtual void OnShimFocus(apps::AppShimHandler::Host* host, |
| 95 apps::AppShimFocusType focus_type, | 97 apps::AppShimFocusType focus_type, |
| 96 const std::vector<base::FilePath>& files) OVERRIDE {} | 98 const std::vector<base::FilePath>& files) OVERRIDE {} |
| 97 virtual void OnShimSetHidden(apps::AppShimHandler::Host* host, | 99 virtual void OnShimSetHidden(apps::AppShimHandler::Host* host, |
| 98 bool hidden) OVERRIDE {} | 100 bool hidden) OVERRIDE {} |
| 99 virtual void OnShimQuit(apps::AppShimHandler::Host* host) OVERRIDE; | 101 virtual void OnShimQuit(apps::AppShimHandler::Host* host) OVERRIDE; |
| 100 | 102 |
| 101 scoped_ptr<TestShimClient> test_client_; | 103 scoped_ptr<TestShimClient> test_client_; |
| 102 base::FilePath short_socket_path_; | |
| 103 std::vector<base::FilePath> last_launch_files_; | 104 std::vector<base::FilePath> last_launch_files_; |
| 104 apps::AppShimLaunchType last_launch_type_; | 105 apps::AppShimLaunchType last_launch_type_; |
| 105 | 106 |
| 106 private: | 107 private: |
| 107 scoped_refptr<content::MessageLoopRunner> runner_; | 108 scoped_refptr<content::MessageLoopRunner> runner_; |
| 108 base::ScopedTempDir short_temp_dir_; | 109 base::ScopedTempDir short_temp_dir_; |
| 109 | 110 |
| 110 int launch_count_; | 111 int launch_count_; |
| 111 int quit_count_; | 112 int quit_count_; |
| 112 | 113 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 132 | 133 |
| 133 runner_ = new content::MessageLoopRunner(); | 134 runner_ = new content::MessageLoopRunner(); |
| 134 test_client_->Send(new AppShimHostMsg_QuitApp); | 135 test_client_->Send(new AppShimHostMsg_QuitApp); |
| 135 EXPECT_EQ(0, quit_count_); | 136 EXPECT_EQ(0, quit_count_); |
| 136 runner_->Run(); // Will stop in OnShimQuit(). | 137 runner_->Run(); // Will stop in OnShimQuit(). |
| 137 EXPECT_EQ(1, quit_count_); | 138 EXPECT_EQ(1, quit_count_); |
| 138 | 139 |
| 139 test_client_.reset(); | 140 test_client_.reset(); |
| 140 } | 141 } |
| 141 | 142 |
| 142 bool AppShimHostManagerBrowserTest::SetUpUserDataDirectory() { | |
| 143 // Create a symlink at /tmp/scoped_dir_XXXXXX/udd that points to the real user | |
| 144 // data dir, and use this as the domain socket path. This is required because | |
| 145 // there is a path length limit for named sockets that is exceeded in | |
| 146 // multi-process test spawning. | |
| 147 base::FilePath real_user_data_dir; | |
| 148 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &real_user_data_dir)); | |
| 149 EXPECT_TRUE( | |
| 150 short_temp_dir_.CreateUniqueTempDirUnderPath(base::FilePath("/tmp"))); | |
| 151 base::FilePath shortened_user_data_dir = short_temp_dir_.path().Append("udd"); | |
| 152 EXPECT_EQ(0, ::symlink(real_user_data_dir.AsUTF8Unsafe().c_str(), | |
| 153 shortened_user_data_dir.AsUTF8Unsafe().c_str())); | |
| 154 | |
| 155 test::AppShimHostManagerTestApi::OverrideUserDataDir(shortened_user_data_dir); | |
| 156 short_socket_path_ = | |
| 157 shortened_user_data_dir.Append(app_mode::kAppShimSocketName); | |
| 158 | |
| 159 return InProcessBrowserTest::SetUpUserDataDirectory(); | |
| 160 } | |
| 161 | |
| 162 void AppShimHostManagerBrowserTest::OnShimLaunch( | 143 void AppShimHostManagerBrowserTest::OnShimLaunch( |
| 163 apps::AppShimHandler::Host* host, | 144 apps::AppShimHandler::Host* host, |
| 164 apps::AppShimLaunchType launch_type, | 145 apps::AppShimLaunchType launch_type, |
| 165 const std::vector<base::FilePath>& files) { | 146 const std::vector<base::FilePath>& files) { |
| 166 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); | 147 host->OnAppLaunchComplete(apps::APP_SHIM_LAUNCH_SUCCESS); |
| 167 ++launch_count_; | 148 ++launch_count_; |
| 168 last_launch_type_ = launch_type; | 149 last_launch_type_ = launch_type; |
| 169 last_launch_files_ = files; | 150 last_launch_files_ = files; |
| 170 runner_->Quit(); | 151 runner_->Quit(); |
| 171 } | 152 } |
| 172 | 153 |
| 173 void AppShimHostManagerBrowserTest::OnShimQuit( | 154 void AppShimHostManagerBrowserTest::OnShimQuit( |
| 174 apps::AppShimHandler::Host* host) { | 155 apps::AppShimHandler::Host* host) { |
| 175 ++quit_count_; | 156 ++quit_count_; |
| 176 runner_->Quit(); | 157 runner_->Quit(); |
| 177 } | 158 } |
| 178 | 159 |
| 179 // Test regular launch, which would ask Chrome to launch the app. | 160 // Test regular launch, which would ask Chrome to launch the app. |
| 180 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { | 161 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchNormal) { |
| 181 test_client_.reset(new TestShimClient(short_socket_path_)); | 162 test_client_.reset(new TestShimClient()); |
| 182 test_client_->Send(new AppShimHostMsg_LaunchApp( | 163 test_client_->Send(new AppShimHostMsg_LaunchApp( |
| 183 browser()->profile()->GetPath(), | 164 browser()->profile()->GetPath(), |
| 184 kTestAppMode, | 165 kTestAppMode, |
| 185 apps::APP_SHIM_LAUNCH_NORMAL, | 166 apps::APP_SHIM_LAUNCH_NORMAL, |
| 186 std::vector<base::FilePath>())); | 167 std::vector<base::FilePath>())); |
| 187 | 168 |
| 188 RunAndExitGracefully(); | 169 RunAndExitGracefully(); |
| 189 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); | 170 EXPECT_EQ(apps::APP_SHIM_LAUNCH_NORMAL, last_launch_type_); |
| 190 EXPECT_TRUE(last_launch_files_.empty()); | 171 EXPECT_TRUE(last_launch_files_.empty()); |
| 191 } | 172 } |
| 192 | 173 |
| 193 // Test register-only launch, used when Chrome has already launched the app. | 174 // Test register-only launch, used when Chrome has already launched the app. |
| 194 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { | 175 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTest, LaunchRegisterOnly) { |
| 195 test_client_.reset(new TestShimClient(short_socket_path_)); | 176 test_client_.reset(new TestShimClient()); |
| 196 test_client_->Send(new AppShimHostMsg_LaunchApp( | 177 test_client_->Send(new AppShimHostMsg_LaunchApp( |
| 197 browser()->profile()->GetPath(), | 178 browser()->profile()->GetPath(), |
| 198 kTestAppMode, | 179 kTestAppMode, |
| 199 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, | 180 apps::APP_SHIM_LAUNCH_REGISTER_ONLY, |
| 200 std::vector<base::FilePath>())); | 181 std::vector<base::FilePath>())); |
| 201 | 182 |
| 202 RunAndExitGracefully(); | 183 RunAndExitGracefully(); |
| 203 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); | 184 EXPECT_EQ(apps::APP_SHIM_LAUNCH_REGISTER_ONLY, last_launch_type_); |
| 204 EXPECT_TRUE(last_launch_files_.empty()); | 185 EXPECT_TRUE(last_launch_files_.empty()); |
| 205 } | 186 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 | 218 |
| 238 bool AppShimHostManagerBrowserTestFailsCreate::SetUpUserDataDirectory() { | 219 bool AppShimHostManagerBrowserTestFailsCreate::SetUpUserDataDirectory() { |
| 239 base::FilePath user_data_dir; | 220 base::FilePath user_data_dir; |
| 240 // Start in the "real" user data dir for this test. This is a meta-test for | 221 // Start in the "real" user data dir for this test. This is a meta-test for |
| 241 // the symlinking steps used in the superclass. That is, by putting the | 222 // the symlinking steps used in the superclass. That is, by putting the |
| 242 // clobber in the actual user data dir, the test will fail if the symlink | 223 // clobber in the actual user data dir, the test will fail if the symlink |
| 243 // does not actually point to the user data dir, since it won't be clobbered. | 224 // does not actually point to the user data dir, since it won't be clobbered. |
| 244 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); | 225 EXPECT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)); |
| 245 base::FilePath socket_path = | 226 base::FilePath socket_path = |
| 246 user_data_dir.Append(app_mode::kAppShimSocketName); | 227 user_data_dir.Append(app_mode::kAppShimSocketName); |
| 228 socket_path = app_mode::GetShortSocketPath(socket_path); | |
| 247 // Create a "barrier" to forming the UNIX domain socket. This is just a | 229 // Create a "barrier" to forming the UNIX domain socket. This is just a |
| 248 // pre-existing directory which can not be unlink()ed, in order to place a | 230 // pre-existing directory which can not be unlink()ed, in order to place a |
| 249 // named socked there instead. | 231 // named socket there instead. |
| 250 EXPECT_TRUE(barrier_dir_.Set(socket_path)); | 232 EXPECT_TRUE(barrier_dir_.Set(socket_path)); |
| 251 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory(); | 233 return AppShimHostManagerBrowserTest::SetUpUserDataDirectory(); |
| 252 } | 234 } |
| 253 | 235 |
| 254 // Test error handling. This is essentially testing for lifetime correctness | 236 // Test error handling. This is essentially testing for lifetime correctness |
| 255 // during startup for unexpected failures. | 237 // during startup for unexpected failures. |
| 256 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestFailsCreate, | 238 IN_PROC_BROWSER_TEST_F(AppShimHostManagerBrowserTestFailsCreate, |
| 257 SocketFailure) { | 239 SocketFailure) { |
| 258 test::AppShimHostManagerTestApi test_api( | 240 test::AppShimHostManagerTestApi test_api( |
| 259 g_browser_process->platform_part()->app_shim_host_manager()); | 241 g_browser_process->platform_part()->app_shim_host_manager()); |
| 260 EXPECT_FALSE(test_api.factory()); | 242 EXPECT_FALSE(test_api.factory()); |
| 261 } | 243 } |
| 262 | 244 |
| 263 } // namespace | 245 } // namespace |
| OLD | NEW |