| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_tab_helper.h" |
| 6 |
| 7 #include "chrome/browser/extensions/extension_message_service.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/extensions/extension_action.h" |
| 11 #include "chrome/common/extensions/extension_icon_set.h" |
| 12 #include "chrome/common/extensions/extension_messages.h" |
| 13 #include "chrome/common/extensions/extension_resource.h" |
| 14 #include "content/browser/tab_contents/tab_contents.h" |
| 15 #include "content/browser/tab_contents/navigation_controller.h" |
| 16 #include "content/common/notification_service.h" |
| 17 |
| 18 ExtensionTabHelper::ExtensionTabHelper(TabContents* tab_contents) |
| 19 : TabContentsObserver(tab_contents), |
| 20 extension_app_(NULL) { |
| 21 } |
| 22 |
| 23 ExtensionTabHelper::~ExtensionTabHelper() { |
| 24 } |
| 25 |
| 26 void ExtensionTabHelper::CopyStateFrom(const ExtensionTabHelper& source) { |
| 27 SetExtensionApp(source.extension_app()); |
| 28 extension_app_icon_ = source.extension_app_icon_; |
| 29 } |
| 30 |
| 31 void ExtensionTabHelper::PageActionStateChanged() { |
| 32 tab_contents()->NotifyNavigationStateChanged( |
| 33 TabContents::INVALIDATE_PAGE_ACTIONS); |
| 34 } |
| 35 |
| 36 void ExtensionTabHelper::SetExtensionApp(const Extension* extension) { |
| 37 DCHECK(!extension || extension->GetFullLaunchURL().is_valid()); |
| 38 extension_app_ = extension; |
| 39 |
| 40 UpdateExtensionAppIcon(extension_app_); |
| 41 |
| 42 NotificationService::current()->Notify( |
| 43 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED, |
| 44 Source<ExtensionTabHelper>(this), |
| 45 NotificationService::NoDetails()); |
| 46 } |
| 47 |
| 48 void ExtensionTabHelper::SetExtensionAppById( |
| 49 const std::string& extension_app_id) { |
| 50 if (extension_app_id.empty()) |
| 51 return; |
| 52 |
| 53 ExtensionService* extension_service = |
| 54 tab_contents()->profile()->GetExtensionService(); |
| 55 if (!extension_service || !extension_service->is_ready()) |
| 56 return; |
| 57 |
| 58 const Extension* extension = |
| 59 extension_service->GetExtensionById(extension_app_id, false); |
| 60 if (extension) |
| 61 SetExtensionApp(extension); |
| 62 } |
| 63 |
| 64 SkBitmap* ExtensionTabHelper::GetExtensionAppIcon() { |
| 65 if (extension_app_icon_.empty()) |
| 66 return NULL; |
| 67 |
| 68 return &extension_app_icon_; |
| 69 } |
| 70 |
| 71 void ExtensionTabHelper::DidNavigateMainFramePostCommit( |
| 72 const NavigationController::LoadCommittedDetails& details, |
| 73 const ViewHostMsg_FrameNavigate_Params& params) { |
| 74 if (details.is_in_page) |
| 75 return; |
| 76 |
| 77 ExtensionService* service = tab_contents()->profile()->GetExtensionService(); |
| 78 if (!service) |
| 79 return; |
| 80 |
| 81 for (size_t i = 0; i < service->extensions()->size(); ++i) { |
| 82 ExtensionAction* browser_action = |
| 83 service->extensions()->at(i)->browser_action(); |
| 84 if (browser_action) { |
| 85 browser_action->ClearAllValuesForTab( |
| 86 tab_contents()->controller().session_id().id()); |
| 87 NotificationService::current()->Notify( |
| 88 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 89 Source<ExtensionAction>(browser_action), |
| 90 NotificationService::NoDetails()); |
| 91 } |
| 92 |
| 93 ExtensionAction* page_action = |
| 94 service->extensions()->at(i)->page_action(); |
| 95 if (page_action) { |
| 96 page_action->ClearAllValuesForTab( |
| 97 tab_contents()->controller().session_id().id()); |
| 98 PageActionStateChanged(); |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 bool ExtensionTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 104 bool handled = true; |
| 105 IPC_BEGIN_MESSAGE_MAP(ExtensionTabHelper, message) |
| 106 IPC_MESSAGE_HANDLER(ExtensionHostMsg_PostMessage, OnPostMessage) |
| 107 IPC_MESSAGE_UNHANDLED(handled = false) |
| 108 IPC_END_MESSAGE_MAP() |
| 109 return handled; |
| 110 } |
| 111 |
| 112 void ExtensionTabHelper::UpdateExtensionAppIcon(const Extension* extension) { |
| 113 extension_app_icon_.reset(); |
| 114 |
| 115 if (extension) { |
| 116 extension_app_image_loader_.reset(new ImageLoadingTracker(this)); |
| 117 extension_app_image_loader_->LoadImage( |
| 118 extension, |
| 119 extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH, |
| 120 ExtensionIconSet::MATCH_EXACTLY), |
| 121 gfx::Size(Extension::EXTENSION_ICON_SMALLISH, |
| 122 Extension::EXTENSION_ICON_SMALLISH), |
| 123 ImageLoadingTracker::CACHE); |
| 124 } else { |
| 125 extension_app_image_loader_.reset(NULL); |
| 126 } |
| 127 } |
| 128 |
| 129 void ExtensionTabHelper::OnImageLoaded(SkBitmap* image, |
| 130 const ExtensionResource& resource, |
| 131 int index) { |
| 132 if (image) { |
| 133 extension_app_icon_ = *image; |
| 134 tab_contents()->NotifyNavigationStateChanged(TabContents::INVALIDATE_TAB); |
| 135 } |
| 136 } |
| 137 |
| 138 void ExtensionTabHelper::OnPostMessage(int port_id, |
| 139 const std::string& message) { |
| 140 if (tab_contents()->profile()->GetExtensionMessageService()) { |
| 141 tab_contents()->profile()->GetExtensionMessageService()-> |
| 142 PostMessageFromRenderer(port_id, message); |
| 143 } |
| 144 } |
| OLD | NEW |