| 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 // Set a timeout for waiting for the notification that the popup is loaded. |
| 825 // Waiting is required so that the popup view can be retrieved by the custom |
| 826 // bindings for the response callback. It's also needed to keep this function |
| 827 // instance around until a notification is observed. |
| 828 base::MessageLoopForUI::current()->PostDelayedTask( |
| 829 FROM_HERE, |
| 830 base::Bind(&BrowserActionOpenPopupFunction::OpenPopupTimedOut, this), |
| 831 base::TimeDelta::FromSeconds(1)); |
| 832 return true; |
| 833 } |
| 834 |
| 835 void BrowserActionOpenPopupFunction::OpenPopupTimedOut() { |
| 836 if (response_sent_) |
| 837 return; |
| 838 |
| 839 DVLOG(1) << "chrome.browserAction.openPopup did not show a popup."; |
| 840 // Custom binding will still try to get the popup, but may fail. |
| 841 SendResponse(false); |
| 842 response_sent_ = true; |
| 843 } |
| 844 |
| 845 void BrowserActionOpenPopupFunction::Observe( |
| 846 int type, |
| 847 const content::NotificationSource& source, |
| 848 const content::NotificationDetails& details) { |
| 849 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING); |
| 850 if (response_sent_) |
| 851 return; |
| 852 |
| 853 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
| 854 if (host->extension_host_type() != VIEW_TYPE_EXTENSION_POPUP || |
| 855 host->extension()->id() != extension_->id()) |
| 856 return; |
| 857 |
| 858 SendResponse(true); |
| 859 response_sent_ = true; |
| 860 registrar_.RemoveAll(); |
| 861 } |
| 862 |
| 808 // | 863 // |
| 809 // ScriptBadgeGetAttentionFunction | 864 // ScriptBadgeGetAttentionFunction |
| 810 // | 865 // |
| 811 | 866 |
| 812 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {} | 867 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {} |
| 813 | 868 |
| 814 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() { | 869 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() { |
| 815 tab_helper().location_bar_controller()->GetAttentionFor(extension_id()); | 870 tab_helper().location_bar_controller()->GetAttentionFor(extension_id()); |
| 816 return true; | 871 return true; |
| 817 } | 872 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 return true; | 939 return true; |
| 885 } | 940 } |
| 886 | 941 |
| 887 bool EnablePageActionsFunction::RunImpl() { | 942 bool EnablePageActionsFunction::RunImpl() { |
| 888 return SetPageActionEnabled(true); | 943 return SetPageActionEnabled(true); |
| 889 } | 944 } |
| 890 | 945 |
| 891 bool DisablePageActionsFunction::RunImpl() { | 946 bool DisablePageActionsFunction::RunImpl() { |
| 892 return SetPageActionEnabled(false); | 947 return SetPageActionEnabled(false); |
| 893 } | 948 } |
| OLD | NEW |