| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_page_actions_module.h" | 5 #include "chrome/browser/extensions/extension_page_actions_module.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
| 10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| 11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" | 11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" |
| 12 #include "chrome/browser/extensions/extension_tabs_module.h" | 12 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 13 #include "chrome/browser/extensions/extensions_service.h" | 13 #include "chrome/browser/extensions/extensions_service.h" |
| 14 #include "chrome/browser/tab_contents/navigation_entry.h" | 14 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 15 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
| 16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 17 #include "chrome/common/extensions/extension_error_utils.h" | 17 #include "chrome/common/extensions/extension_error_utils.h" |
| 18 #include "chrome/common/render_messages.h" | 18 #include "chrome/common/render_messages.h" |
| 19 | 19 |
| 20 namespace keys = extension_page_actions_module_constants; | 20 namespace keys = extension_page_actions_module_constants; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 // Errors. | 23 // Errors. |
| 24 const char kNoExtensionError[] = "No extension with id: *."; | |
| 25 const char kNoTabError[] = "No tab with id: *."; | 24 const char kNoTabError[] = "No tab with id: *."; |
| 26 const char kNoPageActionError[] = | 25 const char kNoPageActionError[] = |
| 27 "This extension has no page action specified."; | 26 "This extension has no page action specified."; |
| 28 const char kUrlNotActiveError[] = "This url is no longer active: *."; | 27 const char kUrlNotActiveError[] = "This url is no longer active: *."; |
| 29 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; | 28 const char kIconIndexOutOfBounds[] = "Page action icon index out of bounds."; |
| 29 const char kNoIconSpecified[] = "Page action has no icons to show."; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // TODO(EXTENSIONS_DEPRECATED): obsolete API. | 32 // TODO(EXTENSIONS_DEPRECATED): obsolete API. |
| 33 bool PageActionFunction::SetPageActionEnabled(bool enable) { | 33 bool PageActionFunction::SetPageActionEnabled(bool enable) { |
| 34 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | 34 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 35 const ListValue* args = static_cast<const ListValue*>(args_); | 35 const ListValue* args = static_cast<const ListValue*>(args_); |
| 36 | 36 |
| 37 std::string page_action_id; | 37 std::string page_action_id; |
| 38 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id)); | 38 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &page_action_id)); |
| 39 DictionaryValue* action; | 39 DictionaryValue* action; |
| 40 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action)); | 40 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &action)); |
| 41 | 41 |
| 42 int tab_id; | 42 int tab_id; |
| 43 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); | 43 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kTabIdKey, &tab_id)); |
| 44 std::string url; | 44 std::string url; |
| 45 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); | 45 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kUrlKey, &url)); |
| 46 | 46 |
| 47 std::string title; | 47 std::string title; |
| 48 int icon_id = 0; | 48 int icon_id = 0; |
| 49 if (enable) { | 49 if (enable) { |
| 50 // Both of those are optional. | 50 // Both of those are optional. |
| 51 if (action->HasKey(keys::kTitleKey)) | 51 if (action->HasKey(keys::kTitleKey)) |
| 52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); | 52 EXTENSION_FUNCTION_VALIDATE(action->GetString(keys::kTitleKey, &title)); |
| 53 if (action->HasKey(keys::kIconIdKey)) { | 53 if (action->HasKey(keys::kIconIdKey)) { |
| 54 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey, | 54 EXTENSION_FUNCTION_VALIDATE(action->GetInteger(keys::kIconIdKey, |
| 55 &icon_id)); | 55 &icon_id)); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 const ExtensionAction* page_action = |
| 60 dispatcher()->GetExtension()->page_action(); |
| 61 if (!page_action) { |
| 62 error_ = kNoPageActionError; |
| 63 return false; |
| 64 } |
| 65 |
| 66 if (icon_id < 0 || |
| 67 static_cast<size_t>(icon_id) >= page_action->icon_paths().size()) { |
| 68 error_ = (icon_id == 0) ? kNoIconSpecified : kIconIndexOutOfBounds; |
| 69 return false; |
| 70 } |
| 71 |
| 59 // Find the TabContents that contains this tab id. | 72 // Find the TabContents that contains this tab id. |
| 60 TabContents* contents = NULL; | 73 TabContents* contents = NULL; |
| 61 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL); | 74 ExtensionTabUtil::GetTabById(tab_id, profile(), NULL, NULL, &contents, NULL); |
| 62 if (!contents) { | 75 if (!contents) { |
| 63 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError, | 76 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoTabError, |
| 64 IntToString(tab_id)); | 77 IntToString(tab_id)); |
| 65 return false; | 78 return false; |
| 66 } | 79 } |
| 67 | 80 |
| 68 // Make sure the URL hasn't changed. | 81 // Make sure the URL hasn't changed. |
| 69 NavigationEntry* entry = contents->controller().GetActiveEntry(); | 82 NavigationEntry* entry = contents->controller().GetActiveEntry(); |
| 70 if (!entry || url != entry->url().spec()) { | 83 if (!entry || url != entry->url().spec()) { |
| 71 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); | 84 error_ = ExtensionErrorUtils::FormatErrorMessage(kUrlNotActiveError, url); |
| 72 return false; | 85 return false; |
| 73 } | 86 } |
| 74 | 87 |
| 75 // Find our extension. | |
| 76 Extension* extension = NULL; | |
| 77 ExtensionsService* service = profile()->GetExtensionsService(); | |
| 78 extension = service->GetExtensionById(extension_id()); | |
| 79 if (!extension) { | |
| 80 error_ = ExtensionErrorUtils::FormatErrorMessage(kNoExtensionError, | |
| 81 extension_id()); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 const ExtensionAction* page_action = extension->page_action(); | |
| 86 if (!page_action) { | |
| 87 error_ = kNoPageActionError; | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 // Set visibility and broadcast notifications that the UI should be updated. | 88 // Set visibility and broadcast notifications that the UI should be updated. |
| 92 contents->SetPageActionEnabled(page_action, enable, title, icon_id); | 89 contents->SetPageActionEnabled(page_action, enable, title, icon_id); |
| 93 contents->PageActionStateChanged(); | 90 contents->PageActionStateChanged(); |
| 94 | 91 |
| 95 return true; | 92 return true; |
| 96 } | 93 } |
| 97 | 94 |
| 98 bool PageActionFunction::InitCommon(int tab_id) { | 95 bool PageActionFunction::InitCommon(int tab_id) { |
| 99 page_action_ = dispatcher()->GetExtension()->page_action(); | 96 page_action_ = dispatcher()->GetExtension()->page_action(); |
| 100 if (!page_action_) { | 97 if (!page_action_) { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 if (!InitCommon(tab_id)) | 249 if (!InitCommon(tab_id)) |
| 253 return false; | 250 return false; |
| 254 | 251 |
| 255 std::string text; | 252 std::string text; |
| 256 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text)); | 253 EXTENSION_FUNCTION_VALIDATE(args->GetString(L"text", &text)); |
| 257 | 254 |
| 258 state_->set_badge_text(text); | 255 state_->set_badge_text(text); |
| 259 contents_->PageActionStateChanged(); | 256 contents_->PageActionStateChanged(); |
| 260 return true; | 257 return true; |
| 261 } | 258 } |
| OLD | NEW |