| OLD | NEW |
| 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 "chrome/browser/extensions/extension_user_script_loader.h" | 5 #include "chrome/browser/extensions/user_script_loader.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 21 #include "extensions/common/host_id.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 22 |
| 24 using content::BrowserThread; | 23 using content::BrowserThread; |
| 25 using extensions::URLPatternSet; | 24 using extensions::URLPatternSet; |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 28 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
| 30 int schemes = URLPattern::SCHEME_ALL; | 29 int schemes = URLPattern::SCHEME_ALL; |
| 31 extent->AddPattern(URLPattern(schemes, pattern)); | 30 extent->AddPattern(URLPattern(schemes, pattern)); |
| 32 } | 31 } |
| 33 } | 32 } |
| 34 | 33 |
| 35 namespace extensions { | 34 namespace extensions { |
| 36 | 35 |
| 37 // Test bringing up a script loader on a specific directory, putting a script | 36 // Test bringing up a script loader on a specific directory, putting a script |
| 38 // in there, etc. | 37 // in there, etc. |
| 39 | 38 |
| 40 class ExtensionUserScriptLoaderTest : public testing::Test, | 39 class UserScriptLoaderTest : public testing::Test, |
| 41 public content::NotificationObserver { | 40 public content::NotificationObserver { |
| 42 public: | 41 public: |
| 43 ExtensionUserScriptLoaderTest() : shared_memory_(NULL) {} | 42 UserScriptLoaderTest() : shared_memory_(NULL) {} |
| 44 | 43 |
| 45 void SetUp() override { | 44 void SetUp() override { |
| 46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 47 | 46 |
| 48 // Register for all user script notifications. | 47 // Register for all user script notifications. |
| 49 registrar_.Add(this, | 48 registrar_.Add(this, |
| 50 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, | 49 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, |
| 51 content::NotificationService::AllSources()); | 50 content::NotificationService::AllSources()); |
| 52 | 51 |
| 53 // ExtensionUserScriptLoader posts tasks to the file thread so make the | 52 // UserScriptLoader posts tasks to the file thread so make the current |
| 54 // current thread look like one. | 53 // thread look like one. |
| 55 file_thread_.reset(new content::TestBrowserThread( | 54 file_thread_.reset(new content::TestBrowserThread( |
| 56 BrowserThread::FILE, base::MessageLoop::current())); | 55 BrowserThread::FILE, base::MessageLoop::current())); |
| 57 ui_thread_.reset(new content::TestBrowserThread( | 56 ui_thread_.reset(new content::TestBrowserThread( |
| 58 BrowserThread::UI, base::MessageLoop::current())); | 57 BrowserThread::UI, base::MessageLoop::current())); |
| 59 } | 58 } |
| 60 | 59 |
| 61 void TearDown() override { | 60 void TearDown() override { |
| 62 file_thread_.reset(); | 61 file_thread_.reset(); |
| 63 ui_thread_.reset(); | 62 ui_thread_.reset(); |
| 64 } | 63 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 82 base::MessageLoopForUI message_loop_; | 81 base::MessageLoopForUI message_loop_; |
| 83 | 82 |
| 84 scoped_ptr<content::TestBrowserThread> file_thread_; | 83 scoped_ptr<content::TestBrowserThread> file_thread_; |
| 85 scoped_ptr<content::TestBrowserThread> ui_thread_; | 84 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 86 | 85 |
| 87 // Updated to the script shared memory when we get notified. | 86 // Updated to the script shared memory when we get notified. |
| 88 base::SharedMemory* shared_memory_; | 87 base::SharedMemory* shared_memory_; |
| 89 }; | 88 }; |
| 90 | 89 |
| 91 // Test that we get notified even when there are no scripts. | 90 // Test that we get notified even when there are no scripts. |
| 92 TEST_F(ExtensionUserScriptLoaderTest, NoScripts) { | 91 TEST_F(UserScriptLoaderTest, NoScripts) { |
| 93 TestingProfile profile; | 92 TestingProfile profile; |
| 94 ExtensionUserScriptLoader loader( | 93 UserScriptLoader loader(&profile, |
| 95 &profile, | 94 std::string() /* owner_extension_id */, |
| 96 HostID(), | 95 true /* listen_for_extension_system_loaded */); |
| 97 true /* listen_for_extension_system_loaded */); | |
| 98 loader.StartLoad(); | 96 loader.StartLoad(); |
| 99 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | 97 message_loop_.PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
| 100 message_loop_.Run(); | 98 message_loop_.Run(); |
| 101 | 99 |
| 102 ASSERT_TRUE(shared_memory_ != NULL); | 100 ASSERT_TRUE(shared_memory_ != NULL); |
| 103 } | 101 } |
| 104 | 102 |
| 105 TEST_F(ExtensionUserScriptLoaderTest, Parse1) { | 103 TEST_F(UserScriptLoaderTest, Parse1) { |
| 106 const std::string text( | 104 const std::string text( |
| 107 "// This is my awesome script\n" | 105 "// This is my awesome script\n" |
| 108 "// It does stuff.\n" | 106 "// It does stuff.\n" |
| 109 "// ==UserScript== trailing garbage\n" | 107 "// ==UserScript== trailing garbage\n" |
| 110 "// @name foobar script\n" | 108 "// @name foobar script\n" |
| 111 "// @namespace http://www.google.com/\n" | 109 "// @namespace http://www.google.com/\n" |
| 112 "// @include *mail.google.com*\n" | 110 "// @include *mail.google.com*\n" |
| 113 "// \n" | 111 "// \n" |
| 114 "// @othergarbage\n" | 112 "// @othergarbage\n" |
| 115 "// @include *mail.yahoo.com*\r\n" | 113 "// @include *mail.yahoo.com*\r\n" |
| 116 "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK | 114 "// @include \t *mail.msn.com*\n" // extra spaces after "@include" OK |
| 117 "//@include not-recognized\n" // must have one space after "//" | 115 "//@include not-recognized\n" // must have one space after "//" |
| 118 "// ==/UserScript== trailing garbage\n" | 116 "// ==/UserScript== trailing garbage\n" |
| 119 "\n" | 117 "\n" |
| 120 "\n" | 118 "\n" |
| 121 "alert('hoo!');\n"); | 119 "alert('hoo!');\n"); |
| 122 | 120 |
| 123 UserScript script; | 121 UserScript script; |
| 124 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); | 122 EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script)); |
| 125 ASSERT_EQ(3U, script.globs().size()); | 123 ASSERT_EQ(3U, script.globs().size()); |
| 126 EXPECT_EQ("*mail.google.com*", script.globs()[0]); | 124 EXPECT_EQ("*mail.google.com*", script.globs()[0]); |
| 127 EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]); | 125 EXPECT_EQ("*mail.yahoo.com*", script.globs()[1]); |
| 128 EXPECT_EQ("*mail.msn.com*", script.globs()[2]); | 126 EXPECT_EQ("*mail.msn.com*", script.globs()[2]); |
| 129 } | 127 } |
| 130 | 128 |
| 131 TEST_F(ExtensionUserScriptLoaderTest, Parse2) { | 129 TEST_F(UserScriptLoaderTest, Parse2) { |
| 132 const std::string text("default to @include *"); | 130 const std::string text("default to @include *"); |
| 133 | 131 |
| 134 UserScript script; | 132 UserScript script; |
| 135 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); | 133 EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script)); |
| 136 ASSERT_EQ(1U, script.globs().size()); | 134 ASSERT_EQ(1U, script.globs().size()); |
| 137 EXPECT_EQ("*", script.globs()[0]); | 135 EXPECT_EQ("*", script.globs()[0]); |
| 138 } | 136 } |
| 139 | 137 |
| 140 TEST_F(ExtensionUserScriptLoaderTest, Parse3) { | 138 TEST_F(UserScriptLoaderTest, Parse3) { |
| 141 const std::string text( | 139 const std::string text( |
| 142 "// ==UserScript==\n" | 140 "// ==UserScript==\n" |
| 143 "// @include *foo*\n" | 141 "// @include *foo*\n" |
| 144 "// ==/UserScript=="); // no trailing newline | 142 "// ==/UserScript=="); // no trailing newline |
| 145 | 143 |
| 146 UserScript script; | 144 UserScript script; |
| 147 ExtensionUserScriptLoader::ParseMetadataHeader(text, &script); | 145 UserScriptLoader::ParseMetadataHeader(text, &script); |
| 148 ASSERT_EQ(1U, script.globs().size()); | 146 ASSERT_EQ(1U, script.globs().size()); |
| 149 EXPECT_EQ("*foo*", script.globs()[0]); | 147 EXPECT_EQ("*foo*", script.globs()[0]); |
| 150 } | 148 } |
| 151 | 149 |
| 152 TEST_F(ExtensionUserScriptLoaderTest, Parse4) { | 150 TEST_F(UserScriptLoaderTest, Parse4) { |
| 153 const std::string text( | 151 const std::string text( |
| 154 "// ==UserScript==\n" | 152 "// ==UserScript==\n" |
| 155 "// @match http://*.mail.google.com/*\n" | 153 "// @match http://*.mail.google.com/*\n" |
| 156 "// @match \t http://mail.yahoo.com/*\n" | 154 "// @match \t http://mail.yahoo.com/*\n" |
| 157 "// ==/UserScript==\n"); | 155 "// ==/UserScript==\n"); |
| 158 | 156 |
| 159 URLPatternSet expected_patterns; | 157 URLPatternSet expected_patterns; |
| 160 AddPattern(&expected_patterns, "http://*.mail.google.com/*"); | 158 AddPattern(&expected_patterns, "http://*.mail.google.com/*"); |
| 161 AddPattern(&expected_patterns, "http://mail.yahoo.com/*"); | 159 AddPattern(&expected_patterns, "http://mail.yahoo.com/*"); |
| 162 | 160 |
| 163 UserScript script; | 161 UserScript script; |
| 164 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); | 162 EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script)); |
| 165 EXPECT_EQ(0U, script.globs().size()); | 163 EXPECT_EQ(0U, script.globs().size()); |
| 166 EXPECT_EQ(expected_patterns, script.url_patterns()); | 164 EXPECT_EQ(expected_patterns, script.url_patterns()); |
| 167 } | 165 } |
| 168 | 166 |
| 169 TEST_F(ExtensionUserScriptLoaderTest, Parse5) { | 167 TEST_F(UserScriptLoaderTest, Parse5) { |
| 170 const std::string text( | 168 const std::string text( |
| 171 "// ==UserScript==\n" | 169 "// ==UserScript==\n" |
| 172 "// @match http://*mail.google.com/*\n" | 170 "// @match http://*mail.google.com/*\n" |
| 173 "// ==/UserScript==\n"); | 171 "// ==/UserScript==\n"); |
| 174 | 172 |
| 175 // Invalid @match value. | 173 // Invalid @match value. |
| 176 UserScript script; | 174 UserScript script; |
| 177 EXPECT_FALSE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); | 175 EXPECT_FALSE(UserScriptLoader::ParseMetadataHeader(text, &script)); |
| 178 } | 176 } |
| 179 | 177 |
| 180 TEST_F(ExtensionUserScriptLoaderTest, Parse6) { | 178 TEST_F(UserScriptLoaderTest, Parse6) { |
| 181 const std::string text( | 179 const std::string text( |
| 182 "// ==UserScript==\n" | 180 "// ==UserScript==\n" |
| 183 "// @include http://*.mail.google.com/*\n" | 181 "// @include http://*.mail.google.com/*\n" |
| 184 "// @match \t http://mail.yahoo.com/*\n" | 182 "// @match \t http://mail.yahoo.com/*\n" |
| 185 "// ==/UserScript==\n"); | 183 "// ==/UserScript==\n"); |
| 186 | 184 |
| 187 // Allowed to match @include and @match. | 185 // Allowed to match @include and @match. |
| 188 UserScript script; | 186 UserScript script; |
| 189 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); | 187 EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script)); |
| 190 } | 188 } |
| 191 | 189 |
| 192 TEST_F(ExtensionUserScriptLoaderTest, Parse7) { | 190 TEST_F(UserScriptLoaderTest, Parse7) { |
| 193 // Greasemonkey allows there to be any leading text before the comment marker. | 191 // Greasemonkey allows there to be any leading text before the comment marker. |
| 194 const std::string text( | 192 const std::string text( |
| 195 "// ==UserScript==\n" | 193 "// ==UserScript==\n" |
| 196 "adsasdfasf// @name hello\n" | 194 "adsasdfasf// @name hello\n" |
| 197 " // @description\twiggity woo\n" | 195 " // @description\twiggity woo\n" |
| 198 "\t// @match \t http://mail.yahoo.com/*\n" | 196 "\t// @match \t http://mail.yahoo.com/*\n" |
| 199 "// ==/UserScript==\n"); | 197 "// ==/UserScript==\n"); |
| 200 | 198 |
| 201 UserScript script; | 199 UserScript script; |
| 202 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); | 200 EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script)); |
| 203 ASSERT_EQ("hello", script.name()); | 201 ASSERT_EQ("hello", script.name()); |
| 204 ASSERT_EQ("wiggity woo", script.description()); | 202 ASSERT_EQ("wiggity woo", script.description()); |
| 205 ASSERT_EQ(1U, script.url_patterns().patterns().size()); | 203 ASSERT_EQ(1U, script.url_patterns().patterns().size()); |
| 206 EXPECT_EQ("http://mail.yahoo.com/*", | 204 EXPECT_EQ("http://mail.yahoo.com/*", |
| 207 script.url_patterns().begin()->GetAsString()); | 205 script.url_patterns().begin()->GetAsString()); |
| 208 } | 206 } |
| 209 | 207 |
| 210 TEST_F(ExtensionUserScriptLoaderTest, Parse8) { | 208 TEST_F(UserScriptLoaderTest, Parse8) { |
| 211 const std::string text( | 209 const std::string text( |
| 212 "// ==UserScript==\n" | 210 "// ==UserScript==\n" |
| 213 "// @name myscript\n" | 211 "// @name myscript\n" |
| 214 "// @match http://www.google.com/*\n" | 212 "// @match http://www.google.com/*\n" |
| 215 "// @exclude_match http://www.google.com/foo*\n" | 213 "// @exclude_match http://www.google.com/foo*\n" |
| 216 "// ==/UserScript==\n"); | 214 "// ==/UserScript==\n"); |
| 217 | 215 |
| 218 UserScript script; | 216 UserScript script; |
| 219 EXPECT_TRUE(ExtensionUserScriptLoader::ParseMetadataHeader(text, &script)); | 217 EXPECT_TRUE(UserScriptLoader::ParseMetadataHeader(text, &script)); |
| 220 ASSERT_EQ("myscript", script.name()); | 218 ASSERT_EQ("myscript", script.name()); |
| 221 ASSERT_EQ(1U, script.url_patterns().patterns().size()); | 219 ASSERT_EQ(1U, script.url_patterns().patterns().size()); |
| 222 EXPECT_EQ("http://www.google.com/*", | 220 EXPECT_EQ("http://www.google.com/*", |
| 223 script.url_patterns().begin()->GetAsString()); | 221 script.url_patterns().begin()->GetAsString()); |
| 224 ASSERT_EQ(1U, script.exclude_url_patterns().patterns().size()); | 222 ASSERT_EQ(1U, script.exclude_url_patterns().patterns().size()); |
| 225 EXPECT_EQ("http://www.google.com/foo*", | 223 EXPECT_EQ("http://www.google.com/foo*", |
| 226 script.exclude_url_patterns().begin()->GetAsString()); | 224 script.exclude_url_patterns().begin()->GetAsString()); |
| 227 } | 225 } |
| 228 | 226 |
| 229 TEST_F(ExtensionUserScriptLoaderTest, SkipBOMAtTheBeginning) { | 227 TEST_F(UserScriptLoaderTest, SkipBOMAtTheBeginning) { |
| 230 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); | 228 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
| 231 const std::string content("\xEF\xBB\xBF alert('hello');"); | 229 const std::string content("\xEF\xBB\xBF alert('hello');"); |
| 232 size_t written = base::WriteFile(path, content.c_str(), content.size()); | 230 size_t written = base::WriteFile(path, content.c_str(), content.size()); |
| 233 ASSERT_EQ(written, content.size()); | 231 ASSERT_EQ(written, content.size()); |
| 234 | 232 |
| 235 UserScript user_script; | 233 UserScript user_script; |
| 236 user_script.js_scripts().push_back( | 234 user_script.js_scripts().push_back( |
| 237 UserScript::File(temp_dir_.path(), path.BaseName(), GURL())); | 235 UserScript::File(temp_dir_.path(), path.BaseName(), GURL())); |
| 238 | 236 |
| 239 UserScriptList user_scripts; | 237 UserScriptList user_scripts; |
| 240 user_scripts.push_back(user_script); | 238 user_scripts.push_back(user_script); |
| 241 | 239 |
| 242 TestingProfile profile; | 240 UserScriptLoader::LoadScriptsForTest(&user_scripts); |
| 243 ExtensionUserScriptLoader loader( | |
| 244 &profile, | |
| 245 HostID(), | |
| 246 true /* listen_for_extension_system_loaded */); | |
| 247 loader.LoadScriptsForTest(&user_scripts); | |
| 248 | 241 |
| 249 EXPECT_EQ(content.substr(3), | 242 EXPECT_EQ(content.substr(3), |
| 250 user_scripts[0].js_scripts()[0].GetContent().as_string()); | 243 user_scripts[0].js_scripts()[0].GetContent().as_string()); |
| 251 } | 244 } |
| 252 | 245 |
| 253 TEST_F(ExtensionUserScriptLoaderTest, LeaveBOMNotAtTheBeginning) { | 246 TEST_F(UserScriptLoaderTest, LeaveBOMNotAtTheBeginning) { |
| 254 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); | 247 base::FilePath path = temp_dir_.path().AppendASCII("script.user.js"); |
| 255 const std::string content("alert('here's a BOOM: \xEF\xBB\xBF');"); | 248 const std::string content("alert('here's a BOOM: \xEF\xBB\xBF');"); |
| 256 size_t written = base::WriteFile(path, content.c_str(), content.size()); | 249 size_t written = base::WriteFile(path, content.c_str(), content.size()); |
| 257 ASSERT_EQ(written, content.size()); | 250 ASSERT_EQ(written, content.size()); |
| 258 | 251 |
| 259 UserScript user_script; | 252 UserScript user_script; |
| 260 user_script.js_scripts().push_back(UserScript::File( | 253 user_script.js_scripts().push_back(UserScript::File( |
| 261 temp_dir_.path(), path.BaseName(), GURL())); | 254 temp_dir_.path(), path.BaseName(), GURL())); |
| 262 | 255 |
| 263 UserScriptList user_scripts; | 256 UserScriptList user_scripts; |
| 264 user_scripts.push_back(user_script); | 257 user_scripts.push_back(user_script); |
| 265 | 258 |
| 266 TestingProfile profile; | 259 UserScriptLoader::LoadScriptsForTest(&user_scripts); |
| 267 ExtensionUserScriptLoader loader( | |
| 268 &profile, | |
| 269 HostID(), | |
| 270 true /* listen_for_extension_system_loaded */); | |
| 271 loader.LoadScriptsForTest(&user_scripts); | |
| 272 | 260 |
| 273 EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string()); | 261 EXPECT_EQ(content, user_scripts[0].js_scripts()[0].GetContent().as_string()); |
| 274 } | 262 } |
| 275 | 263 |
| 276 } // namespace extensions | 264 } // namespace extensions |
| OLD | NEW |