| 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 <algorithm> | |
| 6 #include <string> | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "examples/bitmap_uploader/bitmap_uploader.h" | |
| 12 #include "mojo/application/application_runner_chromium.h" | |
| 13 #include "mojo/application/content_handler_factory.h" | |
| 14 #include "mojo/data_pipe_utils/data_pipe_utils.h" | |
| 15 #include "mojo/public/c/system/main.h" | |
| 16 #include "mojo/public/cpp/application/application_connection.h" | |
| 17 #include "mojo/public/cpp/application/application_delegate.h" | |
| 18 #include "mojo/public/cpp/application/application_impl.h" | |
| 19 #include "mojo/public/cpp/application/interface_factory_impl.h" | |
| 20 #include "mojo/public/cpp/application/service_provider_impl.h" | |
| 21 #include "mojo/services/content_handler/interfaces/content_handler.mojom.h" | |
| 22 #include "mojo/services/view_manager/cpp/types.h" | |
| 23 #include "mojo/services/view_manager/cpp/view.h" | |
| 24 #include "mojo/services/view_manager/cpp/view_manager.h" | |
| 25 #include "mojo/services/view_manager/cpp/view_manager_client_factory.h" | |
| 26 #include "mojo/services/view_manager/cpp/view_manager_delegate.h" | |
| 27 #include "mojo/services/view_manager/cpp/view_observer.h" | |
| 28 #include "third_party/skia/include/core/SkColor.h" | |
| 29 #include "ui/gfx/codec/png_codec.h" | |
| 30 | |
| 31 namespace mojo { | |
| 32 namespace examples { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 class EmbedderData { | |
| 37 public: | |
| 38 EmbedderData(Shell* shell, View* root) : bitmap_uploader_(root) { | |
| 39 bitmap_uploader_.Init(shell); | |
| 40 bitmap_uploader_.SetColor(SK_ColorGRAY); | |
| 41 } | |
| 42 | |
| 43 BitmapUploader& bitmap_uploader() { return bitmap_uploader_; } | |
| 44 | |
| 45 private: | |
| 46 BitmapUploader bitmap_uploader_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(EmbedderData); | |
| 49 }; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 // TODO(aa): Hook up ZoomableMedia interface again. | |
| 54 class PNGView : public ApplicationDelegate, | |
| 55 public ViewManagerDelegate, | |
| 56 public ViewObserver { | |
| 57 public: | |
| 58 PNGView(URLResponsePtr response) | |
| 59 : width_(0), | |
| 60 height_(0), | |
| 61 app_(nullptr), | |
| 62 zoom_percentage_(kDefaultZoomPercentage) { | |
| 63 DecodePNG(response.Pass()); | |
| 64 } | |
| 65 | |
| 66 ~PNGView() override { | |
| 67 for (auto& roots : embedder_for_roots_) { | |
| 68 roots.first->RemoveObserver(this); | |
| 69 delete roots.second; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 static const uint16_t kMaxZoomPercentage = 400; | |
| 75 static const uint16_t kMinZoomPercentage = 20; | |
| 76 static const uint16_t kDefaultZoomPercentage = 100; | |
| 77 static const uint16_t kZoomStep = 20; | |
| 78 | |
| 79 // Overridden from ApplicationDelegate: | |
| 80 void Initialize(ApplicationImpl* app) override { | |
| 81 app_ = app; | |
| 82 view_manager_client_factory_.reset( | |
| 83 new ViewManagerClientFactory(app->shell(), this)); | |
| 84 } | |
| 85 | |
| 86 // Overridden from ApplicationDelegate: | |
| 87 bool ConfigureIncomingConnection( | |
| 88 ApplicationConnection* connection) override { | |
| 89 connection->AddService(view_manager_client_factory_.get()); | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 // Overridden from ViewManagerDelegate: | |
| 94 void OnEmbed(View* root, | |
| 95 InterfaceRequest<ServiceProvider> services, | |
| 96 ServiceProviderPtr exposed_services) override { | |
| 97 // TODO(qsr): The same view should be embeddable on multiple views. | |
| 98 DCHECK(embedder_for_roots_.find(root) == embedder_for_roots_.end()); | |
| 99 root->AddObserver(this); | |
| 100 EmbedderData* embedder_data = new EmbedderData(app_->shell(), root); | |
| 101 embedder_for_roots_[root] = embedder_data; | |
| 102 embedder_data->bitmap_uploader().SetBitmap( | |
| 103 width_, height_, | |
| 104 make_scoped_ptr(new std::vector<unsigned char>(*bitmap_)), | |
| 105 BitmapUploader::BGRA); | |
| 106 } | |
| 107 | |
| 108 void OnViewManagerDisconnected(ViewManager* view_manager) override { | |
| 109 } | |
| 110 | |
| 111 // Overridden from ViewObserver: | |
| 112 void OnViewBoundsChanged(View* view, | |
| 113 const Rect& old_bounds, | |
| 114 const Rect& new_bounds) override { | |
| 115 DCHECK(embedder_for_roots_.find(view) != embedder_for_roots_.end()); | |
| 116 } | |
| 117 | |
| 118 void OnViewDestroyed(View* view) override { | |
| 119 // TODO(aa): Need to figure out how shutdown works. | |
| 120 const auto& it = embedder_for_roots_.find(view); | |
| 121 DCHECK(it != embedder_for_roots_.end()); | |
| 122 delete it->second; | |
| 123 embedder_for_roots_.erase(it); | |
| 124 if (embedder_for_roots_.size() == 0) | |
| 125 ApplicationImpl::Terminate(); | |
| 126 } | |
| 127 | |
| 128 void ZoomIn() { | |
| 129 // TODO(qsr,aa) Zoom should be per embedder view. | |
| 130 if (zoom_percentage_ >= kMaxZoomPercentage) | |
| 131 return; | |
| 132 zoom_percentage_ += kZoomStep; | |
| 133 } | |
| 134 | |
| 135 void ZoomOut() { | |
| 136 if (zoom_percentage_ <= kMinZoomPercentage) | |
| 137 return; | |
| 138 zoom_percentage_ -= kZoomStep; | |
| 139 } | |
| 140 | |
| 141 void ZoomToActualSize() { | |
| 142 if (zoom_percentage_ == kDefaultZoomPercentage) | |
| 143 return; | |
| 144 zoom_percentage_ = kDefaultZoomPercentage; | |
| 145 } | |
| 146 | |
| 147 void DecodePNG(URLResponsePtr response) { | |
| 148 std::string data; | |
| 149 mojo::common::BlockingCopyToString(response->body.Pass(), &data); | |
| 150 bitmap_.reset(new std::vector<unsigned char>); | |
| 151 gfx::PNGCodec::Decode(reinterpret_cast<const unsigned char*>(data.data()), | |
| 152 data.length(), gfx::PNGCodec::FORMAT_BGRA, | |
| 153 bitmap_.get(), &width_, &height_); | |
| 154 } | |
| 155 | |
| 156 int width_; | |
| 157 int height_; | |
| 158 scoped_ptr<std::vector<unsigned char>> bitmap_; | |
| 159 ApplicationImpl* app_; | |
| 160 std::map<View*, EmbedderData*> embedder_for_roots_; | |
| 161 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | |
| 162 uint16_t zoom_percentage_; | |
| 163 | |
| 164 DISALLOW_COPY_AND_ASSIGN(PNGView); | |
| 165 }; | |
| 166 | |
| 167 class PNGViewer : public ApplicationDelegate, | |
| 168 public ContentHandlerFactory::ManagedDelegate { | |
| 169 public: | |
| 170 PNGViewer() : content_handler_factory_(this) {} | |
| 171 | |
| 172 private: | |
| 173 // Overridden from ApplicationDelegate: | |
| 174 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | |
| 175 connection->AddService(&content_handler_factory_); | |
| 176 return true; | |
| 177 } | |
| 178 | |
| 179 // Overridden from ContentHandlerFactory::ManagedDelegate: | |
| 180 scoped_ptr<ContentHandlerFactory::HandledApplicationHolder> | |
| 181 CreateApplication(InterfaceRequest<Application> application_request, | |
| 182 URLResponsePtr response) override { | |
| 183 return make_handled_factory_holder(new mojo::ApplicationImpl( | |
| 184 new PNGView(response.Pass()), application_request.Pass())); | |
| 185 } | |
| 186 | |
| 187 ContentHandlerFactory content_handler_factory_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(PNGViewer); | |
| 190 }; | |
| 191 | |
| 192 } // namespace examples | |
| 193 } // namespace mojo | |
| 194 | |
| 195 MojoResult MojoMain(MojoHandle application_request) { | |
| 196 mojo::ApplicationRunnerChromium runner(new mojo::examples::PNGViewer()); | |
| 197 return runner.Run(application_request); | |
| 198 } | |
| OLD | NEW |