| 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/api/extension_action/extension_page_actions_
api.h" | 5 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" | 10 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_
api_constants.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 using content::NavigationEntry; | 24 using content::NavigationEntry; |
| 25 | 25 |
| 26 namespace keys = extension_page_actions_api_constants; | 26 namespace keys = extension_page_actions_api_constants; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 // Errors. | 29 // Errors. |
| 30 const char kNoTabError[] = "No tab with id: *."; | 30 const char kNoTabError[] = "No tab with id: *."; |
| 31 const char kNoPageActionError[] = | 31 const char kNoPageActionError[] = |
| 32 "This extension has no page action specified."; | 32 "This extension has no page action specified."; |
| 33 const char kUrlNotActiveError[] = "This url is no longer active: *."; | 33 const char kUrlNotActiveError[] = "This url is no longer active: *."; |
| 34 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; | |
| 35 const char kNoIconSpecified[] = "Page action has no icons to show."; | |
| 36 } | 34 } |
| 37 | 35 |
| 38 PageActionsFunction::PageActionsFunction() { | 36 PageActionsFunction::PageActionsFunction() { |
| 39 } | 37 } |
| 40 | 38 |
| 41 PageActionsFunction::~PageActionsFunction() { | 39 PageActionsFunction::~PageActionsFunction() { |
| 42 } | 40 } |
| 43 | 41 |
| 44 bool PageActionsFunction::SetPageActionEnabled(bool enable) { | 42 bool PageActionsFunction::SetPageActionEnabled(bool enable) { |
| 45 std::string extension_action_id; | 43 std::string extension_action_id; |
| 46 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); | 44 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); |
| 47 DictionaryValue* action = NULL; | 45 DictionaryValue* action = NULL; |
| 48 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); | 46 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); |
| 49 | 47 |
| 50 int tab_id; | 48 int tab_id; |
| 51 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); | 49 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); |
| 52 std::string url; | 50 std::string url; |
| 53 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); | 51 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); |
| 54 | 52 |
| 55 std::string title; | 53 std::string title; |
| 56 int icon_id = 0; | |
| 57 if (enable) { | 54 if (enable) { |
| 58 // Both of those are optional. | |
| 59 if (action->HasKey(keys::kTitleKey)) | 55 if (action->HasKey(keys::kTitleKey)) |
| 60 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); | 56 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); |
| 61 if (action->HasKey(keys::kIconIdKey)) { | |
| 62 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey, | |
| 63 &icon_id)); | |
| 64 } | |
| 65 } | 57 } |
| 66 | 58 |
| 67 ExtensionAction* page_action = GetExtension()->page_action(); | 59 ExtensionAction* page_action = GetExtension()->page_action(); |
| 68 if (!page_action) { | 60 if (!page_action) { |
| 69 error_ = kNoPageActionError; | 61 error_ = kNoPageActionError; |
| 70 return false; | 62 return false; |
| 71 } | 63 } |
| 72 | 64 |
| 73 if (icon_id < 0 || | |
| 74 static_cast<size_t>(icon_id) >= page_action->icon_paths()->size()) { | |
| 75 error_ = (icon_id == 0) ? kNoIconSpecified : kIconIndexOutOfBounds; | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 // Find the TabContents that contains this tab id. | 65 // Find the TabContents that contains this tab id. |
| 80 TabContents* contents = NULL; | 66 TabContents* contents = NULL; |
| 81 bool result = ExtensionTabUtil::GetTabById( | 67 bool result = ExtensionTabUtil::GetTabById( |
| 82 tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL); | 68 tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL); |
| 83 if (!result || !contents) { | 69 if (!result || !contents) { |
| 84 error_ = ExtensionErrorUtils::FormatErrorMessage( | 70 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 85 kNoTabError, base::IntToString(tab_id)); | 71 kNoTabError, base::IntToString(tab_id)); |
| 86 return false; | 72 return false; |
| 87 } | 73 } |
| 88 | 74 |
| 89 // Make sure the URL hasn't changed. | 75 // Make sure the URL hasn't changed. |
| 90 NavigationEntry* entry = | 76 NavigationEntry* entry = |
| 91 contents->web_contents()->GetController().GetActiveEntry(); | 77 contents->web_contents()->GetController().GetActiveEntry(); |
| 92 if (!entry || url != entry->GetURL().spec()) { | 78 if (!entry || url != entry->GetURL().spec()) { |
| 93 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); | 79 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); |
| 94 return false; | 80 return false; |
| 95 } | 81 } |
| 96 | 82 |
| 97 // Set visibility and broadcast notifications that the UI should be updated. | 83 // Set visibility and broadcast notifications that the UI should be updated. |
| 98 page_action->SetAppearance( | 84 page_action->SetAppearance( |
| 99 tab_id, enable ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); | 85 tab_id, enable ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); |
| 100 page_action->SetTitle(tab_id, title); | 86 page_action->SetTitle(tab_id, title); |
| 101 page_action->SetIconIndex(tab_id, icon_id); | |
| 102 extensions::TabHelper::FromWebContents(contents->web_contents())-> | 87 extensions::TabHelper::FromWebContents(contents->web_contents())-> |
| 103 location_bar_controller()->NotifyChange(); | 88 location_bar_controller()->NotifyChange(); |
| 104 | 89 |
| 105 return true; | 90 return true; |
| 106 } | 91 } |
| 107 | 92 |
| 108 bool EnablePageActionsFunction::RunImpl() { | 93 bool EnablePageActionsFunction::RunImpl() { |
| 109 return SetPageActionEnabled(true); | 94 return SetPageActionEnabled(true); |
| 110 } | 95 } |
| 111 | 96 |
| 112 bool DisablePageActionsFunction::RunImpl() { | 97 bool DisablePageActionsFunction::RunImpl() { |
| 113 return SetPageActionEnabled(false); | 98 return SetPageActionEnabled(false); |
| 114 } | 99 } |
| OLD | NEW |