| 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_actions_api.h
" | 5 #include "chrome/browser/extensions/api/extension_action/extension_actions_api.h
" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #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" |
| 14 #include "chrome/browser/extensions/extension_action.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/extension_system.h" | 16 #include "chrome/browser/extensions/extension_system.h" |
| 16 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
| 17 #include "chrome/browser/extensions/location_bar_controller.h" | 18 #include "chrome/browser/extensions/location_bar_controller.h" |
| 18 #include "chrome/browser/extensions/state_store.h" | 19 #include "chrome/browser/extensions/state_store.h" |
| 19 #include "chrome/browser/extensions/tab_helper.h" | 20 #include "chrome/browser/extensions/tab_helper.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 22 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 23 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
| 24 #include "chrome/common/extensions/extension_action.h" | |
| 25 #include "chrome/common/extensions/extension_error_utils.h" | 25 #include "chrome/common/extensions/extension_error_utils.h" |
| 26 #include "chrome/common/render_messages.h" | 26 #include "chrome/common/render_messages.h" |
| 27 #include "content/public/browser/navigation_entry.h" | 27 #include "content/public/browser/navigation_entry.h" |
| 28 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 const char kBrowserActionStorageKey[] = "browser_action"; | 32 const char kBrowserActionStorageKey[] = "browser_action"; |
| 33 const char kPopupUrlStorageKey[] = "poupup_url"; | 33 const char kPopupUrlStorageKey[] = "poupup_url"; |
| 34 const char kTitleStorageKey[] = "title"; | 34 const char kTitleStorageKey[] = "title"; |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 233 |
| 234 scoped_ptr<base::DictionaryValue> defaults = | 234 scoped_ptr<base::DictionaryValue> defaults = |
| 235 DefaultsToValue(extension_action); | 235 DefaultsToValue(extension_action); |
| 236 storage->SetExtensionValue(extension_action->extension_id(), | 236 storage->SetExtensionValue(extension_action->extension_id(), |
| 237 kBrowserActionStorageKey, | 237 kBrowserActionStorageKey, |
| 238 defaults.PassAs<base::Value>()); | 238 defaults.PassAs<base::Value>()); |
| 239 } | 239 } |
| 240 | 240 |
| 241 void ExtensionActionStorageManager::ReadFromStorage( | 241 void ExtensionActionStorageManager::ReadFromStorage( |
| 242 const std::string& extension_id, scoped_ptr<base::Value> value) { | 242 const std::string& extension_id, scoped_ptr<base::Value> value) { |
| 243 const Extension* extension = | 243 const ExtensionService* service = |
| 244 ExtensionSystem::Get(profile_)->extension_service()-> | 244 ExtensionSystem::Get(profile_)->extension_service(); |
| 245 GetExtensionById(extension_id, true); | 245 const Extension* extension = service->GetExtensionById(extension_id, true); |
| 246 if (!extension) | 246 if (!extension) |
| 247 return; | 247 return; |
| 248 | 248 |
| 249 CHECK(extension->browser_action()); | 249 ExtensionAction* browser_action = service->GetBrowserAction(*extension); |
| 250 CHECK(browser_action); |
| 250 | 251 |
| 251 // Don't load values from storage if the extension has updated a value | 252 // Don't load values from storage if the extension has updated a value |
| 252 // already. The extension may have only updated some of the values, but | 253 // already. The extension may have only updated some of the values, but |
| 253 // this is a good first approximation. If the extension is doing stuff | 254 // this is a good first approximation. If the extension is doing stuff |
| 254 // to the browser action, we can assume it is ready to take over. | 255 // to the browser action, we can assume it is ready to take over. |
| 255 if (extension->browser_action()->has_changed()) | 256 if (browser_action->has_changed()) |
| 256 return; | 257 return; |
| 257 | 258 |
| 258 const base::DictionaryValue* dict = NULL; | 259 const base::DictionaryValue* dict = NULL; |
| 259 if (!value.get() || !value->GetAsDictionary(&dict)) | 260 if (!value.get() || !value->GetAsDictionary(&dict)) |
| 260 return; | 261 return; |
| 261 | 262 |
| 262 SetDefaultsFromValue(dict, extension->browser_action()); | 263 SetDefaultsFromValue(dict, browser_action); |
| 263 } | 264 } |
| 264 | 265 |
| 265 } // namespace extensions | 266 } // namespace extensions |
| 266 | 267 |
| 267 | 268 |
| 268 // | 269 // |
| 269 // ExtensionActionFunction | 270 // ExtensionActionFunction |
| 270 // | 271 // |
| 271 | 272 |
| 272 ExtensionActionFunction::ExtensionActionFunction() | 273 ExtensionActionFunction::ExtensionActionFunction() |
| 273 : details_(NULL), | 274 : details_(NULL), |
| 274 tab_id_(ExtensionAction::kDefaultTabId), | 275 tab_id_(ExtensionAction::kDefaultTabId), |
| 275 contents_(NULL), | 276 contents_(NULL), |
| 276 extension_action_(NULL) { | 277 extension_action_(NULL) { |
| 277 } | 278 } |
| 278 | 279 |
| 279 ExtensionActionFunction::~ExtensionActionFunction() { | 280 ExtensionActionFunction::~ExtensionActionFunction() { |
| 280 } | 281 } |
| 281 | 282 |
| 282 bool ExtensionActionFunction::RunImpl() { | 283 bool ExtensionActionFunction::RunImpl() { |
| 283 if (base::StringPiece(name()).starts_with("scriptBadge.")) { | 284 if (base::StringPiece(name()).starts_with("scriptBadge.")) { |
| 284 extension_action_ = GetExtension()->script_badge(); | 285 extension_action_ = GetScriptBadge(profile(), *GetExtension()); |
| 285 } else { | 286 } else { |
| 286 extension_action_ = GetExtension()->browser_action(); | 287 extension_action_ = GetBrowserAction(profile(), *GetExtension()); |
| 287 if (!extension_action_) | 288 if (!extension_action_) |
| 288 extension_action_ = GetExtension()->page_action(); | 289 extension_action_ = GetPageAction(profile(), *GetExtension()); |
| 289 } | 290 } |
| 290 if (!extension_action_) { | 291 if (!extension_action_) { |
| 291 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event | 292 // TODO(kalman): ideally the browserAction/pageAction APIs wouldn't event |
| 292 // exist for extensions that don't have one declared. This should come as | 293 // exist for extensions that don't have one declared. This should come as |
| 293 // part of the Feature system. | 294 // part of the Feature system. |
| 294 error_ = kNoExtensionActionError; | 295 error_ = kNoExtensionActionError; |
| 295 return false; | 296 return false; |
| 296 } | 297 } |
| 297 | 298 |
| 298 // There may or may not be details (depends on the function). | 299 // There may or may not be details (depends on the function). |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 error_ = ExtensionErrorUtils::FormatErrorMessage( | 346 error_ = ExtensionErrorUtils::FormatErrorMessage( |
| 346 kNoTabError, base::IntToString(tab_id_)); | 347 kNoTabError, base::IntToString(tab_id_)); |
| 347 return false; | 348 return false; |
| 348 } | 349 } |
| 349 } | 350 } |
| 350 | 351 |
| 351 return RunExtensionAction(); | 352 return RunExtensionAction(); |
| 352 } | 353 } |
| 353 | 354 |
| 354 void ExtensionActionFunction::NotifyChange() { | 355 void ExtensionActionFunction::NotifyChange() { |
| 356 typedef extensions::Extension::ActionInfo ActionInfo; |
| 355 switch (extension_action_->action_type()) { | 357 switch (extension_action_->action_type()) { |
| 356 case ExtensionAction::TYPE_BROWSER: | 358 case ActionInfo::TYPE_BROWSER: |
| 357 case ExtensionAction::TYPE_PAGE: | 359 case ActionInfo::TYPE_PAGE: |
| 358 if (extension_->browser_action()) { | 360 if (extension_->browser_action()) { |
| 359 NotifyBrowserActionChange(); | 361 NotifyBrowserActionChange(); |
| 360 } else if (extension_->page_action()) { | 362 } else if (extension_->page_action()) { |
| 361 NotifyLocationBarChange(); | 363 NotifyLocationBarChange(); |
| 362 } | 364 } |
| 363 return; | 365 return; |
| 364 case ExtensionAction::TYPE_SCRIPT_BADGE: | 366 case ActionInfo::TYPE_SCRIPT_BADGE: |
| 365 NotifyLocationBarChange(); | 367 NotifyLocationBarChange(); |
| 366 return; | 368 return; |
| 367 } | 369 } |
| 368 NOTREACHED(); | 370 NOTREACHED(); |
| 369 } | 371 } |
| 370 | 372 |
| 371 void ExtensionActionFunction::NotifyBrowserActionChange() { | 373 void ExtensionActionFunction::NotifyBrowserActionChange() { |
| 372 content::NotificationService::current()->Notify( | 374 content::NotificationService::current()->Notify( |
| 373 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 375 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
| 374 content::Source<ExtensionAction>(extension_action_), | 376 content::Source<ExtensionAction>(extension_action_), |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { | 555 bool ExtensionActionGetBadgeBackgroundColorFunction::RunExtensionAction() { |
| 554 ListValue* list = new ListValue(); | 556 ListValue* list = new ListValue(); |
| 555 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); | 557 SkColor color = extension_action_->GetBadgeBackgroundColor(tab_id_); |
| 556 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); | 558 list->Append(Value::CreateIntegerValue(SkColorGetR(color))); |
| 557 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); | 559 list->Append(Value::CreateIntegerValue(SkColorGetG(color))); |
| 558 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); | 560 list->Append(Value::CreateIntegerValue(SkColorGetB(color))); |
| 559 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); | 561 list->Append(Value::CreateIntegerValue(SkColorGetA(color))); |
| 560 SetResult(list); | 562 SetResult(list); |
| 561 return true; | 563 return true; |
| 562 } | 564 } |
| OLD | NEW |