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_action_api.h" | 5 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/base64.h" | 7 #include "base/base64.h" |
| 10 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 11 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 13 #include "base/values.h" | 11 #include "base/values.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_ api_constants.h" | 13 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_ api_constants.h" |
| 16 #include "chrome/browser/extensions/event_router.h" | 14 #include "chrome/browser/extensions/event_router.h" |
| 17 #include "chrome/browser/extensions/extension_action.h" | 15 #include "chrome/browser/extensions/extension_action.h" |
| 18 #include "chrome/browser/extensions/extension_action_manager.h" | 16 #include "chrome/browser/extensions/extension_action_manager.h" |
| 19 #include "chrome/browser/extensions/extension_function_registry.h" | 17 #include "chrome/browser/extensions/extension_function_registry.h" |
| 18 #include "chrome/browser/extensions/extension_host.h" | |
| 20 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 20 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/extensions/extension_tab_util.h" | 21 #include "chrome/browser/extensions/extension_tab_util.h" |
| 23 #include "chrome/browser/extensions/location_bar_controller.h" | 22 #include "chrome/browser/extensions/location_bar_controller.h" |
| 24 #include "chrome/browser/extensions/state_store.h" | 23 #include "chrome/browser/extensions/state_store.h" |
| 25 #include "chrome/browser/extensions/tab_helper.h" | 24 #include "chrome/browser/extensions/tab_helper.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/common/extensions/api/extension_action/action_info.h" | 26 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 28 #include "chrome/common/render_messages.h" | 27 #include "chrome/common/render_messages.h" |
| 29 #include "content/public/browser/navigation_entry.h" | 28 #include "content/public/browser/navigation_entry.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 52 // Whether the browser action is visible in the toolbar. | 51 // Whether the browser action is visible in the toolbar. |
| 53 const char kBrowserActionVisible[] = "browser_action_visible"; | 52 const char kBrowserActionVisible[] = "browser_action_visible"; |
| 54 | 53 |
| 55 // Errors. | 54 // Errors. |
| 56 const char kNoExtensionActionError[] = | 55 const char kNoExtensionActionError[] = |
| 57 "This extension has no action specified."; | 56 "This extension has no action specified."; |
| 58 const char kNoTabError[] = "No tab with id: *."; | 57 const char kNoTabError[] = "No tab with id: *."; |
| 59 const char kNoPageActionError[] = | 58 const char kNoPageActionError[] = |
| 60 "This extension has no page action specified."; | 59 "This extension has no page action specified."; |
| 61 const char kUrlNotActiveError[] = "This url is no longer active: *."; | 60 const char kUrlNotActiveError[] = "This url is no longer active: *."; |
| 61 const char kInternalError[] = "Internal error."; | |
| 62 | 62 |
| 63 struct IconRepresentationInfo { | 63 struct IconRepresentationInfo { |
| 64 // Size as a string that will be used to retrieve representation value from | 64 // Size as a string that will be used to retrieve representation value from |
| 65 // SetIcon function arguments. | 65 // SetIcon function arguments. |
| 66 const char* size_string; | 66 const char* size_string; |
| 67 // Scale factor for which the represantion should be used. | 67 // Scale factor for which the represantion should be used. |
| 68 ui::ScaleFactor scale; | 68 ui::ScaleFactor scale; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 const IconRepresentationInfo kIconSizes[] = { | 71 const IconRepresentationInfo kIconSizes[] = { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 registry->RegisterFunction<BrowserActionSetTitleFunction>(); | 202 registry->RegisterFunction<BrowserActionSetTitleFunction>(); |
| 203 registry->RegisterFunction<BrowserActionSetBadgeTextFunction>(); | 203 registry->RegisterFunction<BrowserActionSetBadgeTextFunction>(); |
| 204 registry->RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>(); | 204 registry->RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>(); |
| 205 registry->RegisterFunction<BrowserActionSetPopupFunction>(); | 205 registry->RegisterFunction<BrowserActionSetPopupFunction>(); |
| 206 registry->RegisterFunction<BrowserActionGetTitleFunction>(); | 206 registry->RegisterFunction<BrowserActionGetTitleFunction>(); |
| 207 registry->RegisterFunction<BrowserActionGetBadgeTextFunction>(); | 207 registry->RegisterFunction<BrowserActionGetBadgeTextFunction>(); |
| 208 registry->RegisterFunction<BrowserActionGetBadgeBackgroundColorFunction>(); | 208 registry->RegisterFunction<BrowserActionGetBadgeBackgroundColorFunction>(); |
| 209 registry->RegisterFunction<BrowserActionGetPopupFunction>(); | 209 registry->RegisterFunction<BrowserActionGetPopupFunction>(); |
| 210 registry->RegisterFunction<BrowserActionEnableFunction>(); | 210 registry->RegisterFunction<BrowserActionEnableFunction>(); |
| 211 registry->RegisterFunction<BrowserActionDisableFunction>(); | 211 registry->RegisterFunction<BrowserActionDisableFunction>(); |
| 212 registry->RegisterFunction<BrowserActionOpenPopupFunction>(); | |
| 212 | 213 |
| 213 // Page Actions | 214 // Page Actions |
| 214 registry->RegisterFunction<EnablePageActionsFunction>(); | 215 registry->RegisterFunction<EnablePageActionsFunction>(); |
| 215 registry->RegisterFunction<DisablePageActionsFunction>(); | 216 registry->RegisterFunction<DisablePageActionsFunction>(); |
| 216 registry->RegisterFunction<PageActionShowFunction>(); | 217 registry->RegisterFunction<PageActionShowFunction>(); |
| 217 registry->RegisterFunction<PageActionHideFunction>(); | 218 registry->RegisterFunction<PageActionHideFunction>(); |
| 218 registry->RegisterFunction<PageActionSetIconFunction>(); | 219 registry->RegisterFunction<PageActionSetIconFunction>(); |
| 219 registry->RegisterFunction<PageActionSetTitleFunction>(); | 220 registry->RegisterFunction<PageActionSetTitleFunction>(); |
| 220 registry->RegisterFunction<PageActionSetPopupFunction>(); | 221 registry->RegisterFunction<PageActionSetPopupFunction>(); |
| 221 registry->RegisterFunction<PageActionGetTitleFunction>(); | 222 registry->RegisterFunction<PageActionGetTitleFunction>(); |
| (...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 798 list->Append( | 799 list->Append( |
| 799 new base::FundamentalValue(static_cast<int>(SkColorGetG(color)))); | 800 new base::FundamentalValue(static_cast<int>(SkColorGetG(color)))); |
| 800 list->Append( | 801 list->Append( |
| 801 new base::FundamentalValue(static_cast<int>(SkColorGetB(color)))); | 802 new base::FundamentalValue(static_cast<int>(SkColorGetB(color)))); |
| 802 list->Append( | 803 list->Append( |
| 803 new base::FundamentalValue(static_cast<int>(SkColorGetA(color)))); | 804 new base::FundamentalValue(static_cast<int>(SkColorGetA(color)))); |
| 804 SetResult(list); | 805 SetResult(list); |
| 805 return true; | 806 return true; |
| 806 } | 807 } |
| 807 | 808 |
| 809 BrowserActionOpenPopupFunction::BrowserActionOpenPopupFunction() | |
| 810 : response_sent_(false) { | |
| 811 } | |
| 812 | |
| 813 bool BrowserActionOpenPopupFunction::RunImpl() { | |
| 814 ExtensionToolbarModel* model = extensions::ExtensionSystem::Get(profile_)-> | |
| 815 extension_service()->toolbar_model(); | |
| 816 if (!model) { | |
| 817 error_ = kInternalError; | |
| 818 return false; | |
| 819 } | |
| 820 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | |
| 821 content::Source<Profile>(profile_)); | |
| 822 model->ShowBrowserActionPopup(extension_); | |
| 823 | |
| 824 // Dispatch notification for popup not having been opened after a timeout. | |
| 825 base::MessageLoopForUI::current()->PostDelayedTask( | |
| 826 FROM_HERE, | |
| 827 base::Bind(&BrowserActionOpenPopupFunction::OpenPopupTimedOut, this), | |
| 828 base::TimeDelta::FromSeconds(1)); | |
| 829 return true; | |
| 830 } | |
| 831 | |
| 832 void BrowserActionOpenPopupFunction::OpenPopupTimedOut() { | |
| 833 if (response_sent_) | |
| 834 return; | |
| 835 | |
| 836 DVLOG(1) << "chrome.browserAction.openPopup did not show a popup."; | |
| 837 // Custom binding will still try to get the popup, but may fail. | |
| 838 SendResponse(new base::ListValue()); | |
|
Finnur
2013/10/16 15:01:26
It seems like we're jumping through hoops just to
justinlin
2013/10/16 19:31:29
Hmm, that's a good point and right it's for the cu
| |
| 839 response_sent_ = true; | |
| 840 } | |
| 841 | |
| 842 void BrowserActionOpenPopupFunction::Observe( | |
| 843 int type, | |
| 844 const content::NotificationSource& source, | |
| 845 const content::NotificationDetails& details) { | |
| 846 if (response_sent_ || | |
| 847 type != chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING) | |
| 848 return; | |
|
Finnur
2013/10/16 15:01:26
Should be enough to DCHECK at the top that type ==
justinlin
2013/10/16 19:31:29
Done.
| |
| 849 | |
| 850 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | |
| 851 if (host->extension()->id() != extension_->id()) | |
| 852 return; | |
| 853 | |
| 854 SendResponse(new base::ListValue()); | |
| 855 response_sent_ = true; | |
| 856 // Don't need to de-register notification listener since the last ref for this | |
| 857 // object will go away after OpenPopupTimedOut is called. | |
| 858 } | |
| 859 | |
| 808 // | 860 // |
| 809 // ScriptBadgeGetAttentionFunction | 861 // ScriptBadgeGetAttentionFunction |
| 810 // | 862 // |
| 811 | 863 |
| 812 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {} | 864 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {} |
| 813 | 865 |
| 814 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() { | 866 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() { |
| 815 tab_helper().location_bar_controller()->GetAttentionFor(extension_id()); | 867 tab_helper().location_bar_controller()->GetAttentionFor(extension_id()); |
| 816 return true; | 868 return true; |
| 817 } | 869 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 884 return true; | 936 return true; |
| 885 } | 937 } |
| 886 | 938 |
| 887 bool EnablePageActionsFunction::RunImpl() { | 939 bool EnablePageActionsFunction::RunImpl() { |
| 888 return SetPageActionEnabled(true); | 940 return SetPageActionEnabled(true); |
| 889 } | 941 } |
| 890 | 942 |
| 891 bool DisablePageActionsFunction::RunImpl() { | 943 bool DisablePageActionsFunction::RunImpl() { |
| 892 return SetPageActionEnabled(false); | 944 return SetPageActionEnabled(false); |
| 893 } | 945 } |
| OLD | NEW |