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