| 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/extension_context_menu_model.h" | 5 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/app/chrome_command_ids.h" | 12 #include "chrome/app/chrome_command_ids.h" |
| 12 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 13 #include "chrome/browser/extensions/extension_action_runner.h" | 14 #include "chrome/browser/extensions/extension_action_runner.h" |
| 14 #include "chrome/browser/extensions/extension_action_test_util.h" | 15 #include "chrome/browser/extensions/extension_action_test_util.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_service_test_base.h" | 17 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 17 #include "chrome/browser/extensions/menu_manager.h" | 18 #include "chrome/browser/extensions/menu_manager.h" |
| 18 #include "chrome/browser/extensions/menu_manager_factory.h" | 19 #include "chrome/browser/extensions/menu_manager_factory.h" |
| 19 #include "chrome/browser/extensions/scripting_permissions_modifier.h" | 20 #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 public: | 60 public: |
| 60 MenuBuilder(scoped_refptr<const Extension> extension, | 61 MenuBuilder(scoped_refptr<const Extension> extension, |
| 61 Browser* browser, | 62 Browser* browser, |
| 62 MenuManager* menu_manager) | 63 MenuManager* menu_manager) |
| 63 : extension_(extension), | 64 : extension_(extension), |
| 64 browser_(browser), | 65 browser_(browser), |
| 65 menu_manager_(menu_manager), | 66 menu_manager_(menu_manager), |
| 66 cur_id_(0) {} | 67 cur_id_(0) {} |
| 67 ~MenuBuilder() {} | 68 ~MenuBuilder() {} |
| 68 | 69 |
| 69 scoped_ptr<ExtensionContextMenuModel> BuildMenu() { | 70 std::unique_ptr<ExtensionContextMenuModel> BuildMenu() { |
| 70 return make_scoped_ptr(new ExtensionContextMenuModel( | 71 return base::WrapUnique(new ExtensionContextMenuModel( |
| 71 extension_.get(), browser_, ExtensionContextMenuModel::VISIBLE, | 72 extension_.get(), browser_, ExtensionContextMenuModel::VISIBLE, |
| 72 nullptr)); | 73 nullptr)); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void AddContextItem(MenuItem::Context context) { | 76 void AddContextItem(MenuItem::Context context) { |
| 76 MenuItem::Id id(false /* not incognito */, | 77 MenuItem::Id id(false /* not incognito */, |
| 77 MenuItem::ExtensionKey(extension_->id())); | 78 MenuItem::ExtensionKey(extension_->id())); |
| 78 id.uid = ++cur_id_; | 79 id.uid = ++cur_id_; |
| 79 menu_manager_->AddContextItem( | 80 menu_manager_->AddContextItem( |
| 80 extension_.get(), | 81 extension_.get(), |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 Manifest::Location location); | 128 Manifest::Location location); |
| 128 const Extension* AddExtensionWithHostPermission( | 129 const Extension* AddExtensionWithHostPermission( |
| 129 const std::string& name, | 130 const std::string& name, |
| 130 const char* action_key, | 131 const char* action_key, |
| 131 Manifest::Location location, | 132 Manifest::Location location, |
| 132 const std::string& host_permission); | 133 const std::string& host_permission); |
| 133 | 134 |
| 134 Browser* GetBrowser(); | 135 Browser* GetBrowser(); |
| 135 | 136 |
| 136 private: | 137 private: |
| 137 scoped_ptr<TestBrowserWindow> test_window_; | 138 std::unique_ptr<TestBrowserWindow> test_window_; |
| 138 scoped_ptr<Browser> browser_; | 139 std::unique_ptr<Browser> browser_; |
| 139 | 140 |
| 140 DISALLOW_COPY_AND_ASSIGN(ExtensionContextMenuModelTest); | 141 DISALLOW_COPY_AND_ASSIGN(ExtensionContextMenuModelTest); |
| 141 }; | 142 }; |
| 142 | 143 |
| 143 ExtensionContextMenuModelTest::ExtensionContextMenuModelTest() {} | 144 ExtensionContextMenuModelTest::ExtensionContextMenuModelTest() {} |
| 144 | 145 |
| 145 const Extension* ExtensionContextMenuModelTest::AddExtension( | 146 const Extension* ExtensionContextMenuModelTest::AddExtension( |
| 146 const std::string& name, | 147 const std::string& name, |
| 147 const char* action_key, | 148 const char* action_key, |
| 148 Manifest::Location location) { | 149 Manifest::Location location) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 221 |
| 221 // Don't leave |policy_provider| dangling. | 222 // Don't leave |policy_provider| dangling. |
| 222 system->management_policy()->UnregisterProvider(&policy_provider); | 223 system->management_policy()->UnregisterProvider(&policy_provider); |
| 223 } | 224 } |
| 224 | 225 |
| 225 // Tests the context menu for a component extension. | 226 // Tests the context menu for a component extension. |
| 226 TEST_F(ExtensionContextMenuModelTest, ComponentExtensionContextMenu) { | 227 TEST_F(ExtensionContextMenuModelTest, ComponentExtensionContextMenu) { |
| 227 InitializeEmptyExtensionService(); | 228 InitializeEmptyExtensionService(); |
| 228 | 229 |
| 229 std::string name("component"); | 230 std::string name("component"); |
| 230 scoped_ptr<base::DictionaryValue> manifest = | 231 std::unique_ptr<base::DictionaryValue> manifest = |
| 231 DictionaryBuilder() | 232 DictionaryBuilder() |
| 232 .Set("name", name) | 233 .Set("name", name) |
| 233 .Set("version", "1") | 234 .Set("version", "1") |
| 234 .Set("manifest_version", 2) | 235 .Set("manifest_version", 2) |
| 235 .Set("browser_action", DictionaryBuilder().Build()) | 236 .Set("browser_action", DictionaryBuilder().Build()) |
| 236 .Build(); | 237 .Build(); |
| 237 | 238 |
| 238 { | 239 { |
| 239 scoped_refptr<const Extension> extension = | 240 scoped_refptr<const Extension> extension = |
| 240 ExtensionBuilder() | 241 ExtensionBuilder() |
| 241 .SetManifest(make_scoped_ptr(manifest->DeepCopy())) | 242 .SetManifest(base::WrapUnique(manifest->DeepCopy())) |
| 242 .SetID(crx_file::id_util::GenerateId("component")) | 243 .SetID(crx_file::id_util::GenerateId("component")) |
| 243 .SetLocation(Manifest::COMPONENT) | 244 .SetLocation(Manifest::COMPONENT) |
| 244 .Build(); | 245 .Build(); |
| 245 service()->AddExtension(extension.get()); | 246 service()->AddExtension(extension.get()); |
| 246 | 247 |
| 247 ExtensionContextMenuModel menu(extension.get(), GetBrowser(), | 248 ExtensionContextMenuModel menu(extension.get(), GetBrowser(), |
| 248 ExtensionContextMenuModel::VISIBLE, nullptr); | 249 ExtensionContextMenuModel::VISIBLE, nullptr); |
| 249 | 250 |
| 250 // A component extension's context menu should not include options for | 251 // A component extension's context menu should not include options for |
| 251 // managing extensions or removing it, and should only include an option for | 252 // managing extensions or removing it, and should only include an option for |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 | 318 |
| 318 // We shouldn't go above the limit of top-level items. | 319 // We shouldn't go above the limit of top-level items. |
| 319 EXPECT_EQ(api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT, | 320 EXPECT_EQ(api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT, |
| 320 CountExtensionItems(*builder.BuildMenu())); | 321 CountExtensionItems(*builder.BuildMenu())); |
| 321 } | 322 } |
| 322 | 323 |
| 323 // Test that the "show" and "hide" menu items appear correctly in the extension | 324 // Test that the "show" and "hide" menu items appear correctly in the extension |
| 324 // context menu without the toolbar redesign. | 325 // context menu without the toolbar redesign. |
| 325 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHideLegacy) { | 326 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHideLegacy) { |
| 326 // Start with the toolbar redesign disabled. | 327 // Start with the toolbar redesign disabled. |
| 327 scoped_ptr<FeatureSwitch::ScopedOverride> toolbar_redesign_override( | 328 std::unique_ptr<FeatureSwitch::ScopedOverride> toolbar_redesign_override( |
| 328 new FeatureSwitch::ScopedOverride( | 329 new FeatureSwitch::ScopedOverride( |
| 329 FeatureSwitch::extension_action_redesign(), false)); | 330 FeatureSwitch::extension_action_redesign(), false)); |
| 330 | 331 |
| 331 InitializeEmptyExtensionService(); | 332 InitializeEmptyExtensionService(); |
| 332 Browser* browser = GetBrowser(); | 333 Browser* browser = GetBrowser(); |
| 333 extension_action_test_util::CreateToolbarModelForProfile(profile()); | 334 extension_action_test_util::CreateToolbarModelForProfile(profile()); |
| 334 | 335 |
| 335 const Extension* page_action = AddExtension( | 336 const Extension* page_action = AddExtension( |
| 336 "page_action_extension", manifest_keys::kPageAction, Manifest::INTERNAL); | 337 "page_action_extension", manifest_keys::kPageAction, Manifest::INTERNAL); |
| 337 const Extension* browser_action = | 338 const Extension* browser_action = |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 | 385 |
| 385 // Cleanup - make the action visible again. | 386 // Cleanup - make the action visible again. |
| 386 action_api->SetBrowserActionVisibility(browser_action->id(), true); | 387 action_api->SetBrowserActionVisibility(browser_action->id(), true); |
| 387 } | 388 } |
| 388 } | 389 } |
| 389 | 390 |
| 390 // Test that the "show" and "hide" menu items appear correctly in the extension | 391 // Test that the "show" and "hide" menu items appear correctly in the extension |
| 391 // context menu with the toolbar redesign. | 392 // context menu with the toolbar redesign. |
| 392 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHideRedesign) { | 393 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHideRedesign) { |
| 393 // Start with the toolbar redesign disabled. | 394 // Start with the toolbar redesign disabled. |
| 394 scoped_ptr<FeatureSwitch::ScopedOverride> toolbar_redesign_override( | 395 std::unique_ptr<FeatureSwitch::ScopedOverride> toolbar_redesign_override( |
| 395 new FeatureSwitch::ScopedOverride( | 396 new FeatureSwitch::ScopedOverride( |
| 396 FeatureSwitch::extension_action_redesign(), true)); | 397 FeatureSwitch::extension_action_redesign(), true)); |
| 397 | 398 |
| 398 InitializeEmptyExtensionService(); | 399 InitializeEmptyExtensionService(); |
| 399 Browser* browser = GetBrowser(); | 400 Browser* browser = GetBrowser(); |
| 400 extension_action_test_util::CreateToolbarModelForProfile(profile()); | 401 extension_action_test_util::CreateToolbarModelForProfile(profile()); |
| 401 const Extension* page_action = | 402 const Extension* page_action = |
| 402 AddExtension("page_action_extension", | 403 AddExtension("page_action_extension", |
| 403 manifest_keys::kPageAction, | 404 manifest_keys::kPageAction, |
| 404 Manifest::INTERNAL); | 405 Manifest::INTERNAL); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 ExtensionContextMenuModel::VISIBLE, nullptr); | 483 ExtensionContextMenuModel::VISIBLE, nullptr); |
| 483 menu.ExecuteCommand(ExtensionContextMenuModel::UNINSTALL, 0); | 484 menu.ExecuteCommand(ExtensionContextMenuModel::UNINSTALL, 0); |
| 484 } | 485 } |
| 485 uninstalled_observer.WaitForExtensionUninstalled(); | 486 uninstalled_observer.WaitForExtensionUninstalled(); |
| 486 EXPECT_FALSE(registry()->GetExtensionById(extension_id, | 487 EXPECT_FALSE(registry()->GetExtensionById(extension_id, |
| 487 ExtensionRegistry::EVERYTHING)); | 488 ExtensionRegistry::EVERYTHING)); |
| 488 } | 489 } |
| 489 | 490 |
| 490 TEST_F(ExtensionContextMenuModelTest, TestPageAccessSubmenu) { | 491 TEST_F(ExtensionContextMenuModelTest, TestPageAccessSubmenu) { |
| 491 // This test relies on the click-to-script feature. | 492 // This test relies on the click-to-script feature. |
| 492 scoped_ptr<FeatureSwitch::ScopedOverride> enable_scripts_require_action( | 493 std::unique_ptr<FeatureSwitch::ScopedOverride> enable_scripts_require_action( |
| 493 new FeatureSwitch::ScopedOverride(FeatureSwitch::scripts_require_action(), | 494 new FeatureSwitch::ScopedOverride(FeatureSwitch::scripts_require_action(), |
| 494 true)); | 495 true)); |
| 495 InitializeEmptyExtensionService(); | 496 InitializeEmptyExtensionService(); |
| 496 | 497 |
| 497 // Add an extension with all urls. | 498 // Add an extension with all urls. |
| 498 const Extension* extension = | 499 const Extension* extension = |
| 499 AddExtensionWithHostPermission("extension", manifest_keys::kBrowserAction, | 500 AddExtensionWithHostPermission("extension", manifest_keys::kBrowserAction, |
| 500 Manifest::INTERNAL, "*://*/*"); | 501 Manifest::INTERNAL, "*://*/*"); |
| 501 | 502 |
| 502 const GURL kActiveUrl("http://www.example.com/"); | 503 const GURL kActiveUrl("http://www.example.com/"); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 "feature_disabled_extension", manifest_keys::kBrowserAction, | 651 "feature_disabled_extension", manifest_keys::kBrowserAction, |
| 651 Manifest::INTERNAL, "http://www.google.com/*"); | 652 Manifest::INTERNAL, "http://www.google.com/*"); |
| 652 ExtensionContextMenuModel feature_disabled_menu( | 653 ExtensionContextMenuModel feature_disabled_menu( |
| 653 feature_disabled_extension, GetBrowser(), | 654 feature_disabled_extension, GetBrowser(), |
| 654 ExtensionContextMenuModel::VISIBLE, nullptr); | 655 ExtensionContextMenuModel::VISIBLE, nullptr); |
| 655 EXPECT_EQ(-1, feature_disabled_menu.GetIndexOfCommandId( | 656 EXPECT_EQ(-1, feature_disabled_menu.GetIndexOfCommandId( |
| 656 ExtensionContextMenuModel::PAGE_ACCESS_SUBMENU)); | 657 ExtensionContextMenuModel::PAGE_ACCESS_SUBMENU)); |
| 657 } | 658 } |
| 658 | 659 |
| 659 } // namespace extensions | 660 } // namespace extensions |
| OLD | NEW |