OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #ifndef CHROME_BROWSER_UI_APP_LIST_ARC_ARC_APP_ICON_H_ |
| 6 #define CHROME_BROWSER_UI_APP_LIST_ARC_ARC_APP_ICON_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "ui/base/layout.h" |
| 14 #include "ui/gfx/image/image.h" |
| 15 #include "ui/gfx/image/image_skia.h" |
| 16 |
| 17 namespace base { |
| 18 class FilePath; |
| 19 } |
| 20 |
| 21 namespace content { |
| 22 class BrowserContext; |
| 23 } |
| 24 |
| 25 // A class that provides an ImageSkia for UI code to use. It handles ARC app |
| 26 // icon resource loading, screen scale factor change etc. UI code that uses |
| 27 // ARC app icon should host this class. |
| 28 class ArcAppIcon { |
| 29 public: |
| 30 class Observer { |
| 31 public: |
| 32 // Invoked when a new image rep for an additional scale factor |
| 33 // is loaded and added to |image|. |
| 34 virtual void OnIconUpdated() = 0; |
| 35 |
| 36 protected: |
| 37 virtual ~Observer() {} |
| 38 }; |
| 39 |
| 40 ArcAppIcon(content::BrowserContext* context, |
| 41 const std::string& app_id, |
| 42 int resource_size_in_dip, |
| 43 Observer* observer); |
| 44 ~ArcAppIcon(); |
| 45 |
| 46 gfx::Image image() const { return image_; } |
| 47 const gfx::ImageSkia& image_skia() const { return image_skia_; } |
| 48 |
| 49 // Icon loading is performed in several steps. It is initiated by |
| 50 // LoadImageForScaleFactor request that specifies a required scale factor. |
| 51 // ArcAppListPrefs is used to resolve a path to resource. Content of file is |
| 52 // asynchronously read in context of browser file thread. On successful read, |
| 53 // an icon data is decoded to an image in the special utility process. |
| 54 // DecodeRequest is used to interact with the utility process, and each |
| 55 // active request is stored at |decode_requests_| vector. When decoding is |
| 56 // complete, results are returned in context of UI thread, and corresponding |
| 57 // request is removed from |decode_requests_|. In case of some requests are |
| 58 // not completed by the time of deleting this icon, they are automatically |
| 59 // canceled. |
| 60 // In case of the icon file is not available this requests ArcAppListPrefs to |
| 61 // install required resource from ARC side. ArcAppListPrefs notifies UI items |
| 62 // that new icon is available and corresponding item should invoke |
| 63 // LoadImageForScaleFactor again. |
| 64 void LoadForScaleFactor(ui::ScaleFactor scale_factor); |
| 65 |
| 66 // Disables decoding requests when unit tests are executed. This is done to |
| 67 // avoid two problems. Problems come because icons are decoded at a separate |
| 68 // process created by ImageDecoder. ImageDecoder has 5 seconds delay to stop |
| 69 // since the last request (see its kBatchModeTimeoutSeconds for more details). |
| 70 // This is unacceptably long for unit tests because the test framework waits |
| 71 // until external process is finished. Another problem happens when we issue |
| 72 // a decoding request, but the process has not started its processing yet by |
| 73 // the time when a test exits. This might cause situation when |
| 74 // g_one_utility_thread_lock from in_process_utility_thread.cc gets released |
| 75 // in an acquired state which is crash condition in debug builds. |
| 76 static void DisableDecodingForTesting(); |
| 77 |
| 78 private: |
| 79 class Source; |
| 80 class DecodeRequest; |
| 81 struct ReadResult; |
| 82 |
| 83 void RequestIcon(ui::ScaleFactor scale_factor); |
| 84 static scoped_ptr<ArcAppIcon::ReadResult> ReadOnFileThread( |
| 85 ui::ScaleFactor scale_factor, |
| 86 const base::FilePath& path); |
| 87 void OnIconRead(scoped_ptr<ArcAppIcon::ReadResult> read_result); |
| 88 void Update(const gfx::ImageSkia* image); |
| 89 void DiscardDecodeRequest(DecodeRequest* request); |
| 90 |
| 91 content::BrowserContext* context_; |
| 92 std::string app_id_; |
| 93 const int resource_size_in_dip_; |
| 94 Observer* observer_; |
| 95 |
| 96 Source* source_ = nullptr; // Owned by ImageSkia storage. |
| 97 gfx::ImageSkia image_skia_; |
| 98 |
| 99 // The image wrapper around |image_skia_|. |
| 100 // Note: this is reset each time a new representation is loaded. |
| 101 gfx::Image image_; |
| 102 |
| 103 // Contains pending image decode requests. |
| 104 ScopedVector<DecodeRequest> decode_requests_; |
| 105 |
| 106 base::WeakPtrFactory<ArcAppIcon> weak_ptr_factory_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(ArcAppIcon); |
| 109 }; |
| 110 |
| 111 #endif // CHROME_BROWSER_UI_APP_LIST_ARC_ARC_APP_ICON_H_ |
OLD | NEW |