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