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

Side by Side Diff: chrome/browser/apps/guest_view/app_view_browsertest.cc

Issue 1181893003: Kill bad apps. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/strings/stringprintf.h" 5 #include "base/strings/stringprintf.h"
6 #include "chrome/browser/apps/app_browsertest_util.h" 6 #include "chrome/browser/apps/app_browsertest_util.h"
7 #include "components/guest_view/browser/guest_view_manager.h" 7 #include "components/guest_view/browser/guest_view_manager.h"
8 #include "components/guest_view/browser/guest_view_manager_factory.h" 8 #include "components/guest_view/browser/guest_view_manager_factory.h"
9 #include "components/guest_view/browser/test_guest_view_manager.h" 9 #include "components/guest_view/browser/test_guest_view_manager.h"
10 #include "content/public/browser/notification_observer.h"
10 #include "content/public/browser/notification_service.h" 11 #include "content/public/browser/notification_service.h"
11 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/render_process_host_observer.h"
12 #include "content/public/test/browser_test_utils.h" 14 #include "content/public/test/browser_test_utils.h"
13 #include "content/public/test/test_utils.h" 15 #include "content/public/test/test_utils.h"
16 #include "extensions/browser/app_window/app_window_registry.h"
17 #include "extensions/browser/guest_view/app_view/app_view_guest.h"
18 #include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h "
14 #include "extensions/common/switches.h" 19 #include "extensions/common/switches.h"
15 #include "extensions/test/extension_test_message_listener.h" 20 #include "extensions/test/extension_test_message_listener.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h" 21 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "net/test/embedded_test_server/http_request.h" 22 #include "net/test/embedded_test_server/http_request.h"
18 #include "net/test/embedded_test_server/http_response.h" 23 #include "net/test/embedded_test_server/http_response.h"
19 24
20 using guest_view::GuestViewManager; 25 using guest_view::GuestViewManager;
21 using guest_view::TestGuestViewManagerFactory; 26 using guest_view::TestGuestViewManagerFactory;
22 27
28 namespace {
29
30 class RenderProcessHostObserverForExit
31 : public content::RenderProcessHostObserver {
32 public:
33 RenderProcessHostObserverForExit(content::RenderProcessHost* observed_host)
34 : render_process_host_exited_(false), waiting_(false) {
35 observed_host->AddObserver(this);
36 LOG(INFO) << "Listening for RPH Exit (ID = " << observed_host->GetID()
37 << ")";
38 }
39
40 bool WaitUntilRenderProcessHostKilled() {
41 if (!render_process_host_exited_) {
42 if (!waiting_) {
43 waiting_ = true;
44 content::RunMessageLoop();
45 }
46 }
47 return render_process_host_exited_;
48 }
49
50 private:
51 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override {
lfg 2015/06/12 21:43:46 Use RenderProcessHostExited and check the terminat
EhsanK 2015/06/25 16:19:50 Done.
52 LOG(INFO) << "RPH Exiting (ID = " << host->GetID() << ")";
53 host->RemoveObserver(this);
lfg 2015/06/12 21:43:46 The host was destroyed, is this needed/does it wor
EhsanK 2015/06/25 16:19:50 I copied the pattern from other similar observers.
lfg 2015/06/26 21:46:05 Acknowledged.
EhsanK 2015/06/30 16:53:10 Done.
EhsanK 2015/06/30 16:53:10 Done.
54 render_process_host_exited_ = true;
55 if (waiting_) {
56 waiting_ = false;
57 base::MessageLoopForUI::current()->Quit();
58 }
59 }
60
61 bool render_process_host_exited_;
62 bool waiting_;
63 };
64
65 // This is a mock callback function used for the tests "TestKill*".
66 void OnWebContentsCreated(content::WebContents* webcontents) {
67 LOG(ERROR) << "The RPH was not killed.";
68 }
69
70 } // namespace
71
23 class AppViewTest : public extensions::PlatformAppBrowserTest { 72 class AppViewTest : public extensions::PlatformAppBrowserTest {
24 public: 73 public:
25 AppViewTest() { 74 AppViewTest() {
26 GuestViewManager::set_factory_for_testing(&factory_); 75 GuestViewManager::set_factory_for_testing(&factory_);
27 } 76 }
28 77
29 enum TestServer { 78 enum TestServer {
30 NEEDS_TEST_SERVER, 79 NEEDS_TEST_SERVER,
31 NO_TEST_SERVER 80 NO_TEST_SERVER
32 }; 81 };
(...skipping 19 matching lines...) Expand all
52 GetFirstAppWindowWebContents(); 101 GetFirstAppWindowWebContents();
53 if (!embedder_web_contents) { 102 if (!embedder_web_contents) {
54 LOG(ERROR) << "UNABLE TO FIND EMBEDDER WEB CONTENTS."; 103 LOG(ERROR) << "UNABLE TO FIND EMBEDDER WEB CONTENTS.";
55 return; 104 return;
56 } 105 }
57 106
58 ExtensionTestMessageListener done_listener("TEST_PASSED", false); 107 ExtensionTestMessageListener done_listener("TEST_PASSED", false);
59 done_listener.set_failure_message("TEST_FAILED"); 108 done_listener.set_failure_message("TEST_FAILED");
60 if (!content::ExecuteScript( 109 if (!content::ExecuteScript(
61 embedder_web_contents, 110 embedder_web_contents,
62 base::StringPrintf("runTest('%s', '%s')", 111 base::StringPrintf("runTest('%s', '%s')", test_name.c_str(),
63 test_name.c_str(),
64 app_to_embed.c_str()))) { 112 app_to_embed.c_str()))) {
65 LOG(ERROR) << "UNABLE TO START TEST."; 113 LOG(ERROR) << "UNABLE TO START TEST.";
66 return; 114 return;
67 } 115 }
68 ASSERT_TRUE(done_listener.WaitUntilSatisfied()); 116 ASSERT_TRUE(done_listener.WaitUntilSatisfied());
69 } 117 }
70 118
119 guest_view::TestGuestViewManager* GetGuestViewManager() {
120 guest_view::TestGuestViewManager* manager =
121 static_cast<guest_view::TestGuestViewManager*>(
122 guest_view::TestGuestViewManager::FromBrowserContext(
123 browser()->profile()));
124 if (!manager) {
125 manager = static_cast<guest_view::TestGuestViewManager*>(
126 GuestViewManager::CreateWithDelegate(
127 browser()->profile(),
128 scoped_ptr<guest_view::GuestViewManagerDelegate>(
129 new extensions::ExtensionsGuestViewManagerDelegate(
130 browser()->profile()))));
131 }
132 return manager;
133 }
134
71 private: 135 private:
72 void SetUpCommandLine(base::CommandLine* command_line) override { 136 void SetUpCommandLine(base::CommandLine* command_line) override {
73 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); 137 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
74 } 138 }
75 139
76 TestGuestViewManagerFactory factory_; 140 TestGuestViewManagerFactory factory_;
77 }; 141 };
78 142
79 // Tests that <appview> is able to navigate to another installed app. 143 // Tests that <appview> is able to navigate to another installed app.
80 IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewWithUndefinedDataShouldSucceed) { 144 IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewWithUndefinedDataShouldSucceed) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 182
119 // Tests that <appview> does not embed self (the app which owns appview). 183 // Tests that <appview> does not embed self (the app which owns appview).
120 IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewEmbedSelfShouldFail) { 184 IN_PROC_BROWSER_TEST_F(AppViewTest, TestAppViewEmbedSelfShouldFail) {
121 const extensions::Extension* skeleton_app = 185 const extensions::Extension* skeleton_app =
122 InstallPlatformApp("app_view/shim/skeleton"); 186 InstallPlatformApp("app_view/shim/skeleton");
123 TestHelper("testAppViewEmbedSelfShouldFail", 187 TestHelper("testAppViewEmbedSelfShouldFail",
124 "app_view/shim", 188 "app_view/shim",
125 skeleton_app->id(), 189 skeleton_app->id(),
126 NO_TEST_SERVER); 190 NO_TEST_SERVER);
127 } 191 }
192
193 IN_PROC_BROWSER_TEST_F(AppViewTest, TestKillGuestWithInvalidInstanceID) {
194 const extensions::Extension* mock_bad_app =
195 LoadAndLaunchPlatformApp("app_view/bad_app", "AppViewTest.LAUNCHED");
196
197 content::RenderProcessHost* bad_app_render_process_host =
198 extensions::AppWindowRegistry::Get(browser()->profile())
199 ->GetCurrentAppWindowForApp(mock_bad_app->id())
200 ->web_contents()
201 ->GetRenderProcessHost();
202
203 // Monitor |mock_bad_app|'s "RenderProcessHost" for its exiting.
204 RenderProcessHostObserverForExit exit_observer(bad_app_render_process_host);
205
206 // Get/Create the instance of "GuestViewManager".
207 guest_view::GuestViewManager* guest_view_manager = GetGuestViewManager();
208 EXPECT_TRUE(guest_view_manager != NULL);
209
210 // Choosing a |guest_instance_id| which does not exist.
211 int invalid_guest_instance_id = guest_view_manager->GetNextInstanceID();
212 LOG(INFO) << "Guest instance ID: " << invalid_guest_instance_id;
213
214 // Call the desired function to verify that the |mock_bad_app| gets killed if
215 // the provided |guest_instance_id| is not mapped to any "GuestView"'s.
216 extensions::AppViewGuest::CompletePendingRequest(
217 browser()->profile(), GURL("about:blank"), invalid_guest_instance_id,
218 mock_bad_app->id(), bad_app_render_process_host->GetID());
219 ASSERT_TRUE(exit_observer.WaitUntilRenderProcessHostKilled());
220 }
221
222 IN_PROC_BROWSER_TEST_F(AppViewTest,
223 TestKillGuestCommunicatingWithWrongAppView) {
224 const extensions::Extension* mock_host_app =
225 LoadAndLaunchPlatformApp("app_view/host_app", "AppViewTest.LAUNCHED");
226 const extensions::Extension* mock_guest_extension =
227 InstallPlatformApp("app_view/guest_app");
228 const extensions::Extension* mock_bad_app =
229 LoadAndLaunchPlatformApp("app_view/bad_app", "AppViewTest.LAUNCHED");
230
231 extensions::AppWindowRegistry* app_window_registry =
232 extensions::AppWindowRegistry::Get(browser()->profile());
233 guest_view::GuestViewManager* guest_view_manager = GetGuestViewManager();
234 EXPECT_TRUE(guest_view_manager != NULL);
235 content::WebContents* host_contents =
236 app_window_registry->GetCurrentAppWindowForApp(mock_host_app->id())
237 ->web_contents();
238 extensions::AppViewGuest* app_view =
239 extensions::AppViewGuest::Create(host_contents)
240 ->As<extensions::AppViewGuest>();
241
242 guest_view::GuestViewBase::WebContentsCreatedCallback callback =
243 base::Bind(OnWebContentsCreated);
244
245 int guest_instance_id = GetGuestViewManager()->GetNextInstanceID();
246 LOG(INFO) << "Guest instance ID: " << guest_instance_id;
247
248 // Add an entry for some guest extension and an <appview>.
249 extensions::AppViewGuest::AddFakeRequestInfoForTesting(
250 mock_guest_extension, app_view, callback, guest_instance_id);
251 // Observe |mock_bad_app|'s "RenderProcessHost".
252 content::RenderProcessHost* bad_app_render_process_host =
253 app_window_registry->GetCurrentAppWindowForApp(mock_bad_app->id())
254 ->web_contents()
255 ->GetRenderProcessHost();
256
257 RenderProcessHostObserverForExit exit_observer(bad_app_render_process_host);
258
259 // Now call "AppViewGuest::CompletePendingRequest" form the "mock_bad_app".
260 extensions::AppViewGuest::CompletePendingRequest(
261 browser()->profile(), GURL("about:blank"), guest_instance_id,
262 mock_bad_app->id(), bad_app_render_process_host->GetID());
263 ASSERT_TRUE(exit_observer.WaitUntilRenderProcessHostKilled());
264 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698