Remove Observer methods in RTCVideoEncoderFactory.
[chromium-blink-merge.git] / content / renderer / web_ui_mojo_context_state.cc
blob7757b26cf1316c0f3fcb45a32651a113cb7ee7d3
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.
5 #include "content/renderer/web_ui_mojo_context_state.h"
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "content/public/renderer/resource_fetcher.h"
10 #include "content/renderer/web_ui_runner.h"
11 #include "gin/converter.h"
12 #include "gin/modules/module_registry.h"
13 #include "gin/per_context_data.h"
14 #include "gin/public/context_holder.h"
15 #include "gin/try_catch.h"
16 #include "mojo/bindings/js/core.h"
17 #include "mojo/bindings/js/handle.h"
18 #include "mojo/bindings/js/support.h"
19 #include "third_party/WebKit/public/platform/WebURLResponse.h"
20 #include "third_party/WebKit/public/web/WebFrame.h"
21 #include "third_party/WebKit/public/web/WebScriptSource.h"
23 using v8::Context;
24 using v8::HandleScope;
25 using v8::Isolate;
26 using v8::Object;
27 using v8::ObjectTemplate;
28 using v8::Script;
30 namespace content {
32 namespace {
34 // All modules have this prefixed to them when downloading.
35 // TODO(sky): move this into some common place.
36 const char kModulePrefix[] = "chrome://mojo/";
38 void RunMain(base::WeakPtr<gin::Runner> runner,
39 v8::Handle<v8::Value> module) {
40 v8::Isolate* isolate = runner->GetContextHolder()->isolate();
41 v8::Handle<v8::Function> start;
42 CHECK(gin::ConvertFromV8(isolate, module, &start));
43 runner->Call(start, runner->global(), 0, NULL);
46 } // namespace
48 // WebUIMojo -------------------------------------------------------------------
50 WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame,
51 v8::Handle<v8::Context> context)
52 : frame_(frame),
53 module_added_(false) {
54 gin::PerContextData* context_data = gin::PerContextData::From(context);
55 gin::ContextHolder* context_holder = context_data->context_holder();
56 runner_.reset(new WebUIRunner(frame_, context_holder));
57 gin::Runner::Scope scoper(runner_.get());
58 gin::ModuleRegistry::From(context)->AddObserver(this);
59 runner_->RegisterBuiltinModules();
60 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global());
61 // Warning |frame| may be destroyed.
62 // TODO(sky): add test for this.
65 WebUIMojoContextState::~WebUIMojoContextState() {
66 gin::Runner::Scope scoper(runner_.get());
67 gin::ModuleRegistry::From(
68 runner_->GetContextHolder()->context())->RemoveObserver(this);
71 void WebUIMojoContextState::Run() {
72 gin::ContextHolder* context_holder = runner_->GetContextHolder();
73 gin::ModuleRegistry::From(context_holder->context())->LoadModule(
74 context_holder->isolate(),
75 "main",
76 base::Bind(RunMain, runner_->GetWeakPtr()));
79 void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) {
80 gin::Runner::Scope scoper(runner_.get());
81 gin::ContextHolder* context_holder = runner_->GetContextHolder();
82 gin::ModuleRegistry* registry = gin::ModuleRegistry::From(
83 context_holder->context());
84 for (size_t i = 0; i < ids.size(); ++i) {
85 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() &&
86 registry->available_modules().count(ids[i]) == 0) {
87 FetchModule(ids[i]);
92 void WebUIMojoContextState::FetchModule(const std::string& id) {
93 const GURL url(kModulePrefix + id);
94 // TODO(sky): better error checks here?
95 DCHECK(url.is_valid() && !url.is_empty());
96 DCHECK(fetched_modules_.find(id) == fetched_modules_.end());
97 fetched_modules_.insert(id);
98 ResourceFetcher* fetcher = ResourceFetcher::Create(url);
99 module_fetchers_.push_back(fetcher);
100 fetcher->Start(frame_,
101 blink::WebURLRequest::RequestContextScript,
102 blink::WebURLRequest::FrameTypeNone,
103 ResourceFetcher::PLATFORM_LOADER,
104 base::Bind(&WebUIMojoContextState::OnFetchModuleComplete,
105 base::Unretained(this),
106 fetcher));
109 void WebUIMojoContextState::OnFetchModuleComplete(
110 ResourceFetcher* fetcher,
111 const blink::WebURLResponse& response,
112 const std::string& data) {
113 DCHECK_EQ(kModulePrefix,
114 response.url().string().utf8().substr(0, arraysize(kModulePrefix) - 1));
115 const std::string module =
116 response.url().string().utf8().substr(arraysize(kModulePrefix) - 1);
117 // We can't delete fetch right now as the arguments to this function come from
118 // it and are used below. Instead use a scope_ptr to cleanup.
119 scoped_ptr<ResourceFetcher> deleter(fetcher);
120 module_fetchers_.weak_erase(
121 std::find(module_fetchers_.begin(), module_fetchers_.end(), fetcher));
122 if (data.empty()) {
123 NOTREACHED();
124 return; // TODO(sky): log something?
127 runner_->Run(data, module);
130 void WebUIMojoContextState::OnDidAddPendingModule(
131 const std::string& id,
132 const std::vector<std::string>& dependencies) {
133 FetchModules(dependencies);
135 gin::ContextHolder* context_holder = runner_->GetContextHolder();
136 gin::ModuleRegistry* registry = gin::ModuleRegistry::From(
137 context_holder->context());
138 registry->AttemptToLoadMoreModules(context_holder->isolate());
141 } // namespace content