Chromium Code Reviews| 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/renderer/manifest/manifest_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "content/common/manifest_manager_messages.h" | |
| 9 #include "content/public/renderer/render_frame.h" | |
| 10 #include "content/renderer/fetchers/manifest_fetcher.h" | |
| 11 #include "content/renderer/manifest/manifest_parser.h" | |
| 12 #include "third_party/WebKit/public/platform/WebURLResponse.h" | |
| 13 #include "third_party/WebKit/public/web/WebDocument.h" | |
| 14 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 ManifestManager::ManifestManager(RenderFrame* render_frame) | |
| 19 : RenderFrameObserver(render_frame), | |
| 20 may_have_manifest_(false), | |
| 21 manifest_dirty_(true) { | |
| 22 } | |
| 23 | |
| 24 ManifestManager::~ManifestManager() { | |
| 25 } | |
| 26 | |
| 27 bool ManifestManager::OnMessageReceived(const IPC::Message& message) { | |
| 28 bool handled = true; | |
| 29 | |
| 30 IPC_BEGIN_MESSAGE_MAP(ManifestManager, message) | |
| 31 IPC_MESSAGE_HANDLER(ManifestManagerMsg_RequestManifest, OnRequestManifest) | |
| 32 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 33 IPC_END_MESSAGE_MAP() | |
| 34 | |
| 35 return handled; | |
| 36 } | |
| 37 | |
| 38 void ManifestManager::OnRequestManifest(int request_id) { | |
| 39 request_ids_.push_back(request_id); | |
|
Michael van Ouwerkerk
2014/09/09 15:34:56
Why store this list of ids separately? You can bin
mlamouri (slow - plz ping)
2014/09/10 17:06:20
Done.
| |
| 40 GetManifest(base::Bind(&ManifestManager::OnRequestManifestComplete, | |
| 41 base::Unretained(this))); | |
| 42 } | |
| 43 | |
| 44 void ManifestManager::OnRequestManifestComplete( | |
| 45 const Manifest& manifest) { | |
| 46 // The ManifestManager will run the callbacks in FIFO order so the request_ids | |
| 47 // list can be used the same way. | |
| 48 // This is an implementation detail that the browser process is not aware of. | |
| 49 int request_id = request_ids_.front(); | |
| 50 request_ids_.pop_front(); | |
| 51 | |
| 52 Send(new ManifestManagerHostMsg_RequestManifestResponse( | |
| 53 routing_id(), request_id, manifest)); | |
| 54 } | |
| 55 | |
| 56 void ManifestManager::GetManifest(const GetManifestCallback& callback) { | |
| 57 if (!may_have_manifest_) { | |
| 58 callback.Run(Manifest()); | |
| 59 return; | |
| 60 } | |
| 61 | |
| 62 if (!manifest_dirty_) { | |
| 63 callback.Run(manifest_); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 pending_callbacks_.push_back(callback); | |
| 68 | |
| 69 // Just wait for the running call to be done if there are other callbacks. | |
| 70 if (pending_callbacks_.size() > 1) | |
| 71 return; | |
| 72 | |
| 73 DownloadManifest(); | |
| 74 } | |
| 75 | |
| 76 void ManifestManager::ManifestChanged() { | |
| 77 may_have_manifest_ = true; | |
| 78 manifest_dirty_ = true; | |
| 79 } | |
| 80 | |
| 81 void ManifestManager::DownloadManifest() { | |
| 82 GURL url(render_frame()->GetWebFrame()->document().manifestURL()); | |
| 83 | |
| 84 if (url.is_empty()) { | |
| 85 ResolveCallbacks(ResolveStateFailure); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 fetcher_.reset(new ManifestFetcher(url)); | |
| 90 | |
| 91 // TODO(mlamouri,kenneth): this is not yet taking into account manifest-src | |
| 92 // CSP rule, see http://crbug.com/409996. | |
| 93 fetcher_->Start(render_frame()->GetWebFrame(), | |
| 94 base::Bind(&ManifestManager::OnManifestFetchComplete, | |
| 95 base::Unretained(this))); | |
| 96 } | |
| 97 | |
| 98 void ManifestManager::OnManifestFetchComplete( | |
| 99 const blink::WebURLResponse& response, | |
| 100 const std::string& data) { | |
| 101 if (response.isNull() && data.empty()) { | |
| 102 ResolveCallbacks(ResolveStateFailure); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 manifest_ = ManifestParser::Parse(data); | |
| 107 ResolveCallbacks(ResolveStateSuccess); | |
| 108 } | |
| 109 | |
| 110 void ManifestManager::ResolveCallbacks(ResolveState state) { | |
| 111 if (state == ResolveStateFailure) | |
| 112 manifest_ = Manifest(); | |
| 113 | |
| 114 for (std::list<GetManifestCallback>::const_iterator | |
| 115 it = pending_callbacks_.begin(); it != pending_callbacks_.end(); ++it) { | |
| 116 it->Run(manifest_); | |
| 117 } | |
| 118 | |
| 119 pending_callbacks_.clear(); | |
| 120 manifest_dirty_ = state != ResolveStateSuccess; | |
| 121 } | |
| 122 | |
| 123 } // namespace content | |
| OLD | NEW |