Chromium Code Reviews| 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."; | 34 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; |
|
Yoyo Zhou
2012/09/13 20:42:39
These 2 are no longer used.
Matt Perry
2012/09/13 21:39:28
Done.
| |
| 35 const char kNoIconSpecified[] = "Page action has no icons to show."; | 35 const char kNoIconSpecified[] = "Page action has no icons to show."; |
| 36 } | 36 } |
| 37 | 37 |
| 38 PageActionsFunction::PageActionsFunction() { | 38 PageActionsFunction::PageActionsFunction() { |
| 39 } | 39 } |
| 40 | 40 |
| 41 PageActionsFunction::~PageActionsFunction() { | 41 PageActionsFunction::~PageActionsFunction() { |
| 42 } | 42 } |
| 43 | 43 |
| 44 bool PageActionsFunction::SetPageActionEnabled(bool enable) { | 44 bool PageActionsFunction::SetPageActionEnabled(bool enable) { |
| 45 std::string extension_action_id; | 45 std::string extension_action_id; |
| 46 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); | 46 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &extension_action_id)); |
| 47 DictionaryValue* action = NULL; | 47 DictionaryValue* action = NULL; |
| 48 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); | 48 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &action)); |
| 49 | 49 |
| 50 int tab_id; | 50 int tab_id; |
| 51 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); | 51 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); |
| 52 std::string url; | 52 std::string url; |
| 53 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); | 53 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); |
| 54 | 54 |
| 55 std::string title; | 55 std::string title; |
| 56 int icon_id = 0; | |
| 57 if (enable) { | 56 if (enable) { |
| 58 // Both of those are optional. | |
| 59 if (action->HasKey(keys::kTitleKey)) | 57 if (action->HasKey(keys::kTitleKey)) |
| 60 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); | 58 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); |
| 61 if (action->HasKey(keys::kIconIdKey)) { | |
| 62 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey, | |
|
Yoyo Zhou
2012/09/13 20:42:39
Looks like this constant can be removed.
(Of cours
Matt Perry
2012/09/13 21:39:28
Done.
| |
| 63 &icon_id)); | |
| 64 } | |
| 65 } | 59 } |
| 66 | 60 |
| 67 ExtensionAction* page_action = GetExtension()->page_action(); | 61 ExtensionAction* page_action = GetExtension()->page_action(); |
| 68 if (!page_action) { | 62 if (!page_action) { |
| 69 error_ = kNoPageActionError; | 63 error_ = kNoPageActionError; |
| 70 return false; | 64 return false; |
| 71 } | 65 } |
| 72 | 66 |
| 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. | 67 // Find the TabContents that contains this tab id. |
| 80 TabContents* contents = NULL; | 68 TabContents* contents = NULL; |
| 81 bool result = ExtensionTabUtil::GetTabById( | 69 bool result = ExtensionTabUtil::GetTabById( |
| 82 tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL); | 70 tab_id, profile(), include_incognito(), NULL, NULL, &contents, NULL); |
| 83 if (!result || !contents) { | 71 if (!result || !contents) { |
| 84 error_ = ExtensionErrorUtils::FormatErrorMessage( | 72 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 85 kNoTabError, base::IntToString(tab_id)); | 73 kNoTabError, base::IntToString(tab_id)); |
| 86 return false; | 74 return false; |
| 87 } | 75 } |
| 88 | 76 |
| 89 // Make sure the URL hasn't changed. | 77 // Make sure the URL hasn't changed. |
| 90 NavigationEntry* entry = | 78 NavigationEntry* entry = |
| 91 contents->web_contents()->GetController().GetActiveEntry(); | 79 contents->web_contents()->GetController().GetActiveEntry(); |
| 92 if (!entry || url != entry->GetURL().spec()) { | 80 if (!entry || url != entry->GetURL().spec()) { |
| 93 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); | 81 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); |
| 94 return false; | 82 return false; |
| 95 } | 83 } |
| 96 | 84 |
| 97 // Set visibility and broadcast notifications that the UI should be updated. | 85 // Set visibility and broadcast notifications that the UI should be updated. |
| 98 page_action->SetAppearance( | 86 page_action->SetAppearance( |
| 99 tab_id, enable ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); | 87 tab_id, enable ? ExtensionAction::ACTIVE : ExtensionAction::INVISIBLE); |
| 100 page_action->SetTitle(tab_id, title); | 88 page_action->SetTitle(tab_id, title); |
| 101 page_action->SetIconIndex(tab_id, icon_id); | |
| 102 extensions::TabHelper::FromWebContents(contents->web_contents())-> | 89 extensions::TabHelper::FromWebContents(contents->web_contents())-> |
| 103 location_bar_controller()->NotifyChange(); | 90 location_bar_controller()->NotifyChange(); |
| 104 | 91 |
| 105 return true; | 92 return true; |
| 106 } | 93 } |
| 107 | 94 |
| 108 bool EnablePageActionsFunction::RunImpl() { | 95 bool EnablePageActionsFunction::RunImpl() { |
| 109 return SetPageActionEnabled(true); | 96 return SetPageActionEnabled(true); |
| 110 } | 97 } |
| 111 | 98 |
| 112 bool DisablePageActionsFunction::RunImpl() { | 99 bool DisablePageActionsFunction::RunImpl() { |
| 113 return SetPageActionEnabled(false); | 100 return SetPageActionEnabled(false); |
| 114 } | 101 } |
| OLD | NEW |