| 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 "chrome/browser/extensions/api/extension_action/extension_page_actions_
api.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" | |
| 11 #include "chrome/browser/extensions/extension_action.h" | |
| 12 #include "chrome/browser/extensions/extension_action_manager.h" | |
| 13 #include "chrome/browser/extensions/extension_service.h" | |
| 14 #include "chrome/browser/extensions/extension_tab_util.h" | |
| 15 #include "chrome/browser/extensions/location_bar_controller.h" | |
| 16 #include "chrome/browser/extensions/tab_helper.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/ui/browser_list.h" | |
| 19 #include "chrome/common/extensions/extension.h" | |
| 20 #include "content/public/browser/navigation_entry.h" | |
| 21 #include "content/public/browser/web_contents.h" | |
| 22 #include "extensions/common/error_utils.h" | |
| 23 | |
| 24 using content::NavigationEntry; | |
| 25 using extensions::ErrorUtils; | |
| 26 | |
| 27 namespace keys = extension_page_actions_api_constants; | |
| 28 | |
| 29 namespace { | |
| 30 // Errors. | |
| 31 const char kNoTabError[] = "No tab with id: *."; | |
| 32 const char kNoPageActionError[] = | |
| 33 "This extension has no page action specified."; | |
| 34 const char kUrlNotActiveError[] = "This url is no longer active: *."; | |
| 35 } | |
| 36 | |
| 37 PageActionsFunction::PageActionsFunction() { | |
| 38 } | |
| 39 | |
| 40 PageActionsFunction::~PageActionsFunction() { | |
| 41 } | |
| 42 | |
| 43 bool PageActionsFunction::SetPageActionEnabled(bool enable) { | |
| 44 std::string extension_action_id; | |
| 45 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); | |
| 46 DictionaryValue* action = NULL; | |
| 47 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); | |
| 48 | |
| 49 int tab_id; | |
| 50 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); | |
| 51 std::string url; | |
| 52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); | |
| 53 | |
| 54 std::string title; | |
| 55 if (enable) { | |
| 56 if (action->HasKey(keys::kTitleKey)) | |
| 57 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); | |
| 58 } | |
| 59 | |
| 60 ExtensionAction* page_action = | |
| 61 extensions::ExtensionActionManager::Get(profile())-> | |
| 62 GetPageAction(*GetExtension()); | |
| 63 if (!page_action) { | |
| 64 error_ = kNoPageActionError; | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 // Find the WebContents that contains this tab id. | |
| 69 content::WebContents* contents = NULL; | |
| 70 bool result = ExtensionTabUtil::GetTabById( | |
| 71 tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL); | |
| 72 if (!result || !contents) { | |
| 73 error_ = ErrorUtils::FormatErrorMessage( | |
| 74 kNoTabError, base::IntToString(tab_id)); | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 // Make sure the URL hasn't changed. | |
| 79 NavigationEntry* entry = contents->GetController().GetActiveEntry(); | |
| 80 if (!entry || url != entry->GetURL().spec()) { | |
| 81 error_ = ErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 // Set visibility and broadcast notifications that the UI should be updated. | |
| 86 page_action->SetAppearance( | |
| 87 tab_id, enable ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); | |
| 88 page_action->SetTitle(tab_id, title); | |
| 89 extensions::TabHelper::FromWebContents(contents)-> | |
| 90 location_bar_controller()->NotifyChange(); | |
| 91 | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 bool EnablePageActionsFunction::RunImpl() { | |
| 96 return SetPageActionEnabled(true); | |
| 97 } | |
| 98 | |
| 99 bool DisablePageActionsFunction::RunImpl() { | |
| 100 return SetPageActionEnabled(false); | |
| 101 } | |
| OLD | NEW |