| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_sidebar_api.h" | 5 #include "chrome/browser/extensions/extension_sidebar_api.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/string16.h" | 10 #include "base/string16.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/extension_event_router.h" | 12 #include "chrome/browser/extensions/extension_event_router.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_tabs_module.h" | 14 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/sidebar/sidebar_container.h" | 16 #include "chrome/browser/sidebar/sidebar_container.h" |
| 17 #include "chrome/browser/sidebar/sidebar_manager.h" | 17 #include "chrome/browser/sidebar/sidebar_manager.h" |
| 18 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 20 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
| 22 #include "chrome/common/extensions/extension_error_utils.h" | 21 #include "chrome/common/extensions/extension_error_utils.h" |
| 23 #include "chrome/common/extensions/extension_sidebar_utils.h" | 22 #include "chrome/common/extensions/extension_sidebar_utils.h" |
| 24 #include "chrome/common/render_messages.h" | 23 #include "chrome/common/render_messages.h" |
| 25 #include "ipc/ipc_message_utils.h" | 24 #include "ipc/ipc_message_utils.h" |
| 26 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 47 | 46 |
| 48 namespace extension_sidebar_constants { | 47 namespace extension_sidebar_constants { |
| 49 // Sidebar states. | 48 // Sidebar states. |
| 50 const char kActiveState[] = "active"; | 49 const char kActiveState[] = "active"; |
| 51 const char kHiddenState[] = "hidden"; | 50 const char kHiddenState[] = "hidden"; |
| 52 const char kShownState[] = "shown"; | 51 const char kShownState[] = "shown"; |
| 53 } // namespace extension_sidebar_constants | 52 } // namespace extension_sidebar_constants |
| 54 | 53 |
| 55 // static | 54 // static |
| 56 void ExtensionSidebarEventRouter::OnStateChanged( | 55 void ExtensionSidebarEventRouter::OnStateChanged( |
| 57 Profile* profile, TabContents* tab, const std::string& content_id, | 56 Profile* profile, |
| 57 TabContentsWrapper* tab, |
| 58 const std::string& content_id, |
| 58 const std::string& state) { | 59 const std::string& state) { |
| 59 int tab_id = ExtensionTabUtil::GetTabId(tab); | 60 int tab_id = ExtensionTabUtil::GetTabId(tab->tab_contents()); |
| 60 DictionaryValue* details = new DictionaryValue; | 61 DictionaryValue* details = new DictionaryValue; |
| 61 details->Set(kTabIdKey, Value::CreateIntegerValue(tab_id)); | 62 details->Set(kTabIdKey, Value::CreateIntegerValue(tab_id)); |
| 62 details->Set(kStateKey, Value::CreateStringValue(state)); | 63 details->Set(kStateKey, Value::CreateStringValue(state)); |
| 63 | 64 |
| 64 ListValue args; | 65 ListValue args; |
| 65 args.Set(0, details); | 66 args.Set(0, details); |
| 66 std::string json_args; | 67 std::string json_args; |
| 67 base::JSONWriter::Write(&args, false, &json_args); | 68 base::JSONWriter::Write(&args, false, &json_args); |
| 68 | 69 |
| 69 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 70 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 123 } |
| 123 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) { | 124 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) { |
| 124 error_ = kNoDefaultTabError; | 125 error_ = kNoDefaultTabError; |
| 125 return false; | 126 return false; |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 if (!tab_contents) | 129 if (!tab_contents) |
| 129 return false; | 130 return false; |
| 130 | 131 |
| 131 std::string content_id(GetExtension()->id()); | 132 std::string content_id(GetExtension()->id()); |
| 132 return RunImpl(tab_contents->tab_contents(), content_id, *details); | 133 return RunImpl(tab_contents, content_id, *details); |
| 133 } | 134 } |
| 134 | 135 |
| 135 | 136 |
| 136 bool CollapseSidebarFunction::RunImpl(TabContents* tab, | 137 bool CollapseSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 137 const std::string& content_id, | 138 const std::string& content_id, |
| 138 const DictionaryValue& details) { | 139 const DictionaryValue& details) { |
| 139 SidebarManager::GetInstance()->CollapseSidebar(tab, content_id); | 140 SidebarManager::GetInstance()->CollapseSidebar(tab, content_id); |
| 140 return true; | 141 return true; |
| 141 } | 142 } |
| 142 | 143 |
| 143 bool ExpandSidebarFunction::RunImpl(TabContents* tab, | 144 bool ExpandSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 144 const std::string& content_id, | 145 const std::string& content_id, |
| 145 const DictionaryValue& details) { | 146 const DictionaryValue& details) { |
| 146 // TODO(alekseys): enable this check back when WebKit's user gesture flag | 147 // TODO(alekseys): enable this check back when WebKit's user gesture flag |
| 147 // reporting for extension calls is fixed. | 148 // reporting for extension calls is fixed. |
| 148 // if (!user_gesture()) { | 149 // if (!user_gesture()) { |
| 149 // error_ = kInvalidExpandContextError; | 150 // error_ = kInvalidExpandContextError; |
| 150 // return false; | 151 // return false; |
| 151 // } | 152 // } |
| 152 SidebarManager::GetInstance()->ExpandSidebar(tab, content_id); | 153 SidebarManager::GetInstance()->ExpandSidebar(tab, content_id); |
| 153 return true; | 154 return true; |
| 154 } | 155 } |
| 155 | 156 |
| 156 bool GetStateSidebarFunction::RunImpl(TabContents* tab, | 157 bool GetStateSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 157 const std::string& content_id, | 158 const std::string& content_id, |
| 158 const DictionaryValue& details) { | 159 const DictionaryValue& details) { |
| 159 SidebarManager* manager = SidebarManager::GetInstance(); | 160 SidebarManager* manager = SidebarManager::GetInstance(); |
| 160 | 161 |
| 161 const char* result = extension_sidebar_constants::kHiddenState; | 162 const char* result = extension_sidebar_constants::kHiddenState; |
| 162 if (manager->GetSidebarTabContents(tab, content_id)) { | 163 if (manager->GetSidebarTabContents(tab, content_id)) { |
| 163 bool is_active = false; | 164 bool is_active = false; |
| 164 // Sidebar is considered active only if tab is selected, sidebar UI | 165 // Sidebar is considered active only if tab is selected, sidebar UI |
| 165 // is expanded and this extension's content is displayed on it. | 166 // is expanded and this extension's content is displayed on it. |
| 166 SidebarContainer* active_sidebar = | 167 SidebarContainer* active_sidebar = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 187 } | 188 } |
| 188 | 189 |
| 189 result = is_active ? extension_sidebar_constants::kActiveState : | 190 result = is_active ? extension_sidebar_constants::kActiveState : |
| 190 extension_sidebar_constants::kShownState; | 191 extension_sidebar_constants::kShownState; |
| 191 } | 192 } |
| 192 | 193 |
| 193 result_.reset(Value::CreateStringValue(result)); | 194 result_.reset(Value::CreateStringValue(result)); |
| 194 return true; | 195 return true; |
| 195 } | 196 } |
| 196 | 197 |
| 197 bool HideSidebarFunction::RunImpl(TabContents* tab, | 198 bool HideSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 198 const std::string& content_id, | 199 const std::string& content_id, |
| 199 const DictionaryValue& details) { | 200 const DictionaryValue& details) { |
| 200 SidebarManager::GetInstance()->HideSidebar(tab, content_id); | 201 SidebarManager::GetInstance()->HideSidebar(tab, content_id); |
| 201 return true; | 202 return true; |
| 202 } | 203 } |
| 203 | 204 |
| 204 bool NavigateSidebarFunction::RunImpl(TabContents* tab, | 205 bool NavigateSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 205 const std::string& content_id, | 206 const std::string& content_id, |
| 206 const DictionaryValue& details) { | 207 const DictionaryValue& details) { |
| 207 std::string path_string; | 208 std::string path_string; |
| 208 EXTENSION_FUNCTION_VALIDATE(details.GetString(kPathKey, &path_string)); | 209 EXTENSION_FUNCTION_VALIDATE(details.GetString(kPathKey, &path_string)); |
| 209 | 210 |
| 210 GURL url = extension_sidebar_utils::ResolveRelativePath( | 211 GURL url = extension_sidebar_utils::ResolveRelativePath( |
| 211 path_string, GetExtension(), &error_); | 212 path_string, GetExtension(), &error_); |
| 212 if (!url.is_valid()) | 213 if (!url.is_valid()) |
| 213 return false; | 214 return false; |
| 214 | 215 |
| 215 SidebarManager::GetInstance()->NavigateSidebar(tab, content_id, url); | 216 SidebarManager::GetInstance()->NavigateSidebar(tab, content_id, url); |
| 216 return true; | 217 return true; |
| 217 } | 218 } |
| 218 | 219 |
| 219 bool SetBadgeTextSidebarFunction::RunImpl(TabContents* tab, | 220 bool SetBadgeTextSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 220 const std::string& content_id, | 221 const std::string& content_id, |
| 221 const DictionaryValue& details) { | 222 const DictionaryValue& details) { |
| 222 string16 badge_text; | 223 string16 badge_text; |
| 223 EXTENSION_FUNCTION_VALIDATE(details.GetString(kBadgeTextKey, &badge_text)); | 224 EXTENSION_FUNCTION_VALIDATE(details.GetString(kBadgeTextKey, &badge_text)); |
| 224 SidebarManager::GetInstance()->SetSidebarBadgeText( | 225 SidebarManager::GetInstance()->SetSidebarBadgeText( |
| 225 tab, content_id, badge_text); | 226 tab, content_id, badge_text); |
| 226 return true; | 227 return true; |
| 227 } | 228 } |
| 228 | 229 |
| 229 bool SetIconSidebarFunction::RunImpl(TabContents* tab, | 230 bool SetIconSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 230 const std::string& content_id, | 231 const std::string& content_id, |
| 231 const DictionaryValue& details) { | 232 const DictionaryValue& details) { |
| 232 BinaryValue* binary; | 233 BinaryValue* binary; |
| 233 EXTENSION_FUNCTION_VALIDATE(details.GetBinary(kImageDataKey, &binary)); | 234 EXTENSION_FUNCTION_VALIDATE(details.GetBinary(kImageDataKey, &binary)); |
| 234 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); | 235 IPC::Message bitmap_pickle(binary->GetBuffer(), binary->GetSize()); |
| 235 void* iter = NULL; | 236 void* iter = NULL; |
| 236 scoped_ptr<SkBitmap> bitmap(new SkBitmap); | 237 scoped_ptr<SkBitmap> bitmap(new SkBitmap); |
| 237 EXTENSION_FUNCTION_VALIDATE( | 238 EXTENSION_FUNCTION_VALIDATE( |
| 238 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); | 239 IPC::ReadParam(&bitmap_pickle, &iter, bitmap.get())); |
| 239 SidebarManager::GetInstance()->SetSidebarIcon(tab, content_id, *bitmap); | 240 SidebarManager::GetInstance()->SetSidebarIcon(tab, content_id, *bitmap); |
| 240 return true; | 241 return true; |
| 241 } | 242 } |
| 242 | 243 |
| 243 bool SetTitleSidebarFunction::RunImpl(TabContents* tab, | 244 bool SetTitleSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 244 const std::string& content_id, | 245 const std::string& content_id, |
| 245 const DictionaryValue& details) { | 246 const DictionaryValue& details) { |
| 246 string16 title; | 247 string16 title; |
| 247 EXTENSION_FUNCTION_VALIDATE(details.GetString(kTitleKey, &title)); | 248 EXTENSION_FUNCTION_VALIDATE(details.GetString(kTitleKey, &title)); |
| 248 SidebarManager::GetInstance()->SetSidebarTitle(tab, content_id, title); | 249 SidebarManager::GetInstance()->SetSidebarTitle(tab, content_id, title); |
| 249 return true; | 250 return true; |
| 250 } | 251 } |
| 251 | 252 |
| 252 bool ShowSidebarFunction::RunImpl(TabContents* tab, | 253 bool ShowSidebarFunction::RunImpl(TabContentsWrapper* tab, |
| 253 const std::string& content_id, | 254 const std::string& content_id, |
| 254 const DictionaryValue& details) { | 255 const DictionaryValue& details) { |
| 255 SidebarManager::GetInstance()->ShowSidebar(tab, content_id); | 256 SidebarManager::GetInstance()->ShowSidebar(tab, content_id); |
| 256 return true; | 257 return true; |
| 257 } | 258 } |
| OLD | NEW |