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 "apps/browser/api/app_runtime/app_runtime_api.h"
7 #include "apps/browser/file_handler_util.h"
8 #include "apps/common/api/app_runtime.h"
9 #include "base/time/time.h"
10 #include "base/values.h"
11 #include "extensions/browser/event_router.h"
12 #include "extensions/browser/extension_prefs.h"
13 #include "extensions/browser/extensions_browser_client.h"
16 using content::BrowserContext;
17 using extensions::Event;
18 using extensions::Extension;
19 using extensions::ExtensionPrefs;
23 namespace app_runtime = api::app_runtime;
27 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
28 scoped_ptr<base::DictionaryValue> launch_data,
29 BrowserContext* context) {
30 // "Forced app mode" is true for Chrome OS kiosk mode.
31 launch_data->SetBoolean(
33 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode());
34 scoped_ptr<base::ListValue> args(new base::ListValue());
35 args->Append(launch_data.release());
36 scoped_ptr<Event> event(
37 new Event(app_runtime::OnLaunched::kEventName, args.Pass()));
38 event->restrict_to_browser_context = context;
39 event->can_load_ephemeral_apps = true;
40 extensions::EventRouter::Get(context)
41 ->DispatchEventWithLazyListener(extension_id, event.Pass());
42 ExtensionPrefs::Get(context)
43 ->SetLastLaunchTime(extension_id, base::Time::Now());
46 } // anonymous namespace
49 void AppEventRouter::DispatchOnLaunchedEvent(BrowserContext* context,
50 const Extension* extension) {
51 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue());
52 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context);
56 void AppEventRouter::DispatchOnRestartedEvent(BrowserContext* context,
57 const Extension* extension) {
58 scoped_ptr<base::ListValue> arguments(new base::ListValue());
59 scoped_ptr<Event> event(
60 new Event(app_runtime::OnRestarted::kEventName, arguments.Pass()));
61 event->restrict_to_browser_context = context;
62 event->can_load_ephemeral_apps = true;
63 extensions::EventRouter::Get(context)
64 ->DispatchEventToExtension(extension->id(), event.Pass());
68 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
69 BrowserContext* context,
70 const Extension* extension,
71 const std::string& handler_id,
72 const std::string& mime_type,
73 const file_handler_util::GrantedFileEntry& file_entry) {
74 // TODO(sergeygs): Use the same way of creating an event (using the generated
75 // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
76 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue);
77 launch_data->SetString("id", handler_id);
78 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue);
79 launch_item->SetString("fileSystemId", file_entry.filesystem_id);
80 launch_item->SetString("baseName", file_entry.registered_name);
81 launch_item->SetString("mimeType", mime_type);
82 launch_item->SetString("entryId", file_entry.id);
83 scoped_ptr<base::ListValue> items(new base::ListValue);
84 items->Append(launch_item.release());
85 launch_data->Set("items", items.release());
86 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), context);
90 void AppEventRouter::DispatchOnLaunchedEventWithUrl(
91 BrowserContext* context,
92 const Extension* extension,
93 const std::string& handler_id,
95 const GURL& referrer_url) {
96 api::app_runtime::LaunchData launch_data;
97 launch_data.id.reset(new std::string(handler_id));
98 launch_data.url.reset(new std::string(url.spec()));
99 launch_data.referrer_url.reset(new std::string(referrer_url.spec()));
100 DispatchOnLaunchedEventImpl(
101 extension->id(), launch_data.ToValue().Pass(), context);