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

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: comments 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 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
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
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 response callback.
827 base::MessageLoopForUI::current()->PostDelayedTask(
828 FROM_HERE,
829 base::Bind(&BrowserActionOpenPopupFunction::OpenPopupTimedOut, this),
830 base::TimeDelta::FromSeconds(1));
Matt Perry 2013/10/16 20:34:35 Why add a timeout? The popup can take a while to l
justinlin 2013/10/16 21:16:23 The timeout is mostly needed because there isn't a
Matt Perry 2013/10/16 21:32:50 I believe you're guaranteed to get a DID_STOP_LOAD
justinlin 2013/10/16 21:42:01 I mean if the extension doesn't try loading at all
Matt Perry 2013/10/16 21:45:21 Can you add logic to detect that instead? Maybe Sh
justinlin 2013/10/17 04:14:52 Yea, I considered it, but it seemed a bit weird to
831 return true;
832 }
833
834 void BrowserActionOpenPopupFunction::OpenPopupTimedOut() {
835 if (response_sent_)
836 return;
837
838 DVLOG(1) << "chrome.browserAction.openPopup did not show a popup.";
839 // Custom binding will still try to get the popup, but may fail.
840 SendResponse(false);
841 response_sent_ = true;
842 }
843
844 void BrowserActionOpenPopupFunction::Observe(
845 int type,
846 const content::NotificationSource& source,
847 const content::NotificationDetails& details) {
848 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING);
849 if (response_sent_)
850 return;
851
852 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
853 if (host->extension()->id() != extension_->id())
Matt Perry 2013/10/16 20:34:35 You should also check that the host type is popup.
justinlin 2013/10/16 21:16:23 Done.
854 return;
855
856 SendResponse(true);
857 response_sent_ = true;
858 registrar_.RemoveAll();
859 }
860
808 // 861 //
809 // ScriptBadgeGetAttentionFunction 862 // ScriptBadgeGetAttentionFunction
810 // 863 //
811 864
812 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {} 865 ScriptBadgeGetAttentionFunction::~ScriptBadgeGetAttentionFunction() {}
813 866
814 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() { 867 bool ScriptBadgeGetAttentionFunction::RunExtensionAction() {
815 tab_helper().location_bar_controller()->GetAttentionFor(extension_id()); 868 tab_helper().location_bar_controller()->GetAttentionFor(extension_id());
816 return true; 869 return true;
817 } 870 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 return true; 937 return true;
885 } 938 }
886 939
887 bool EnablePageActionsFunction::RunImpl() { 940 bool EnablePageActionsFunction::RunImpl() {
888 return SetPageActionEnabled(true); 941 return SetPageActionEnabled(true);
889 } 942 }
890 943
891 bool DisablePageActionsFunction::RunImpl() { 944 bool DisablePageActionsFunction::RunImpl() {
892 return SetPageActionEnabled(false); 945 return SetPageActionEnabled(false);
893 } 946 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698