| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/active_tab_permission_manager.h" |
| 12 #include "chrome/browser/extensions/extension_tab_helper.h" |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 14 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/notification_types.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/common/page_transition_types.h" |
| 20 #include "content/public/test/test_browser_thread.h" |
| 21 |
| 22 using base::DictionaryValue; |
| 23 using base::ListValue; |
| 24 using content::BrowserThread; |
| 25 using content::NavigationController; |
| 26 |
| 27 namespace extensions { |
| 28 namespace { |
| 29 |
| 30 class ActiveTabTest : public TabContentsWrapperTestHarness { |
| 31 public: |
| 32 ActiveTabTest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) { |
| 33 { |
| 34 scoped_ptr<ListValue> permissions(new ListValue()); |
| 35 permissions->Append(Value::CreateStringValue("activeTab")); |
| 36 DictionaryValue manifest; |
| 37 manifest.SetString("name", "Extension with activeTab."); |
| 38 manifest.SetString("version", "1.0.0"); |
| 39 manifest.SetInteger("manifest_version", 2); |
| 40 manifest.Set("permissions", permissions.release()); |
| 41 std::string error; |
| 42 extension_ = Extension::Create( |
| 43 FilePath(), |
| 44 Extension::EXTERNAL_PREF, |
| 45 manifest, |
| 46 0, |
| 47 &error); |
| 48 CHECK_EQ("", error); |
| 49 CHECK(extension_->HasAPIPermission(ExtensionAPIPermission::kActiveTab)); |
| 50 } |
| 51 { |
| 52 scoped_ptr<ListValue> permissions(new ListValue()); |
| 53 permissions->Append(Value::CreateStringValue("activeTab")); |
| 54 DictionaryValue manifest; |
| 55 manifest.SetString("name", "Another extension with activeTab."); |
| 56 manifest.SetString("version", "1.0.0"); |
| 57 manifest.SetInteger("manifest_version", 2); |
| 58 manifest.Set("permissions", permissions.release()); |
| 59 std::string error; |
| 60 another_extension_ = Extension::Create( |
| 61 FilePath(), |
| 62 Extension::EXTERNAL_PREF, |
| 63 manifest, |
| 64 0, |
| 65 &error); |
| 66 CHECK_EQ("", error); |
| 67 CHECK(another_extension_->HasAPIPermission( |
| 68 ExtensionAPIPermission::kActiveTab)); |
| 69 } |
| 70 { |
| 71 scoped_ptr<ListValue> permissions(new ListValue()); |
| 72 DictionaryValue manifest; |
| 73 manifest.SetString("name", "Extension without activeTab."); |
| 74 manifest.SetString("version", "1.0.0"); |
| 75 manifest.SetInteger("manifest_version", 2); |
| 76 manifest.Set("permissions", permissions.release()); |
| 77 std::string error; |
| 78 extension_without_active_tab_ = Extension::Create( |
| 79 FilePath(), |
| 80 Extension::EXTERNAL_PREF, |
| 81 manifest, |
| 82 0, |
| 83 &error); |
| 84 CHECK_EQ("", error); |
| 85 CHECK(!extension_without_active_tab_->HasAPIPermission( |
| 86 ExtensionAPIPermission::kActiveTab)); |
| 87 } |
| 88 } |
| 89 |
| 90 protected: |
| 91 const Extension* extension() { |
| 92 return extension_.get(); |
| 93 } |
| 94 |
| 95 const Extension* another_extension() { |
| 96 return another_extension_.get(); |
| 97 } |
| 98 |
| 99 const Extension* extension_without_active_tab() { |
| 100 return extension_without_active_tab_.get(); |
| 101 } |
| 102 |
| 103 int tab_id() { |
| 104 return contents_wrapper()->extension_tab_helper()->GetTabId(); |
| 105 } |
| 106 |
| 107 ActiveTabPermissionManager* manager() { |
| 108 return contents_wrapper()->extension_tab_helper()-> |
| 109 active_tab_permission_manager(); |
| 110 } |
| 111 |
| 112 bool Allowed(const Extension* extension, const GURL& url) { |
| 113 return Allowed(extension, url, tab_id()); |
| 114 } |
| 115 |
| 116 bool Allowed(const Extension* extension, const GURL& url, int tab_id) { |
| 117 return (extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) && |
| 118 extension->CanCaptureVisiblePage(url, tab_id, NULL)); |
| 119 } |
| 120 |
| 121 // Fakes loading a new frame on the page using the WebContentsObserver |
| 122 // interface. |
| 123 // TODO(kalman): if somebody can tell me a way to do this from the |
| 124 // TabContentsWrapperTestHarness (or any other test harness) then pray tell. |
| 125 void AddFrame(const GURL& url) { |
| 126 manager()->DidCommitProvisionalLoadForFrame( |
| 127 0, // frame_id |
| 128 false, // is_main_frame |
| 129 url, |
| 130 content::PAGE_TRANSITION_AUTO_SUBFRAME, |
| 131 NULL); // render_view_host |
| 132 } |
| 133 |
| 134 private: |
| 135 // An extension with the activeTab permission. |
| 136 scoped_refptr<const Extension> extension_; |
| 137 |
| 138 // Another extension with activeTab (for good measure). |
| 139 scoped_refptr<const Extension> another_extension_; |
| 140 |
| 141 // An extension without the activeTab permission. |
| 142 scoped_refptr<const Extension> extension_without_active_tab_; |
| 143 |
| 144 content::TestBrowserThread ui_thread_; |
| 145 }; |
| 146 |
| 147 // TODO: |
| 148 // - multiple frames on the same tab |
| 149 // - uninstalling extensions |
| 150 // - ... |
| 151 |
| 152 TEST_F(ActiveTabTest, GrantToSinglePage) { |
| 153 GURL google("http://www.google.com"); |
| 154 NavigateAndCommit(google); |
| 155 |
| 156 EXPECT_FALSE(Allowed(extension(), google)); |
| 157 EXPECT_FALSE(Allowed(another_extension(), google)); |
| 158 EXPECT_FALSE(Allowed(extension_without_active_tab(), google)); |
| 159 |
| 160 manager()->MaybeGrant(extension()); |
| 161 manager()->MaybeGrant(extension_without_active_tab()); |
| 162 |
| 163 EXPECT_TRUE(Allowed(extension(), google)); |
| 164 EXPECT_FALSE(Allowed(another_extension(), google)); |
| 165 EXPECT_FALSE(Allowed(extension_without_active_tab(), google)); |
| 166 |
| 167 GURL chromium("http://www.chromium.org"); |
| 168 rvh_tester()->SendNavigate(1, chromium); |
| 169 |
| 170 EXPECT_FALSE(Allowed(extension(), google)); |
| 171 EXPECT_FALSE(Allowed(another_extension(), google)); |
| 172 EXPECT_FALSE(Allowed(extension_without_active_tab(), google)); |
| 173 |
| 174 EXPECT_FALSE(Allowed(extension(), chromium)); |
| 175 EXPECT_FALSE(Allowed(another_extension(), chromium)); |
| 176 EXPECT_FALSE(Allowed(extension_without_active_tab(), chromium)); |
| 177 |
| 178 manager()->MaybeGrant(extension()); |
| 179 manager()->MaybeGrant(another_extension()); |
| 180 manager()->MaybeGrant(extension_without_active_tab()); |
| 181 |
| 182 EXPECT_FALSE(Allowed(extension(), google)); |
| 183 EXPECT_FALSE(Allowed(another_extension(), google)); |
| 184 EXPECT_FALSE(Allowed(extension_without_active_tab(), google)); |
| 185 |
| 186 EXPECT_TRUE(Allowed(extension(), chromium)); |
| 187 EXPECT_TRUE(Allowed(another_extension(), chromium)); |
| 188 EXPECT_FALSE(Allowed(extension_without_active_tab(), chromium)); |
| 189 }; |
| 190 |
| 191 } // namespace |
| 192 } // namespace extensions |
| OLD | NEW |