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 "base/strings/nullable_string16.h" |
| 9 #include "content/common/manifest_manager_messages.h" |
| 10 #include "content/public/renderer/render_frame.h" |
| 11 #include "content/renderer/fetchers/manifest_fetcher.h" |
| 12 #include "content/renderer/manifest/manifest_parser.h" |
| 13 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 14 #include "third_party/WebKit/public/web/WebDocument.h" |
| 15 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 ManifestManager::ManifestManager(RenderFrame* render_frame) |
| 20 : RenderFrameObserver(render_frame), |
| 21 may_have_manifest_(false), |
| 22 manifest_dirty_(true) { |
| 23 } |
| 24 |
| 25 ManifestManager::~ManifestManager() { |
| 26 if (fetcher_) |
| 27 fetcher_->Cancel(); |
| 28 |
| 29 // Consumers in the browser process will not receive this message but they |
| 30 // will be aware of the RenderFrame dying and should act on that. Consumers |
| 31 // in the renderer process should be correctly notified. |
| 32 ResolveCallbacks(ResolveStateFailure); |
| 33 } |
| 34 |
| 35 bool ManifestManager::OnMessageReceived(const IPC::Message& message) { |
| 36 bool handled = true; |
| 37 |
| 38 IPC_BEGIN_MESSAGE_MAP(ManifestManager, message) |
| 39 IPC_MESSAGE_HANDLER(ManifestManagerMsg_RequestManifest, OnRequestManifest) |
| 40 IPC_MESSAGE_UNHANDLED(handled = false) |
| 41 IPC_END_MESSAGE_MAP() |
| 42 |
| 43 return handled; |
| 44 } |
| 45 |
| 46 void ManifestManager::OnRequestManifest(int request_id) { |
| 47 GetManifest(base::Bind(&ManifestManager::OnRequestManifestComplete, |
| 48 base::Unretained(this), request_id)); |
| 49 } |
| 50 |
| 51 void ManifestManager::OnRequestManifestComplete( |
| 52 int request_id, const Manifest& manifest) { |
| 53 // When sent via IPC, the Manifest must follow certain security rules. |
| 54 Manifest ipc_manifest = manifest; |
| 55 ipc_manifest.name = base::NullableString16( |
| 56 ipc_manifest.name.string().substr(0, Manifest::kMaxIPCStringLength), |
| 57 ipc_manifest.name.is_null()); |
| 58 ipc_manifest.short_name = base::NullableString16( |
| 59 ipc_manifest.short_name.string().substr(0, |
| 60 Manifest::kMaxIPCStringLength), |
| 61 ipc_manifest.short_name.is_null()); |
| 62 |
| 63 Send(new ManifestManagerHostMsg_RequestManifestResponse( |
| 64 routing_id(), request_id, ipc_manifest)); |
| 65 } |
| 66 |
| 67 void ManifestManager::GetManifest(const GetManifestCallback& callback) { |
| 68 if (!may_have_manifest_) { |
| 69 callback.Run(Manifest()); |
| 70 return; |
| 71 } |
| 72 |
| 73 if (!manifest_dirty_) { |
| 74 callback.Run(manifest_); |
| 75 return; |
| 76 } |
| 77 |
| 78 pending_callbacks_.push_back(callback); |
| 79 |
| 80 // Just wait for the running call to be done if there are other callbacks. |
| 81 if (pending_callbacks_.size() > 1) |
| 82 return; |
| 83 |
| 84 FetchManifest(); |
| 85 } |
| 86 |
| 87 void ManifestManager::DidChangeManifest() { |
| 88 may_have_manifest_ = true; |
| 89 manifest_dirty_ = true; |
| 90 } |
| 91 |
| 92 void ManifestManager::FetchManifest() { |
| 93 GURL url(render_frame()->GetWebFrame()->document().manifestURL()); |
| 94 |
| 95 if (url.is_empty()) { |
| 96 ResolveCallbacks(ResolveStateFailure); |
| 97 return; |
| 98 } |
| 99 |
| 100 fetcher_.reset(new ManifestFetcher(url)); |
| 101 |
| 102 // TODO(mlamouri,kenneth): this is not yet taking into account manifest-src |
| 103 // CSP rule, see http://crbug.com/409996. |
| 104 fetcher_->Start(render_frame()->GetWebFrame(), |
| 105 base::Bind(&ManifestManager::OnManifestFetchComplete, |
| 106 base::Unretained(this))); |
| 107 } |
| 108 |
| 109 void ManifestManager::OnManifestFetchComplete( |
| 110 const blink::WebURLResponse& response, |
| 111 const std::string& data) { |
| 112 if (response.isNull() && data.empty()) { |
| 113 ResolveCallbacks(ResolveStateFailure); |
| 114 return; |
| 115 } |
| 116 |
| 117 manifest_ = ManifestParser::Parse(data); |
| 118 |
| 119 fetcher_.reset(); |
| 120 ResolveCallbacks(ResolveStateSuccess); |
| 121 } |
| 122 |
| 123 void ManifestManager::ResolveCallbacks(ResolveState state) { |
| 124 if (state == ResolveStateFailure) |
| 125 manifest_ = Manifest(); |
| 126 |
| 127 manifest_dirty_ = state != ResolveStateSuccess; |
| 128 |
| 129 Manifest manifest = manifest_; |
| 130 std::list<GetManifestCallback> callbacks = pending_callbacks_; |
| 131 |
| 132 pending_callbacks_.clear(); |
| 133 |
| 134 for (std::list<GetManifestCallback>::const_iterator it = callbacks.begin(); |
| 135 it != callbacks.end(); ++it) { |
| 136 it->Run(manifest); |
| 137 } |
| 138 } |
| 139 |
| 140 } // namespace content |
OLD | NEW |