| 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_popup_api.h" | 5 #include "chrome/browser/extensions/extension_popup_api.h" |
| 6 | 6 |
| 7 #include "base/gfx/point.h" | 7 #include "base/gfx/point.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 const char kBadAnchorArgument[] = "Invalid anchor argument."; | 35 const char kBadAnchorArgument[] = "Invalid anchor argument."; |
| 36 const char kInvalidURLError[] = "Invalid URL."; | 36 const char kInvalidURLError[] = "Invalid URL."; |
| 37 const char kNotAnExtension[] = "Not an extension view."; | 37 const char kNotAnExtension[] = "Not an extension view."; |
| 38 | 38 |
| 39 // Keys. | 39 // Keys. |
| 40 const wchar_t kUrlKey[] = L"url"; | 40 const wchar_t kUrlKey[] = L"url"; |
| 41 const wchar_t kWidthKey[] = L"width"; | 41 const wchar_t kWidthKey[] = L"width"; |
| 42 const wchar_t kHeightKey[] = L"height"; | 42 const wchar_t kHeightKey[] = L"height"; |
| 43 const wchar_t kTopKey[] = L"top"; | 43 const wchar_t kTopKey[] = L"top"; |
| 44 const wchar_t kLeftKey[] = L"left"; | 44 const wchar_t kLeftKey[] = L"left"; |
| 45 const wchar_t kGiveFocusKey[] = L"giveFocus"; |
| 46 const wchar_t kDomAnchorKey[] = L"domAnchor"; |
| 45 | 47 |
| 46 }; // namespace | 48 }; // namespace |
| 47 | 49 |
| 48 PopupShowFunction::PopupShowFunction() | 50 PopupShowFunction::PopupShowFunction() |
| 49 #if defined (TOOLKIT_VIEWS) | 51 #if defined (TOOLKIT_VIEWS) |
| 50 : popup_(NULL) | 52 : popup_(NULL) |
| 51 #endif | 53 #endif |
| 52 {} | 54 {} |
| 53 | 55 |
| 54 void PopupShowFunction::Run() { | 56 void PopupShowFunction::Run() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 74 #endif | 76 #endif |
| 75 } | 77 } |
| 76 | 78 |
| 77 bool PopupShowFunction::RunImpl() { | 79 bool PopupShowFunction::RunImpl() { |
| 78 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); | 80 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 79 const ListValue* args = args_as_list(); | 81 const ListValue* args = args_as_list(); |
| 80 | 82 |
| 81 std::string url_string; | 83 std::string url_string; |
| 82 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &url_string)); | 84 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, &url_string)); |
| 83 | 85 |
| 86 DictionaryValue* show_details = NULL; |
| 87 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &show_details)); |
| 88 |
| 84 DictionaryValue* dom_anchor = NULL; | 89 DictionaryValue* dom_anchor = NULL; |
| 85 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(1, &dom_anchor)); | 90 EXTENSION_FUNCTION_VALIDATE(show_details->GetDictionary(kDomAnchorKey, |
| 91 &dom_anchor)); |
| 86 | 92 |
| 87 int dom_top, dom_left; | 93 int dom_top, dom_left; |
| 88 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kTopKey, | 94 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kTopKey, |
| 89 &dom_top)); | 95 &dom_top)); |
| 90 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kLeftKey, | 96 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kLeftKey, |
| 91 &dom_left)); | 97 &dom_left)); |
| 92 | 98 |
| 93 int dom_width, dom_height; | 99 int dom_width, dom_height; |
| 94 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kWidthKey, | 100 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kWidthKey, |
| 95 &dom_width)); | 101 &dom_width)); |
| 96 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kHeightKey, | 102 EXTENSION_FUNCTION_VALIDATE(dom_anchor->GetInteger(kHeightKey, |
| 97 &dom_height)); | 103 &dom_height)); |
| 98 EXTENSION_FUNCTION_VALIDATE(dom_top >= 0 && dom_left >= 0 && | 104 EXTENSION_FUNCTION_VALIDATE(dom_top >= 0 && dom_left >= 0 && |
| 99 dom_width >= 0 && dom_height >= 0); | 105 dom_width >= 0 && dom_height >= 0); |
| 100 | 106 |
| 107 // The default behaviour is to give the focus to the pop-up window. |
| 108 bool give_focus = true; |
| 109 if (show_details->HasKey(kGiveFocusKey)) { |
| 110 EXTENSION_FUNCTION_VALIDATE(show_details->GetBoolean(kGiveFocusKey, |
| 111 &give_focus)); |
| 112 } |
| 113 |
| 101 GURL url = dispatcher()->url().Resolve(url_string); | 114 GURL url = dispatcher()->url().Resolve(url_string); |
| 102 if (!url.is_valid()) { | 115 if (!url.is_valid()) { |
| 103 error_ = kInvalidURLError; | 116 error_ = kInvalidURLError; |
| 104 return false; | 117 return false; |
| 105 } | 118 } |
| 106 | 119 |
| 107 // Disallow non-extension requests, or requests outside of the requesting | 120 // Disallow non-extension requests, or requests outside of the requesting |
| 108 // extension view's extension. | 121 // extension view's extension. |
| 109 const std::string& extension_id = url.host(); | 122 const std::string& extension_id = url.host(); |
| 110 if (extension_id != dispatcher()->GetExtension()->id() || | 123 if (extension_id != dispatcher()->GetExtension()->id() || |
| 111 !url.SchemeIs("chrome-extension")) { | 124 !url.SchemeIs("chrome-extension")) { |
| 112 error_ = kInvalidURLError; | 125 error_ = kInvalidURLError; |
| 113 return false; | 126 return false; |
| 114 } | 127 } |
| 115 | 128 |
| 116 #if defined(TOOLKIT_VIEWS) | 129 #if defined(TOOLKIT_VIEWS) |
| 117 gfx::Point origin(dom_left, dom_top); | 130 gfx::Point origin(dom_left, dom_top); |
| 118 if (!ConvertHostPointToScreen(&origin)) { | 131 if (!ConvertHostPointToScreen(&origin)) { |
| 119 error_ = kNotAnExtension; | 132 error_ = kNotAnExtension; |
| 120 return false; | 133 return false; |
| 121 } | 134 } |
| 122 gfx::Rect rect(origin.x(), origin.y(), dom_width, dom_height); | 135 gfx::Rect rect(origin.x(), origin.y(), dom_width, dom_height); |
| 123 | 136 |
| 124 // Pop-up from extension views (ExtensionShelf, etc.), and drop-down when | 137 // Pop-up from extension views (ExtensionShelf, etc.), and drop-down when |
| 125 // in a TabContents view. | 138 // in a TabContents view. |
| 126 BubbleBorder::ArrowLocation arrow_location = | 139 BubbleBorder::ArrowLocation arrow_location = |
| 127 (NULL != dispatcher()->GetExtensionHost()) ? BubbleBorder::BOTTOM_LEFT : | 140 (NULL != dispatcher()->GetExtensionHost()) ? BubbleBorder::BOTTOM_LEFT : |
| 128 BubbleBorder::TOP_LEFT; | 141 BubbleBorder::TOP_LEFT; |
| 129 popup_ = ExtensionPopup::Show(url, dispatcher()->GetBrowser(), rect, | 142 popup_ = ExtensionPopup::Show(url, dispatcher()->GetBrowser(), rect, |
| 130 arrow_location); | 143 arrow_location, give_focus); |
| 131 | 144 |
| 132 ExtensionPopupHost* popup_host = dispatcher()->GetPopupHost(); | 145 ExtensionPopupHost* popup_host = dispatcher()->GetPopupHost(); |
| 133 DCHECK(popup_host); | 146 DCHECK(popup_host); |
| 134 | 147 |
| 135 popup_host->set_child_popup(popup_); | 148 popup_host->set_child_popup(popup_); |
| 136 popup_->set_delegate(popup_host); | 149 popup_->set_delegate(popup_host); |
| 137 #endif // defined(TOOLKIT_VIEWS) | 150 #endif // defined(TOOLKIT_VIEWS) |
| 138 return true; | 151 return true; |
| 139 } | 152 } |
| 140 | 153 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 void PopupEventRouter::OnPopupClosed(Profile* profile, | 212 void PopupEventRouter::OnPopupClosed(Profile* profile, |
| 200 int routing_id) { | 213 int routing_id) { |
| 201 std::string full_event_name = StringPrintf( | 214 std::string full_event_name = StringPrintf( |
| 202 extension_popup_module_events::kOnPopupClosed, | 215 extension_popup_module_events::kOnPopupClosed, |
| 203 routing_id); | 216 routing_id); |
| 204 | 217 |
| 205 profile->GetExtensionMessageService()->DispatchEventToRenderers( | 218 profile->GetExtensionMessageService()->DispatchEventToRenderers( |
| 206 full_event_name, | 219 full_event_name, |
| 207 base::JSONWriter::kEmptyArray); | 220 base::JSONWriter::kEmptyArray); |
| 208 } | 221 } |
| OLD | NEW |