| 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/sidebar/sidebar_container.h" | |
| 6 | |
| 7 #include "chrome/browser/extensions/extension_service.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h" | |
| 10 #include "chrome/common/extensions/extension.h" | |
| 11 #include "chrome/common/extensions/extension_resource.h" | |
| 12 #include "chrome/common/extensions/extension_sidebar_defaults.h" | |
| 13 #include "chrome/common/extensions/extension_sidebar_utils.h" | |
| 14 #include "content/browser/renderer_host/render_view_host.h" | |
| 15 #include "content/browser/tab_contents/navigation_controller.h" | |
| 16 #include "content/browser/tab_contents/navigation_entry.h" | |
| 17 #include "content/browser/tab_contents/tab_contents.h" | |
| 18 #include "content/browser/tab_contents/tab_contents_view.h" | |
| 19 #include "content/public/browser/render_process_host.h" | |
| 20 #include "googleurl/src/gurl.h" | |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | |
| 22 | |
| 23 SidebarContainer::SidebarContainer(TabContents* tab, | |
| 24 const std::string& content_id, | |
| 25 Delegate* delegate) | |
| 26 : tab_(tab), | |
| 27 content_id_(content_id), | |
| 28 delegate_(delegate), | |
| 29 icon_(new SkBitmap), | |
| 30 navigate_to_default_page_on_expand_(true), | |
| 31 use_default_icon_(true) { | |
| 32 // Create TabContents for sidebar. | |
| 33 sidebar_contents_.reset( | |
| 34 new TabContents(Profile::FromBrowserContext(tab->browser_context()), | |
| 35 NULL, MSG_ROUTING_NONE, NULL, NULL)); | |
| 36 sidebar_contents_->SetDelegate(this); | |
| 37 } | |
| 38 | |
| 39 SidebarContainer::~SidebarContainer() { | |
| 40 } | |
| 41 | |
| 42 void SidebarContainer::SidebarClosing() { | |
| 43 delegate_->UpdateSidebar(this); | |
| 44 } | |
| 45 | |
| 46 void SidebarContainer::LoadDefaults() { | |
| 47 const Extension* extension = GetExtension(); | |
| 48 if (!extension) | |
| 49 return; // Can be NULL in tests. | |
| 50 const ExtensionSidebarDefaults* sidebar_defaults = | |
| 51 extension->sidebar_defaults(); | |
| 52 | |
| 53 title_ = sidebar_defaults->default_title(); | |
| 54 | |
| 55 if (!sidebar_defaults->default_icon_path().empty()) { | |
| 56 image_loading_tracker_.reset(new ImageLoadingTracker(this)); | |
| 57 image_loading_tracker_->LoadImage( | |
| 58 extension, | |
| 59 extension->GetResource(sidebar_defaults->default_icon_path()), | |
| 60 gfx::Size(Extension::kSidebarIconMaxSize, | |
| 61 Extension::kSidebarIconMaxSize), | |
| 62 ImageLoadingTracker::CACHE); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void SidebarContainer::Show() { | |
| 67 delegate_->UpdateSidebar(this); | |
| 68 } | |
| 69 | |
| 70 void SidebarContainer::Expand() { | |
| 71 if (navigate_to_default_page_on_expand_) { | |
| 72 navigate_to_default_page_on_expand_ = false; | |
| 73 // Check whether a default page is specified for this sidebar. | |
| 74 const Extension* extension = GetExtension(); | |
| 75 if (extension) { // Can be NULL in tests. | |
| 76 if (extension->sidebar_defaults()->default_page().is_valid()) | |
| 77 Navigate(extension->sidebar_defaults()->default_page()); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 delegate_->UpdateSidebar(this); | |
| 82 sidebar_contents_->view()->SetInitialFocus(); | |
| 83 } | |
| 84 | |
| 85 void SidebarContainer::Collapse() { | |
| 86 delegate_->UpdateSidebar(this); | |
| 87 } | |
| 88 | |
| 89 void SidebarContainer::Navigate(const GURL& url) { | |
| 90 // TODO(alekseys): add a progress UI. | |
| 91 navigate_to_default_page_on_expand_ = false; | |
| 92 sidebar_contents_->controller().LoadURL( | |
| 93 url, content::Referrer(), content::PAGE_TRANSITION_START_PAGE, | |
| 94 std::string()); | |
| 95 } | |
| 96 | |
| 97 void SidebarContainer::SetBadgeText(const string16& badge_text) { | |
| 98 badge_text_ = badge_text; | |
| 99 } | |
| 100 | |
| 101 void SidebarContainer::SetIcon(const SkBitmap& bitmap) { | |
| 102 use_default_icon_ = false; | |
| 103 *icon_ = bitmap; | |
| 104 } | |
| 105 | |
| 106 void SidebarContainer::SetTitle(const string16& title) { | |
| 107 title_ = title; | |
| 108 } | |
| 109 | |
| 110 content::JavaScriptDialogCreator* | |
| 111 SidebarContainer::GetJavaScriptDialogCreator() { | |
| 112 return GetJavaScriptDialogCreatorInstance(); | |
| 113 } | |
| 114 | |
| 115 void SidebarContainer::OnImageLoaded(SkBitmap* image, | |
| 116 const ExtensionResource& resource, | |
| 117 int index) { | |
| 118 if (image && use_default_icon_) { | |
| 119 *icon_ = *image; | |
| 120 delegate_->UpdateSidebar(this); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 const Extension* SidebarContainer::GetExtension() const { | |
| 125 Profile* profile = | |
| 126 Profile::FromBrowserContext(sidebar_contents_->browser_context()); | |
| 127 ExtensionService* service = profile->GetExtensionService(); | |
| 128 if (!service) | |
| 129 return NULL; | |
| 130 return service->GetExtensionById( | |
| 131 extension_sidebar_utils::GetExtensionIdByContentId(content_id_), false); | |
| 132 } | |
| OLD | NEW |