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