| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/tools/package_manager/package_manager_application.h" | 5 #include "mojo/tools/package_manager/package_manager_application.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "mojo/tools/package_manager/manifest.h" |
| 9 #include "mojo/tools/package_manager/unpacker.h" |
| 8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 9 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
| 11 | 13 |
| 12 namespace mojo { | 14 namespace mojo { |
| 13 | 15 |
| 16 namespace { |
| 17 |
| 18 const base::FilePath::CharType kManifestFileName[] = |
| 19 FILE_PATH_LITERAL("manifest.json"); |
| 20 |
| 21 } // namespace |
| 22 |
| 14 PackageManagerApplication::PendingLoad::PendingLoad() { | 23 PackageManagerApplication::PendingLoad::PendingLoad() { |
| 15 } | 24 } |
| 16 | 25 |
| 17 PackageManagerApplication::PendingLoad::~PendingLoad() { | 26 PackageManagerApplication::PendingLoad::~PendingLoad() { |
| 18 } | 27 } |
| 19 | 28 |
| 20 PackageManagerApplication::PackageManagerApplication() { | 29 PackageManagerApplication::PackageManagerApplication() { |
| 21 } | 30 } |
| 22 | 31 |
| 23 PackageManagerApplication::~PackageManagerApplication() { | 32 PackageManagerApplication::~PackageManagerApplication() { |
| 24 printf("APPLICATION EXITING\n"); | |
| 25 STLDeleteContainerPairSecondPointers(pending_.begin(), pending_.end()); | 33 STLDeleteContainerPairSecondPointers(pending_.begin(), pending_.end()); |
| 26 } | 34 } |
| 27 | 35 |
| 28 void PackageManagerApplication::Initialize(ApplicationImpl* app) { | 36 void PackageManagerApplication::Initialize(ApplicationImpl* app) { |
| 29 app->ConnectToService("mojo:mojo_network_service", &network_service_); | 37 app->ConnectToService("mojo:mojo_network_service", &network_service_); |
| 30 | 38 |
| 31 printf("Enter URL> "); | 39 printf("Enter URL> "); |
| 32 char buf[1024]; | 40 char buf[1024]; |
| 33 if (scanf("%1023s", buf) != 1) { | 41 if (scanf("%1023s", buf) != 1) { |
| 34 printf("No input, exiting\n"); | 42 printf("No input, exiting\n"); |
| 35 base::MessageLoop::current()->Quit(); | 43 base::MessageLoop::current()->Quit(); |
| 36 return; | 44 return; |
| 37 } | 45 } |
| 38 GURL url(buf); | 46 GURL url(buf); |
| 39 if (!url.is_valid()) { | 47 if (!url.is_valid()) { |
| 40 printf("Invalid URL\n"); | 48 printf("Invalid URL\n"); |
| 41 base::MessageLoop::current()->Quit(); | 49 base::MessageLoop::current()->Quit(); |
| 42 return; | 50 return; |
| 43 } | 51 } |
| 44 | 52 |
| 53 StartLoad(url); |
| 54 } |
| 55 |
| 56 void PackageManagerApplication::StartLoad(const GURL& url) { |
| 57 if (completed_.find(url) != completed_.end() || |
| 58 pending_.find(url) != pending_.end()) |
| 59 return; // Already loaded or are loading this one. |
| 60 |
| 45 PendingLoad* load = new PendingLoad; | 61 PendingLoad* load = new PendingLoad; |
| 46 load->fetcher.reset(new mojo::PackageFetcher( | 62 load->fetcher.reset(new mojo::PackageFetcher( |
| 47 network_service_.get(), this, url)); | 63 network_service_.get(), this, url)); |
| 64 pending_[url] = load; |
| 65 } |
| 48 | 66 |
| 49 pending_[url] = load; | 67 void PackageManagerApplication::LoadDeps(const Manifest& manifest) { |
| 68 for (size_t i = 0; i < manifest.deps().size(); i++) |
| 69 StartLoad(manifest.deps()[i]); |
| 50 } | 70 } |
| 51 | 71 |
| 52 void PackageManagerApplication::PendingLoadComplete(const GURL& url) { | 72 void PackageManagerApplication::PendingLoadComplete(const GURL& url) { |
| 53 pending_.erase(pending_.find(url)); | 73 pending_.erase(pending_.find(url)); |
| 74 completed_.insert(url); |
| 54 if (pending_.empty()) | 75 if (pending_.empty()) |
| 55 base::MessageLoop::current()->Quit(); | 76 base::MessageLoop::current()->Quit(); |
| 56 } | 77 } |
| 57 | 78 |
| 58 void PackageManagerApplication::FetchSucceeded( | 79 void PackageManagerApplication::FetchSucceeded( |
| 59 const GURL& url, | 80 const GURL& url, |
| 60 const base::FilePath& name) { | 81 const base::FilePath& name) { |
| 61 PendingLoad* load = pending_.find(url)->second; | 82 Unpacker unpacker; |
| 62 | 83 if (!unpacker.Unpack(name)) { |
| 63 if (!load->unpacker.Unpack(name)) { | |
| 64 base::DeleteFile(name, false); | 84 base::DeleteFile(name, false); |
| 65 printf("Failed to unpack\n"); | 85 printf("Failed to unpack\n"); |
| 66 PendingLoadComplete(url); | 86 PendingLoadComplete(url); |
| 67 return; | 87 return; |
| 68 } | 88 } |
| 89 // The downloaded .zip file is no longer necessary. |
| 69 base::DeleteFile(name, false); | 90 base::DeleteFile(name, false); |
| 70 | 91 |
| 92 // Load the manifest. |
| 93 base::FilePath manifest_path = unpacker.dir().Append(kManifestFileName); |
| 94 Manifest manifest; |
| 95 std::string err_msg; |
| 96 if (!manifest.ParseFromFile(manifest_path, &err_msg)) { |
| 97 printf("%s\n", err_msg.c_str()); |
| 98 PendingLoadComplete(url); |
| 99 return; |
| 100 } |
| 101 |
| 102 // Enqueue loads for any deps. |
| 103 LoadDeps(manifest); |
| 104 |
| 105 printf("Loaded %s\n", url.spec().c_str()); |
| 71 PendingLoadComplete(url); | 106 PendingLoadComplete(url); |
| 72 } | 107 } |
| 73 | 108 |
| 74 void PackageManagerApplication::FetchFailed(const GURL& url) { | 109 void PackageManagerApplication::FetchFailed(const GURL& url) { |
| 75 PendingLoadComplete(url); | 110 PendingLoadComplete(url); |
| 76 } | 111 } |
| 77 | 112 |
| 78 } // namespace mojo | 113 } // namespace mojo |
| OLD | NEW |