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