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 if (fetcher_) |
| 26 fetcher_->Cancel(); |
| 27 |
| 28 // Consumers in the browser process will not receive this message but they |
| 29 // will be aware of the RenderFrame dying and should act on that. Consumers |
| 30 // in the renderer process should be correctly notified. |
| 31 ResolveCallbacks(ResolveStateFailure); |
| 32 } |
| 33 |
| 34 bool ManifestManager::OnMessageReceived(const IPC::Message& message) { |
| 35 bool handled = true; |
| 36 |
| 37 IPC_BEGIN_MESSAGE_MAP(ManifestManager, message) |
| 38 IPC_MESSAGE_HANDLER(ManifestManagerMsg_RequestManifest, OnRequestManifest) |
| 39 IPC_MESSAGE_UNHANDLED(handled = false) |
| 40 IPC_END_MESSAGE_MAP() |
| 41 |
| 42 return handled; |
| 43 } |
| 44 |
| 45 void ManifestManager::OnRequestManifest(int request_id) { |
| 46 GetManifest(base::Bind(&ManifestManager::OnRequestManifestComplete, |
| 47 base::Unretained(this), request_id)); |
| 48 } |
| 49 |
| 50 void ManifestManager::OnRequestManifestComplete( |
| 51 int request_id, const Manifest& manifest) { |
| 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::DidChangeManifest() { |
| 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 |
| 108 fetcher_.reset(); |
| 109 ResolveCallbacks(ResolveStateSuccess); |
| 110 } |
| 111 |
| 112 void ManifestManager::ResolveCallbacks(ResolveState state) { |
| 113 if (state == ResolveStateFailure) |
| 114 manifest_ = Manifest(); |
| 115 |
| 116 manifest_dirty_ = state != ResolveStateSuccess; |
| 117 |
| 118 Manifest manifest = manifest_; |
| 119 std::list<GetManifestCallback> callbacks = pending_callbacks_; |
| 120 |
| 121 pending_callbacks_.clear(); |
| 122 |
| 123 for (std::list<GetManifestCallback>::const_iterator it = callbacks.begin(); |
| 124 it != callbacks.end(); ++it) { |
| 125 it->Run(manifest); |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace content |
OLD | NEW |