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