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 SystemUIResourceManagerImpl::SystemUIResourceManagerImpl( |
| 22 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::RefreshResources() { |
| 41 GetData(ui::SystemUIResourceManager::OVERSCROLL_GLOW)->RefreshResource(); |
| 42 GetData(ui::SystemUIResourceManager::OVERSCROLL_EDGE)->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 DCHECK(!GetData(type)->HasResource()); |
| 61 |
| 62 // Instead of blocking the main thread, we post a task to load the bitmap. |
| 63 SkBitmap* bitmap = new SkBitmap(); |
| 64 base::Closure load_bitmap = |
| 65 base::Bind(&SystemUIResourceManagerImpl::LoadBitmap, type, bitmap); |
| 66 base::Closure finished_load = |
| 67 base::Bind(&SystemUIResourceManagerImpl::OnFinishedLoadBitmap, |
| 68 weak_factory_.GetWeakPtr(), |
| 69 type, |
| 70 base::Owned(bitmap)); |
| 71 base::WorkerPool::PostTaskAndReply( |
| 72 FROM_HERE, load_bitmap, finished_load, true); |
| 73 } |
| 74 |
| 75 void SystemUIResourceManagerImpl::LoadBitmap( |
| 76 ui::SystemUIResourceManager::Type type, |
| 77 SkBitmap* bitmap_holder) { |
| 78 SkBitmap bitmap; |
| 79 switch (type) { |
| 80 case ui::SystemUIResourceManager::OVERSCROLL_EDGE: |
| 81 bitmap = gfx::CreateSkBitmapFromAndroidResource( |
| 82 "android:drawable/overscroll_edge", gfx::Size(128, 12)); |
| 83 break; |
| 84 case ui::SystemUIResourceManager::OVERSCROLL_GLOW: |
| 85 bitmap = gfx::CreateSkBitmapFromAndroidResource( |
| 86 "android:drawable/overscroll_glow", gfx::Size(128, 64)); |
| 87 break; |
| 88 } |
| 89 bitmap.setImmutable(); |
| 90 *bitmap_holder = bitmap; |
| 91 } |
| 92 |
| 93 void SystemUIResourceManagerImpl::OnFinishedLoadBitmap( |
| 94 ui::SystemUIResourceManager::Type type, |
| 95 SkBitmap* bitmap_holder) { |
| 96 scoped_ptr<SystemUIResource> resource = |
| 97 SystemUIResource::Create(*bitmap_holder, ui_resource_provider_); |
| 98 GetData(type)->SetResource(resource.Pass()); |
| 99 } |
| 100 |
| 101 SystemUIResourceManagerImpl::Data::Data() { |
| 102 } |
| 103 |
| 104 SystemUIResourceManagerImpl::Data::~Data() { |
| 105 if (resource_) |
| 106 resource_->SetClient(NULL); |
| 107 } |
| 108 |
| 109 void SystemUIResourceManagerImpl::Data::Subscribe( |
| 110 ui::SystemUIResourceLayer* layer) { |
| 111 DCHECK(layer); |
| 112 if (resource_) |
| 113 layer->SetUIResourceId(resource_->id()); |
| 114 |
| 115 if (!subscribers_.HasObserver(layer)) |
| 116 subscribers_.AddObserver(layer); |
| 117 } |
| 118 |
| 119 void SystemUIResourceManagerImpl::Data::Unsubscribe( |
| 120 ui::SystemUIResourceLayer* layer) { |
| 121 DCHECK(layer); |
| 122 layer->SetUIResourceId(0); |
| 123 subscribers_.RemoveObserver(layer); |
| 124 } |
| 125 |
| 126 void SystemUIResourceManagerImpl::Data::SetResource( |
| 127 scoped_ptr<SystemUIResource> resource) { |
| 128 DCHECK(resource); |
| 129 DCHECK(!resource_); |
| 130 resource_ = resource.Pass(); |
| 131 resource_->SetClient(this); |
| 132 |
| 133 RefreshResource(); |
| 134 } |
| 135 |
| 136 void SystemUIResourceManagerImpl::Data::RefreshResource() { |
| 137 if (!resource_) |
| 138 return; |
| 139 resource_->Load(); |
| 140 FOR_EACH_OBSERVER(ui::SystemUIResourceLayer, |
| 141 subscribers_, |
| 142 SetUIResourceId(resource_->id())); |
| 143 } |
| 144 |
| 145 bool SystemUIResourceManagerImpl::Data::HasResource() { |
| 146 return resource_; |
| 147 } |
| 148 |
| 149 // Implements SystemUIResourceClient. |
| 150 void SystemUIResourceManagerImpl::Data::OnUIResourceIdChanged() { |
| 151 DCHECK(resource_); |
| 152 FOR_EACH_OBSERVER(ui::SystemUIResourceLayer, |
| 153 subscribers_, |
| 154 SetUIResourceId(resource_->id())); |
| 155 } |
| 156 |
| 157 } // namespace content |
OLD | NEW |