Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / api / app_runtime / app_runtime_api.cc
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 "extensions/browser/api/app_runtime/app_runtime_api.h"
6
7 #include "base/metrics/histogram.h"
8 #include "base/time/time.h"
9 #include "base/values.h"
10 #include "extensions/browser/event_router.h"
11 #include "extensions/browser/extension_prefs.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/extensions_browser_client.h"
14 #include "extensions/browser/granted_file_entry.h"
15 #include "extensions/common/api/app_runtime.h"
16 #include "extensions/common/constants.h"
17 #include "extensions/common/feature_switch.h"
18 #include "url/gurl.h"
19
20 using content::BrowserContext;
21
22 namespace extensions {
23
24 namespace app_runtime = core_api::app_runtime;
25
26 namespace {
27
28 void DispatchOnEmbedRequestedEventImpl(
29     const std::string& extension_id,
30     scoped_ptr<base::DictionaryValue> app_embedding_request_data,
31     content::BrowserContext* context) {
32   scoped_ptr<base::ListValue> args(new base::ListValue());
33   args->Append(app_embedding_request_data.release());
34   ExtensionSystem* system = ExtensionSystem::Get(context);
35   scoped_ptr<Event> event(
36       new Event(app_runtime::OnEmbedRequested::kEventName, args.Pass()));
37   event->restrict_to_browser_context = context;
38   system->event_router()->DispatchEventWithLazyListener(extension_id,
39                                                         event.Pass());
40
41   ExtensionPrefs::Get(context)
42       ->SetLastLaunchTime(extension_id, base::Time::Now());
43 }
44
45 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
46                                  app_runtime::LaunchSource source,
47                                  scoped_ptr<base::DictionaryValue> launch_data,
48                                  BrowserContext* context) {
49   UMA_HISTOGRAM_ENUMERATION(
50       "Extensions.AppLaunchSource", source, NUM_APP_LAUNCH_SOURCES);
51
52   // "Forced app mode" is true for Chrome OS kiosk mode.
53   launch_data->SetBoolean(
54       "isKioskSession",
55       ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode());
56   scoped_ptr<base::ListValue> args(new base::ListValue());
57   args->Append(launch_data.release());
58   scoped_ptr<Event> event(
59       new Event(app_runtime::OnLaunched::kEventName, args.Pass()));
60   event->restrict_to_browser_context = context;
61   EventRouter::Get(context)
62       ->DispatchEventWithLazyListener(extension_id, event.Pass());
63   ExtensionPrefs::Get(context)
64       ->SetLastLaunchTime(extension_id, base::Time::Now());
65 }
66
67 app_runtime::LaunchSource getLaunchSourceEnum(
68     extensions::AppLaunchSource source) {
69   switch (source) {
70     case extensions::SOURCE_APP_LAUNCHER:
71       return app_runtime::LAUNCH_SOURCE_APP_LAUNCHER;
72     case extensions::SOURCE_NEW_TAB_PAGE:
73       return app_runtime::LAUNCH_SOURCE_NEW_TAB_PAGE;
74     case extensions::SOURCE_RELOAD:
75       return app_runtime::LAUNCH_SOURCE_RELOAD;
76     case extensions::SOURCE_RESTART:
77       return app_runtime::LAUNCH_SOURCE_RESTART;
78     case extensions::SOURCE_LOAD_AND_LAUNCH:
79       return app_runtime::LAUNCH_SOURCE_LOAD_AND_LAUNCH;
80     case extensions::SOURCE_COMMAND_LINE:
81       return app_runtime::LAUNCH_SOURCE_COMMAND_LINE;
82     case extensions::SOURCE_FILE_HANDLER:
83       return app_runtime::LAUNCH_SOURCE_FILE_HANDLER;
84     case extensions::SOURCE_URL_HANDLER:
85       return app_runtime::LAUNCH_SOURCE_URL_HANDLER;
86
87     case extensions::SOURCE_SYSTEM_TRAY:
88       return app_runtime::LAUNCH_SOURCE_SYSTEM_TRAY;
89     case extensions::SOURCE_ABOUT_PAGE:
90       return app_runtime::LAUNCH_SOURCE_ABOUT_PAGE;
91     case extensions::SOURCE_KEYBOARD:
92       return app_runtime::LAUNCH_SOURCE_KEYBOARD;
93
94     default:
95       return app_runtime::LAUNCH_SOURCE_NONE;
96   }
97 }
98
99 }  // namespace
100
101 // static
102 void AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
103     content::BrowserContext* context,
104     scoped_ptr<base::DictionaryValue> embed_app_data,
105     const Extension* extension) {
106   DispatchOnEmbedRequestedEventImpl(
107       extension->id(), embed_app_data.Pass(), context);
108 }
109
110 // static
111 void AppRuntimeEventRouter::DispatchOnLaunchedEvent(
112     BrowserContext* context,
113     const Extension* extension,
114     extensions::AppLaunchSource source) {
115   app_runtime::LaunchData launch_data;
116
117   app_runtime::LaunchSource source_enum = getLaunchSourceEnum(source);
118   if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
119     launch_data.source = source_enum;
120   }
121   DispatchOnLaunchedEventImpl(
122       extension->id(), source_enum, launch_data.ToValue().Pass(), context);
123 }
124
125 // static
126 void AppRuntimeEventRouter::DispatchOnRestartedEvent(
127     BrowserContext* context,
128     const Extension* extension) {
129   scoped_ptr<base::ListValue> arguments(new base::ListValue());
130   scoped_ptr<Event> event(
131       new Event(app_runtime::OnRestarted::kEventName, arguments.Pass()));
132   event->restrict_to_browser_context = context;
133   EventRouter::Get(context)
134       ->DispatchEventToExtension(extension->id(), event.Pass());
135 }
136
137 // static
138 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithFileEntries(
139     BrowserContext* context,
140     const Extension* extension,
141     const std::string& handler_id,
142     const std::vector<std::string>& mime_types,
143     const std::vector<GrantedFileEntry>& file_entries) {
144   // TODO(sergeygs): Use the same way of creating an event (using the generated
145   // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
146   scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue);
147   launch_data->SetString("id", handler_id);
148
149   app_runtime::LaunchSource source_enum =
150       app_runtime::LAUNCH_SOURCE_FILE_HANDLER;
151   if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
152     launch_data->SetString("source", app_runtime::ToString(source_enum));
153   }
154
155   scoped_ptr<base::ListValue> items(new base::ListValue);
156   DCHECK(file_entries.size() == mime_types.size());
157   for (size_t i = 0; i < file_entries.size(); ++i) {
158     scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue);
159
160     launch_item->SetString("fileSystemId", file_entries[i].filesystem_id);
161     launch_item->SetString("baseName", file_entries[i].registered_name);
162     launch_item->SetString("mimeType", mime_types[i]);
163     launch_item->SetString("entryId", file_entries[i].id);
164     items->Append(launch_item.release());
165   }
166   launch_data->Set("items", items.release());
167   DispatchOnLaunchedEventImpl(
168       extension->id(), source_enum, launch_data.Pass(), context);
169 }
170
171 // static
172 void AppRuntimeEventRouter::DispatchOnLaunchedEventWithUrl(
173     BrowserContext* context,
174     const Extension* extension,
175     const std::string& handler_id,
176     const GURL& url,
177     const GURL& referrer_url) {
178   app_runtime::LaunchData launch_data;
179   app_runtime::LaunchSource source_enum =
180       app_runtime::LAUNCH_SOURCE_URL_HANDLER;
181   launch_data.id.reset(new std::string(handler_id));
182   launch_data.url.reset(new std::string(url.spec()));
183   launch_data.referrer_url.reset(new std::string(referrer_url.spec()));
184   if (extensions::FeatureSwitch::trace_app_source()->IsEnabled()) {
185     launch_data.source = source_enum;
186   }
187   DispatchOnLaunchedEventImpl(
188       extension->id(), source_enum, launch_data.ToValue().Pass(), context);
189 }
190
191 }  // namespace extensions