OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "content/browser/android/system_ui_resource_manager_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/location.h" | |
10 #include "base/threading/worker_pool.h" | |
11 #include "cc/resources/ui_resource_bitmap.h" | |
12 #include "content/browser/android/system_ui_resource.h" | |
13 #include "third_party/skia/include/core/SkBitmap.h" | |
14 #include "ui/base/android/system_ui_resource_layer.h" | |
15 #include "ui/base/android/ui_resource_client_android.h" | |
16 #include "ui/base/android/ui_resource_provider.h" | |
17 #include "ui/gfx/android/java_bitmap.h" | |
18 | |
19 namespace content { | |
20 | |
21 SystemUIResourceManagerImpl::SystemUIResourceManagerImpl( | |
22 ui::UIResourceProvider* ui_resource_provider) | |
23 : ui_resource_provider_(ui_resource_provider), weak_factory_(this) { | |
24 } | |
25 | |
26 void SystemUIResourceManagerImpl::Subscribe( | |
27 ui::SystemUIResourceManager::Type type, | |
28 ui::SystemUIResourceLayer* layer) { | |
29 DCHECK(layer); | |
30 GetData(type)->Subscribe(layer); | |
31 } | |
32 | |
33 void SystemUIResourceManagerImpl::Unsubscribe( | |
34 ui::SystemUIResourceManager::Type type, | |
35 ui::SystemUIResourceLayer* layer) { | |
36 DCHECK(layer); | |
37 GetData(type)->Unsubscribe(layer); | |
38 } | |
39 | |
40 void SystemUIResourceManagerImpl::RefreshResourceOnSubscribers( | |
41 ui::SystemUIResourceManager::Type type) { | |
42 GetData(type)->RefreshResource(); | |
43 } | |
44 | |
45 SystemUIResourceManagerImpl::Data* SystemUIResourceManagerImpl::GetData( | |
46 ui::SystemUIResourceManager::Type type) { | |
47 SystemUIResourceMap::iterator iter = resource_map_.find(type); | |
48 if (iter == resource_map_.end()) { | |
49 scoped_ptr<Data> resource_data = make_scoped_ptr(new Data()); | |
50 resource_map_.set(type, resource_data.Pass()); | |
51 | |
52 // Lazily build the resource. | |
53 BuildResource(type); | |
54 } | |
55 return resource_map_.get(type); | |
56 } | |
57 | |
58 void SystemUIResourceManagerImpl::BuildResource( | |
59 ui::SystemUIResourceManager::Type type) { | |
60 if (GetData(type)->HasResource()) | |
jdduke (slow)
2014/07/16 19:01:51
Should this be DCHECK(!GetData(type)->HasResource(
powei
2014/07/16 23:33:51
Done.
| |
61 return; | |
62 | |
63 // Instead of blocking the main thread, we post a task to load the bitmap. | |
64 SkBitmap* bitmap = new SkBitmap(); | |
65 base::Closure load_bitmap = | |
66 base::Bind(&SystemUIResourceManagerImpl::LoadBitmap, type, bitmap); | |
67 base::Closure finished_load = | |
68 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, | |
69 weak_factory_.GetWeakPtr(), | |
70 type, | |
71 base::Owned(bitmap)); | |
72 base::WorkerPool::PostTaskAndReply( | |
73 FROM_HERE, load_bitmap, finished_load, true); | |
74 } | |
75 | |
76 void SystemUIResourceManagerImpl::LoadBitmap( | |
77 ui::SystemUIResourceManager::Type type, | |
78 SkBitmap* bitmap_holder) { | |
79 SkBitmap bitmap; | |
80 switch (type) { | |
81 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: | |
82 bitmap = gfx::CreateSkBitmapFromAndroidResource( | |
83 "android:drawable/overscroll_edge", gfx::Size(128, 12)); | |
84 break; | |
85 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: | |
86 bitmap = gfx::CreateSkBitmapFromAndroidResource( | |
87 "android:drawable/overscroll_glow", gfx::Size(128, 64)); | |
88 break; | |
89 } | |
90 bitmap.setImmutable(); | |
91 *bitmap_holder = bitmap; | |
92 } | |
93 | |
94 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( | |
95 ui::SystemUIResourceManager::Type type, | |
96 SkBitmap* bitmap_holder) { | |
97 scoped_ptr<SystemUIResource> resource = | |
98 SystemUIResource::Create(*bitmap_holder, ui_resource_provider_); | |
99 GetData(type)->SetResource(resource.Pass()); | |
100 } | |
101 | |
102 SystemUIResourceManagerImpl::Data::Data() { | |
103 } | |
104 | |
105 SystemUIResourceManagerImpl::Data::~Data() { | |
106 if (resource_) | |
107 resource_->SetClient(NULL); | |
108 } | |
109 | |
110 void SystemUIResourceManagerImpl::Data::Subscribe( | |
111 ui::SystemUIResourceLayer* layer) { | |
112 DCHECK(layer); | |
113 if (resource_) | |
114 layer->SetUIResourceId(resource_->id()); | |
115 | |
116 if (!subscribers_.HasObserver(layer)) | |
117 subscribers_.AddObserver(layer); | |
118 } | |
119 | |
120 void SystemUIResourceManagerImpl::Data::Unsubscribe( | |
121 ui::SystemUIResourceLayer* layer) { | |
122 DCHECK(layer); | |
123 layer->SetUIResourceId(0); | |
124 subscribers_.RemoveObserver(layer); | |
125 } | |
126 | |
127 void SystemUIResourceManagerImpl::Data::SetResource( | |
128 scoped_ptr<SystemUIResource> resource) { | |
129 DCHECK(resource); | |
jdduke (slow)
2014/07/16 19:01:51
also DCHECK(!resource_)?
powei
2014/07/16 23:33:51
Done.
| |
130 resource_ = resource.Pass(); | |
131 resource_->SetClient(this); | |
132 | |
133 RefreshResource(); | |
134 } | |
135 | |
136 void SystemUIResourceManagerImpl::Data::RefreshResource() { | |
137 if (!resource_) | |
138 return; | |
139 | |
140 resource_->Load(); | |
141 FOR_EACH_OBSERVER(ui::SystemUIResourceLayer, | |
jdduke (slow)
2014/07/16 19:01:51
Shouldn't the loading trigger a call to |OnUIResou
powei
2014/07/16 23:33:51
Done. Now this only happens when the LTH returns.
| |
142 subscribers_, | |
143 SetUIResourceId(resource_->id())); | |
144 } | |
145 | |
146 void SystemUIResourceManagerImpl::Data::OnUIResourceIdChanged() { | |
147 DCHECK(resource_); | |
148 | |
149 FOR_EACH_OBSERVER(ui::SystemUIResourceLayer, | |
150 subscribers_, | |
151 SetUIResourceId(resource_->id())); | |
152 } | |
153 | |
154 } // namespace content | |
OLD | NEW |