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