[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
vivek.vg | d155cb80 | 2014-12-22 14:37:10 | [diff] [blame] | 5 | #include "chrome/test/base/chrome_render_view_test.h" |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 6 | #include "content/public/renderer/render_frame.h" |
| 7 | #include "content/public/test/frame_load_waiter.h" |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 8 | #include "extensions/renderer/script_context.h" |
Jeremy Roman | 1a4d9b8 | 2017-12-08 01:46:40 | [diff] [blame] | 9 | #include "extensions/renderer/script_context_set.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame^] | 10 | #include "third_party/blink/public/web/web_document.h" |
| 11 | #include "third_party/blink/public/web/web_local_frame.h" |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 12 | #include "url/gurl.h" |
| 13 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 14 | using blink::WebLocalFrame; |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 15 | |
| 16 | namespace extensions { |
| 17 | namespace { |
| 18 | |
vivek.vg | d155cb80 | 2014-12-22 14:37:10 | [diff] [blame] | 19 | class ScriptContextTest : public ChromeRenderViewTest { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 20 | protected: |
lukasza | bedb4b2 | 2017-06-23 00:00:13 | [diff] [blame] | 21 | GURL GetEffectiveDocumentURL(WebLocalFrame* frame) { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 22 | return ScriptContext::GetEffectiveDocumentURL( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 23 | frame, frame->GetDocument().Url(), true); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 24 | } |
| 25 | }; |
| 26 | |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 27 | TEST_F(ScriptContextTest, GetEffectiveDocumentURL) { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 28 | GURL top_url("http://example.com/"); |
| 29 | GURL different_url("http://example.net/"); |
| 30 | GURL blank_url("about:blank"); |
| 31 | GURL srcdoc_url("about:srcdoc"); |
| 32 | |
| 33 | const char frame_html[] = |
| 34 | "<iframe name='frame1' srcdoc=\"" |
| 35 | " <iframe name='frame1_1'></iframe>" |
| 36 | " <iframe name='frame1_2' sandbox=''></iframe>" |
| 37 | "\"></iframe>" |
| 38 | "<iframe name='frame2' sandbox='' srcdoc=\"" |
| 39 | " <iframe name='frame2_1'></iframe>" |
| 40 | "\"></iframe>" |
| 41 | "<iframe name='frame3'></iframe>"; |
| 42 | |
| 43 | const char frame3_html[] = "<iframe name='frame3_1'></iframe>"; |
| 44 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 45 | WebLocalFrame* frame = GetMainFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 46 | ASSERT_TRUE(frame); |
| 47 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 48 | frame->LoadHTMLString(frame_html, top_url); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 49 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 50 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 51 | WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 52 | ASSERT_TRUE(frame1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 53 | ASSERT_EQ("frame1", frame1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 54 | WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 55 | ASSERT_TRUE(frame1_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 56 | ASSERT_EQ("frame1_1", frame1_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 57 | WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 58 | ASSERT_TRUE(frame1_2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 59 | ASSERT_EQ("frame1_2", frame1_2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 60 | WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 61 | ASSERT_TRUE(frame2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 62 | ASSERT_EQ("frame2", frame2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 63 | WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 64 | ASSERT_TRUE(frame2_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 65 | ASSERT_EQ("frame2_1", frame2_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 66 | WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 67 | ASSERT_TRUE(frame3); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 68 | ASSERT_EQ("frame3", frame3->AssignedName()); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 69 | |
| 70 | // Load a blank document in a frame from a different origin. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 71 | frame3->LoadHTMLString(frame3_html, different_url); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 72 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 73 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame] | 74 | WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 75 | ASSERT_TRUE(frame3_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 76 | ASSERT_EQ("frame3_1", frame3_1->AssignedName()); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 77 | |
| 78 | // Top-level frame |
| 79 | EXPECT_EQ(GetEffectiveDocumentURL(frame), top_url); |
| 80 | // top -> srcdoc = inherit |
| 81 | EXPECT_EQ(GetEffectiveDocumentURL(frame1), top_url); |
| 82 | // top -> srcdoc -> about:blank = inherit |
| 83 | EXPECT_EQ(GetEffectiveDocumentURL(frame1_1), top_url); |
| 84 | // top -> srcdoc -> about:blank sandboxed = same URL |
| 85 | EXPECT_EQ(GetEffectiveDocumentURL(frame1_2), blank_url); |
| 86 | |
| 87 | // top -> srcdoc [sandboxed] = same URL |
| 88 | EXPECT_EQ(GetEffectiveDocumentURL(frame2), srcdoc_url); |
| 89 | // top -> srcdoc [sandboxed] -> about:blank = same URL |
| 90 | EXPECT_EQ(GetEffectiveDocumentURL(frame2_1), blank_url); |
| 91 | |
| 92 | // top -> different origin = different origin |
| 93 | EXPECT_EQ(GetEffectiveDocumentURL(frame3), different_url); |
| 94 | // top -> different origin -> about:blank = inherit |
| 95 | EXPECT_EQ(GetEffectiveDocumentURL(frame3_1), different_url); |
| 96 | } |
| 97 | |
Jeremy Roman | 1a4d9b8 | 2017-12-08 01:46:40 | [diff] [blame] | 98 | TEST_F(ScriptContextTest, GetMainWorldContextForFrame) { |
| 99 | // ScriptContextSet::GetMainWorldContextForFrame should work, even without an |
| 100 | // existing v8::HandleScope. |
| 101 | content::RenderFrame* render_frame = |
| 102 | content::RenderFrame::FromWebFrame(GetMainFrame()); |
| 103 | ScriptContext* script_context = |
| 104 | ScriptContextSet::GetMainWorldContextForFrame(render_frame); |
| 105 | ASSERT_TRUE(script_context); |
| 106 | EXPECT_EQ(render_frame, script_context->GetRenderFrame()); |
| 107 | } |
| 108 | |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 109 | } // namespace |
| 110 | } // namespace extensions |