[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" |
| 9 | #include "third_party/WebKit/public/web/WebDocument.h" |
| 10 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 11 | #include "url/gurl.h" |
| 12 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 13 | using blink::WebLocalFrame; |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 14 | |
| 15 | namespace extensions { |
| 16 | namespace { |
| 17 | |
vivek.vg | d155cb80 | 2014-12-22 14:37:10 | [diff] [blame] | 18 | class ScriptContextTest : public ChromeRenderViewTest { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 19 | protected: |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 20 | GURL GetEffectiveDocumentURL(const WebLocalFrame* frame) { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 21 | return ScriptContext::GetEffectiveDocumentURL( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 22 | frame, frame->GetDocument().Url(), true); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 23 | } |
| 24 | }; |
| 25 | |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 26 | TEST_F(ScriptContextTest, GetEffectiveDocumentURL) { |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 27 | GURL top_url("http://example.com/"); |
| 28 | GURL different_url("http://example.net/"); |
| 29 | GURL blank_url("about:blank"); |
| 30 | GURL srcdoc_url("about:srcdoc"); |
| 31 | |
| 32 | const char frame_html[] = |
| 33 | "<iframe name='frame1' srcdoc=\"" |
| 34 | " <iframe name='frame1_1'></iframe>" |
| 35 | " <iframe name='frame1_2' sandbox=''></iframe>" |
| 36 | "\"></iframe>" |
| 37 | "<iframe name='frame2' sandbox='' srcdoc=\"" |
| 38 | " <iframe name='frame2_1'></iframe>" |
| 39 | "\"></iframe>" |
| 40 | "<iframe name='frame3'></iframe>"; |
| 41 | |
| 42 | const char frame3_html[] = "<iframe name='frame3_1'></iframe>"; |
| 43 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 44 | WebLocalFrame* frame = GetMainFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 45 | ASSERT_TRUE(frame); |
| 46 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 47 | frame->LoadHTMLString(frame_html, top_url); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 48 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 49 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 50 | WebLocalFrame* frame1 = frame->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 51 | ASSERT_TRUE(frame1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 52 | ASSERT_EQ("frame1", frame1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 53 | WebLocalFrame* frame1_1 = frame1->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 54 | ASSERT_TRUE(frame1_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 55 | ASSERT_EQ("frame1_1", frame1_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 56 | WebLocalFrame* frame1_2 = frame1_1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 57 | ASSERT_TRUE(frame1_2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 58 | ASSERT_EQ("frame1_2", frame1_2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 59 | WebLocalFrame* frame2 = frame1->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 60 | ASSERT_TRUE(frame2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 61 | ASSERT_EQ("frame2", frame2->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 62 | WebLocalFrame* frame2_1 = frame2->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 63 | ASSERT_TRUE(frame2_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 64 | ASSERT_EQ("frame2_1", frame2_1->AssignedName()); |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 65 | WebLocalFrame* frame3 = frame2->NextSibling()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 66 | ASSERT_TRUE(frame3); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 67 | ASSERT_EQ("frame3", frame3->AssignedName()); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 68 | |
| 69 | // Load a blank document in a frame from a different origin. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 70 | frame3->LoadHTMLString(frame3_html, different_url); |
mek | cbb4b86 | 2014-09-05 03:08:22 | [diff] [blame] | 71 | content::FrameLoadWaiter(content::RenderFrame::FromWebFrame(frame3)).Wait(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 72 | |
Daniel Cheng | 971cd452 | 2017-05-31 21:58:22 | [diff] [blame^] | 73 | WebLocalFrame* frame3_1 = frame3->FirstChild()->ToWebLocalFrame(); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 74 | ASSERT_TRUE(frame3_1); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 75 | ASSERT_EQ("frame3_1", frame3_1->AssignedName()); |
[email protected] | ae26b28 | 2014-05-15 16:40:16 | [diff] [blame] | 76 | |
| 77 | // Top-level frame |
| 78 | EXPECT_EQ(GetEffectiveDocumentURL(frame), top_url); |
| 79 | // top -> srcdoc = inherit |
| 80 | EXPECT_EQ(GetEffectiveDocumentURL(frame1), top_url); |
| 81 | // top -> srcdoc -> about:blank = inherit |
| 82 | EXPECT_EQ(GetEffectiveDocumentURL(frame1_1), top_url); |
| 83 | // top -> srcdoc -> about:blank sandboxed = same URL |
| 84 | EXPECT_EQ(GetEffectiveDocumentURL(frame1_2), blank_url); |
| 85 | |
| 86 | // top -> srcdoc [sandboxed] = same URL |
| 87 | EXPECT_EQ(GetEffectiveDocumentURL(frame2), srcdoc_url); |
| 88 | // top -> srcdoc [sandboxed] -> about:blank = same URL |
| 89 | EXPECT_EQ(GetEffectiveDocumentURL(frame2_1), blank_url); |
| 90 | |
| 91 | // top -> different origin = different origin |
| 92 | EXPECT_EQ(GetEffectiveDocumentURL(frame3), different_url); |
| 93 | // top -> different origin -> about:blank = inherit |
| 94 | EXPECT_EQ(GetEffectiveDocumentURL(frame3_1), different_url); |
| 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | } // namespace extensions |