Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: chrome/browser/extensions/api/extension_action/extension_action_api.cc

Issue 25305002: Implement initial chrome.browserAction.openPopup API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move to interactive_ui_test Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 kOpenPopupError[] =
62 "Failed to show popup either because there is an existing popup or another "
63 "error occurred.";
64 const char kInternalError[] = "Internal error.";
62 65
63 struct IconRepresentationInfo { 66 struct IconRepresentationInfo {
64 // Size as a string that will be used to retrieve representation value from 67 // Size as a string that will be used to retrieve representation value from
65 // SetIcon function arguments. 68 // SetIcon function arguments.
66 const char* size_string; 69 const char* size_string;
67 // Scale factor for which the represantion should be used. 70 // Scale factor for which the represantion should be used.
68 ui::ScaleFactor scale; 71 ui::ScaleFactor scale;
69 }; 72 };
70 73
71 const IconRepresentationInfo kIconSizes[] = { 74 const IconRepresentationInfo kIconSizes[] = {
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 registry->RegisterFunction<BrowserActionSetTitleFunction>(); 205 registry->RegisterFunction<BrowserActionSetTitleFunction>();
203 registry->RegisterFunction<BrowserActionSetBadgeTextFunction>(); 206 registry->RegisterFunction<BrowserActionSetBadgeTextFunction>();
204 registry->RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>(); 207 registry->RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>();
205 registry->RegisterFunction<BrowserActionSetPopupFunction>(); 208 registry->RegisterFunction<BrowserActionSetPopupFunction>();
206 registry->RegisterFunction<BrowserActionGetTitleFunction>(); 209 registry->RegisterFunction<BrowserActionGetTitleFunction>();
207 registry->RegisterFunction<BrowserActionGetBadgeTextFunction>(); 210 registry->RegisterFunction<BrowserActionGetBadgeTextFunction>();
208 registry->RegisterFunction<BrowserActionGetBadgeBackgroundColorFunction>(); 211 registry->RegisterFunction<BrowserActionGetBadgeBackgroundColorFunction>();
209 registry->RegisterFunction<BrowserActionGetPopupFunction>(); 212 registry->RegisterFunction<BrowserActionGetPopupFunction>();
210 registry->RegisterFunction<BrowserActionEnableFunction>(); 213 registry->RegisterFunction<BrowserActionEnableFunction>();
211 registry->RegisterFunction<BrowserActionDisableFunction>(); 214 registry->RegisterFunction<BrowserActionDisableFunction>();
215 registry->RegisterFunction<BrowserActionOpenPopupFunction>();
212 216
213 // Page Actions 217 // Page Actions
214 registry->RegisterFunction<EnablePageActionsFunction>(); 218 registry->RegisterFunction<EnablePageActionsFunction>();
215 registry->RegisterFunction<DisablePageActionsFunction>(); 219 registry->RegisterFunction<DisablePageActionsFunction>();
216 registry->RegisterFunction<PageActionShowFunction>(); 220 registry->RegisterFunction<PageActionShowFunction>();
217 registry->RegisterFunction<PageActionHideFunction>(); 221 registry->RegisterFunction<PageActionHideFunction>();
218 registry->RegisterFunction<PageActionSetIconFunction>(); 222 registry->RegisterFunction<PageActionSetIconFunction>();
219 registry->RegisterFunction<PageActionSetTitleFunction>(); 223 registry->RegisterFunction<PageActionSetTitleFunction>();
220 registry->RegisterFunction<PageActionSetPopupFunction>(); 224 registry->RegisterFunction<PageActionSetPopupFunction>();
221 registry->RegisterFunction<PageActionGetTitleFunction>(); 225 registry->RegisterFunction<PageActionGetTitleFunction>();
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 list->Append( 802 list->Append(
799 new base::FundamentalValue(static_cast<int>(SkColorGetG(color)))); 803 new base::FundamentalValue(static_cast<int>(SkColorGetG(color))));
800 list->Append( 804 list->Append(
801 new base::FundamentalValue(static_cast<int>(SkColorGetB(color)))); 805 new base::FundamentalValue(static_cast<int>(SkColorGetB(color))));
802 list->Append( 806 list->Append(
803 new base::FundamentalValue(static_cast<int>(SkColorGetA(color)))); 807 new base::FundamentalValue(static_cast<int>(SkColorGetA(color))));
804 SetResult(list); 808 SetResult(list);
805 return true; 809 return true;
806 } 810 }
807 811
812 BrowserActionOpenPopupFunction::BrowserActionOpenPopupFunction()
813 : response_sent_(false) {
814 }
815
816 bool BrowserActionOpenPopupFunction::RunImpl() {
817 ExtensionToolbarModel* model = extensions::ExtensionSystem::Get(profile_)->
818 extension_service()->toolbar_model();
819 if (!model) {
820 error_ = kInternalError;
821 return false;
822 }
823
824 if (!model->ShowBrowserActionPopup(extension_)) {
825 error_ = kOpenPopupError;
826 return false;
827 }
828
829 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
830 content::Source<Profile>(profile_));
831
832 // Set a timeout for waiting for the notification that the popup is loaded.
833 // Waiting is required so that the popup view can be retrieved by the custom
834 // bindings for the response callback. It's also needed to keep this function
835 // instance around until a notification is observed.
836 base::MessageLoopForUI::current()->PostDelayedTask(
837 FROM_HERE,
838 base::Bind(&BrowserActionOpenPopupFunction::OpenPopupTimedOut, this),
839 base::TimeDelta::FromSeconds(10));
840 return true;
841 }
842
843 void BrowserActionOpenPopupFunction::OpenPopupTimedOut() {
844 if (response_sent_)
845 return;
846
847 DVLOG(1) << "chrome.browserAction.openPopup did not show a popup.";
848 error_ = kOpenPopupError;
849 SendResponse(false);
850 response_sent_ = true;
851 }
852
853 void BrowserActionOpenPopupFunction::Observe(
854 int type,
855 const content::NotificationSource& source,
856 const content::NotificationDetails& details) {
857 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, type);
858 if (response_sent_)
859 return;
860
861 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
862 if (host->extension_host_type() != VIEW_TYPE_EXTENSION_POPUP ||
863 host->extension()->id() != extension_->id())
864 return;
865
866 SendResponse(true);
867 response_sent_ = true;
868 registrar_.RemoveAll();
869 }
870
808 // 871 //
809 // ScriptBadgeGetAttentionFunction 872 // ScriptBadgeGetAttentionFunction
810 // 873 //
811 874
812 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {} 875 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {}
813 876
814 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() { 877 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() {
815 tab_helper().location_bar_controller()->GetAttentionFor(extension_id()); 878 tab_helper().location_bar_controller()->GetAttentionFor(extension_id());
816 return true; 879 return true;
817 } 880 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 return true; 947 return true;
885 } 948 }
886 949
887 bool EnablePageActionsFunction::RunImpl() { 950 bool EnablePageActionsFunction::RunImpl() {
888 return SetPageActionEnabled(true); 951 return SetPageActionEnabled(true);
889 } 952 }
890 953
891 bool DisablePageActionsFunction::RunImpl() { 954 bool DisablePageActionsFunction::RunImpl() {
892 return SetPageActionEnabled(false); 955 return SetPageActionEnabled(false);
893 } 956 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698